megaraid_sas_base.c 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2009-2011 LSI Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * FILE: megaraid_sas_base.c
  21. * Version : v00.00.05.34-rc1
  22. *
  23. * Authors: LSI Corporation
  24. * Sreenivas Bagalkote
  25. * Sumant Patro
  26. * Bo Yang
  27. * Adam Radford <linuxraid@lsi.com>
  28. *
  29. * Send feedback to: <megaraidlinux@lsi.com>
  30. *
  31. * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
  32. * ATTN: Linuxraid
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/types.h>
  36. #include <linux/pci.h>
  37. #include <linux/list.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/module.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/delay.h>
  43. #include <linux/uio.h>
  44. #include <linux/slab.h>
  45. #include <asm/uaccess.h>
  46. #include <linux/fs.h>
  47. #include <linux/compat.h>
  48. #include <linux/blkdev.h>
  49. #include <linux/mutex.h>
  50. #include <linux/poll.h>
  51. #include <scsi/scsi.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #include <scsi/scsi_device.h>
  54. #include <scsi/scsi_host.h>
  55. #include "megaraid_sas_fusion.h"
  56. #include "megaraid_sas.h"
  57. /*
  58. * poll_mode_io:1- schedule complete completion from q cmd
  59. */
  60. static unsigned int poll_mode_io;
  61. module_param_named(poll_mode_io, poll_mode_io, int, 0);
  62. MODULE_PARM_DESC(poll_mode_io,
  63. "Complete cmds from IO path, (default=0)");
  64. /*
  65. * Number of sectors per IO command
  66. * Will be set in megasas_init_mfi if user does not provide
  67. */
  68. static unsigned int max_sectors;
  69. module_param_named(max_sectors, max_sectors, int, 0);
  70. MODULE_PARM_DESC(max_sectors,
  71. "Maximum number of sectors per IO command");
  72. static int msix_disable;
  73. module_param(msix_disable, int, S_IRUGO);
  74. MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
  75. MODULE_LICENSE("GPL");
  76. MODULE_VERSION(MEGASAS_VERSION);
  77. MODULE_AUTHOR("megaraidlinux@lsi.com");
  78. MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
  79. int megasas_transition_to_ready(struct megasas_instance *instance);
  80. static int megasas_get_pd_list(struct megasas_instance *instance);
  81. static int megasas_issue_init_mfi(struct megasas_instance *instance);
  82. static int megasas_register_aen(struct megasas_instance *instance,
  83. u32 seq_num, u32 class_locale_word);
  84. /*
  85. * PCI ID table for all supported controllers
  86. */
  87. static struct pci_device_id megasas_pci_table[] = {
  88. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
  89. /* xscale IOP */
  90. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
  91. /* ppc IOP */
  92. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
  93. /* ppc IOP */
  94. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
  95. /* gen2*/
  96. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
  97. /* gen2*/
  98. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
  99. /* skinny*/
  100. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
  101. /* skinny*/
  102. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
  103. /* xscale IOP, vega */
  104. {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
  105. /* xscale IOP */
  106. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
  107. /* Fusion */
  108. {}
  109. };
  110. MODULE_DEVICE_TABLE(pci, megasas_pci_table);
  111. static int megasas_mgmt_majorno;
  112. static struct megasas_mgmt_info megasas_mgmt_info;
  113. static struct fasync_struct *megasas_async_queue;
  114. static DEFINE_MUTEX(megasas_async_queue_mutex);
  115. static int megasas_poll_wait_aen;
  116. static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
  117. static u32 support_poll_for_event;
  118. u32 megasas_dbg_lvl;
  119. static u32 support_device_change;
  120. /* define lock for aen poll */
  121. spinlock_t poll_aen_lock;
  122. void
  123. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  124. u8 alt_status);
  125. static u32
  126. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
  127. static int
  128. megasas_adp_reset_gen2(struct megasas_instance *instance,
  129. struct megasas_register_set __iomem *reg_set);
  130. static irqreturn_t megasas_isr(int irq, void *devp);
  131. static u32
  132. megasas_init_adapter_mfi(struct megasas_instance *instance);
  133. u32
  134. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  135. struct scsi_cmnd *scmd);
  136. static void megasas_complete_cmd_dpc(unsigned long instance_addr);
  137. void
  138. megasas_release_fusion(struct megasas_instance *instance);
  139. int
  140. megasas_ioc_init_fusion(struct megasas_instance *instance);
  141. void
  142. megasas_free_cmds_fusion(struct megasas_instance *instance);
  143. u8
  144. megasas_get_map_info(struct megasas_instance *instance);
  145. int
  146. megasas_sync_map_info(struct megasas_instance *instance);
  147. int
  148. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
  149. void megasas_reset_reply_desc(struct megasas_instance *instance);
  150. u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
  151. struct LD_LOAD_BALANCE_INFO *lbInfo);
  152. int megasas_reset_fusion(struct Scsi_Host *shost);
  153. void megasas_fusion_ocr_wq(struct work_struct *work);
  154. void
  155. megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  156. {
  157. instance->instancet->fire_cmd(instance,
  158. cmd->frame_phys_addr, 0, instance->reg_set);
  159. }
  160. /**
  161. * megasas_get_cmd - Get a command from the free pool
  162. * @instance: Adapter soft state
  163. *
  164. * Returns a free command from the pool
  165. */
  166. struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  167. *instance)
  168. {
  169. unsigned long flags;
  170. struct megasas_cmd *cmd = NULL;
  171. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  172. if (!list_empty(&instance->cmd_pool)) {
  173. cmd = list_entry((&instance->cmd_pool)->next,
  174. struct megasas_cmd, list);
  175. list_del_init(&cmd->list);
  176. } else {
  177. printk(KERN_ERR "megasas: Command pool empty!\n");
  178. }
  179. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  180. return cmd;
  181. }
  182. /**
  183. * megasas_return_cmd - Return a cmd to free command pool
  184. * @instance: Adapter soft state
  185. * @cmd: Command packet to be returned to free command pool
  186. */
  187. inline void
  188. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  189. {
  190. unsigned long flags;
  191. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  192. cmd->scmd = NULL;
  193. cmd->frame_count = 0;
  194. list_add_tail(&cmd->list, &instance->cmd_pool);
  195. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  196. }
  197. /**
  198. * The following functions are defined for xscale
  199. * (deviceid : 1064R, PERC5) controllers
  200. */
  201. /**
  202. * megasas_enable_intr_xscale - Enables interrupts
  203. * @regs: MFI register set
  204. */
  205. static inline void
  206. megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
  207. {
  208. writel(0, &(regs)->outbound_intr_mask);
  209. /* Dummy readl to force pci flush */
  210. readl(&regs->outbound_intr_mask);
  211. }
  212. /**
  213. * megasas_disable_intr_xscale -Disables interrupt
  214. * @regs: MFI register set
  215. */
  216. static inline void
  217. megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
  218. {
  219. u32 mask = 0x1f;
  220. writel(mask, &regs->outbound_intr_mask);
  221. /* Dummy readl to force pci flush */
  222. readl(&regs->outbound_intr_mask);
  223. }
  224. /**
  225. * megasas_read_fw_status_reg_xscale - returns the current FW status value
  226. * @regs: MFI register set
  227. */
  228. static u32
  229. megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
  230. {
  231. return readl(&(regs)->outbound_msg_0);
  232. }
  233. /**
  234. * megasas_clear_interrupt_xscale - Check & clear interrupt
  235. * @regs: MFI register set
  236. */
  237. static int
  238. megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
  239. {
  240. u32 status;
  241. u32 mfiStatus = 0;
  242. /*
  243. * Check if it is our interrupt
  244. */
  245. status = readl(&regs->outbound_intr_status);
  246. if (status & MFI_OB_INTR_STATUS_MASK)
  247. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  248. if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
  249. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  250. /*
  251. * Clear the interrupt by writing back the same value
  252. */
  253. if (mfiStatus)
  254. writel(status, &regs->outbound_intr_status);
  255. /* Dummy readl to force pci flush */
  256. readl(&regs->outbound_intr_status);
  257. return mfiStatus;
  258. }
  259. /**
  260. * megasas_fire_cmd_xscale - Sends command to the FW
  261. * @frame_phys_addr : Physical address of cmd
  262. * @frame_count : Number of frames for the command
  263. * @regs : MFI register set
  264. */
  265. static inline void
  266. megasas_fire_cmd_xscale(struct megasas_instance *instance,
  267. dma_addr_t frame_phys_addr,
  268. u32 frame_count,
  269. struct megasas_register_set __iomem *regs)
  270. {
  271. unsigned long flags;
  272. spin_lock_irqsave(&instance->hba_lock, flags);
  273. writel((frame_phys_addr >> 3)|(frame_count),
  274. &(regs)->inbound_queue_port);
  275. spin_unlock_irqrestore(&instance->hba_lock, flags);
  276. }
  277. /**
  278. * megasas_adp_reset_xscale - For controller reset
  279. * @regs: MFI register set
  280. */
  281. static int
  282. megasas_adp_reset_xscale(struct megasas_instance *instance,
  283. struct megasas_register_set __iomem *regs)
  284. {
  285. u32 i;
  286. u32 pcidata;
  287. writel(MFI_ADP_RESET, &regs->inbound_doorbell);
  288. for (i = 0; i < 3; i++)
  289. msleep(1000); /* sleep for 3 secs */
  290. pcidata = 0;
  291. pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
  292. printk(KERN_NOTICE "pcidata = %x\n", pcidata);
  293. if (pcidata & 0x2) {
  294. printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
  295. pcidata &= ~0x2;
  296. pci_write_config_dword(instance->pdev,
  297. MFI_1068_PCSR_OFFSET, pcidata);
  298. for (i = 0; i < 2; i++)
  299. msleep(1000); /* need to wait 2 secs again */
  300. pcidata = 0;
  301. pci_read_config_dword(instance->pdev,
  302. MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
  303. printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
  304. if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
  305. printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
  306. pcidata = 0;
  307. pci_write_config_dword(instance->pdev,
  308. MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
  309. }
  310. }
  311. return 0;
  312. }
  313. /**
  314. * megasas_check_reset_xscale - For controller reset check
  315. * @regs: MFI register set
  316. */
  317. static int
  318. megasas_check_reset_xscale(struct megasas_instance *instance,
  319. struct megasas_register_set __iomem *regs)
  320. {
  321. u32 consumer;
  322. consumer = *instance->consumer;
  323. if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
  324. (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. static struct megasas_instance_template megasas_instance_template_xscale = {
  330. .fire_cmd = megasas_fire_cmd_xscale,
  331. .enable_intr = megasas_enable_intr_xscale,
  332. .disable_intr = megasas_disable_intr_xscale,
  333. .clear_intr = megasas_clear_intr_xscale,
  334. .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
  335. .adp_reset = megasas_adp_reset_xscale,
  336. .check_reset = megasas_check_reset_xscale,
  337. .service_isr = megasas_isr,
  338. .tasklet = megasas_complete_cmd_dpc,
  339. .init_adapter = megasas_init_adapter_mfi,
  340. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  341. .issue_dcmd = megasas_issue_dcmd,
  342. };
  343. /**
  344. * This is the end of set of functions & definitions specific
  345. * to xscale (deviceid : 1064R, PERC5) controllers
  346. */
  347. /**
  348. * The following functions are defined for ppc (deviceid : 0x60)
  349. * controllers
  350. */
  351. /**
  352. * megasas_enable_intr_ppc - Enables interrupts
  353. * @regs: MFI register set
  354. */
  355. static inline void
  356. megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
  357. {
  358. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  359. writel(~0x80000000, &(regs)->outbound_intr_mask);
  360. /* Dummy readl to force pci flush */
  361. readl(&regs->outbound_intr_mask);
  362. }
  363. /**
  364. * megasas_disable_intr_ppc - Disable interrupt
  365. * @regs: MFI register set
  366. */
  367. static inline void
  368. megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
  369. {
  370. u32 mask = 0xFFFFFFFF;
  371. writel(mask, &regs->outbound_intr_mask);
  372. /* Dummy readl to force pci flush */
  373. readl(&regs->outbound_intr_mask);
  374. }
  375. /**
  376. * megasas_read_fw_status_reg_ppc - returns the current FW status value
  377. * @regs: MFI register set
  378. */
  379. static u32
  380. megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
  381. {
  382. return readl(&(regs)->outbound_scratch_pad);
  383. }
  384. /**
  385. * megasas_clear_interrupt_ppc - Check & clear interrupt
  386. * @regs: MFI register set
  387. */
  388. static int
  389. megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
  390. {
  391. u32 status;
  392. /*
  393. * Check if it is our interrupt
  394. */
  395. status = readl(&regs->outbound_intr_status);
  396. if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
  397. return 0;
  398. }
  399. /*
  400. * Clear the interrupt by writing back the same value
  401. */
  402. writel(status, &regs->outbound_doorbell_clear);
  403. /* Dummy readl to force pci flush */
  404. readl(&regs->outbound_doorbell_clear);
  405. return 1;
  406. }
  407. /**
  408. * megasas_fire_cmd_ppc - Sends command to the FW
  409. * @frame_phys_addr : Physical address of cmd
  410. * @frame_count : Number of frames for the command
  411. * @regs : MFI register set
  412. */
  413. static inline void
  414. megasas_fire_cmd_ppc(struct megasas_instance *instance,
  415. dma_addr_t frame_phys_addr,
  416. u32 frame_count,
  417. struct megasas_register_set __iomem *regs)
  418. {
  419. unsigned long flags;
  420. spin_lock_irqsave(&instance->hba_lock, flags);
  421. writel((frame_phys_addr | (frame_count<<1))|1,
  422. &(regs)->inbound_queue_port);
  423. spin_unlock_irqrestore(&instance->hba_lock, flags);
  424. }
  425. /**
  426. * megasas_adp_reset_ppc - For controller reset
  427. * @regs: MFI register set
  428. */
  429. static int
  430. megasas_adp_reset_ppc(struct megasas_instance *instance,
  431. struct megasas_register_set __iomem *regs)
  432. {
  433. return 0;
  434. }
  435. /**
  436. * megasas_check_reset_ppc - For controller reset check
  437. * @regs: MFI register set
  438. */
  439. static int
  440. megasas_check_reset_ppc(struct megasas_instance *instance,
  441. struct megasas_register_set __iomem *regs)
  442. {
  443. return 0;
  444. }
  445. static struct megasas_instance_template megasas_instance_template_ppc = {
  446. .fire_cmd = megasas_fire_cmd_ppc,
  447. .enable_intr = megasas_enable_intr_ppc,
  448. .disable_intr = megasas_disable_intr_ppc,
  449. .clear_intr = megasas_clear_intr_ppc,
  450. .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
  451. .adp_reset = megasas_adp_reset_ppc,
  452. .check_reset = megasas_check_reset_ppc,
  453. .service_isr = megasas_isr,
  454. .tasklet = megasas_complete_cmd_dpc,
  455. .init_adapter = megasas_init_adapter_mfi,
  456. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  457. .issue_dcmd = megasas_issue_dcmd,
  458. };
  459. /**
  460. * megasas_enable_intr_skinny - Enables interrupts
  461. * @regs: MFI register set
  462. */
  463. static inline void
  464. megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
  465. {
  466. writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
  467. writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  468. /* Dummy readl to force pci flush */
  469. readl(&regs->outbound_intr_mask);
  470. }
  471. /**
  472. * megasas_disable_intr_skinny - Disables interrupt
  473. * @regs: MFI register set
  474. */
  475. static inline void
  476. megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
  477. {
  478. u32 mask = 0xFFFFFFFF;
  479. writel(mask, &regs->outbound_intr_mask);
  480. /* Dummy readl to force pci flush */
  481. readl(&regs->outbound_intr_mask);
  482. }
  483. /**
  484. * megasas_read_fw_status_reg_skinny - returns the current FW status value
  485. * @regs: MFI register set
  486. */
  487. static u32
  488. megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
  489. {
  490. return readl(&(regs)->outbound_scratch_pad);
  491. }
  492. /**
  493. * megasas_clear_interrupt_skinny - Check & clear interrupt
  494. * @regs: MFI register set
  495. */
  496. static int
  497. megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
  498. {
  499. u32 status;
  500. u32 mfiStatus = 0;
  501. /*
  502. * Check if it is our interrupt
  503. */
  504. status = readl(&regs->outbound_intr_status);
  505. if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
  506. return 0;
  507. }
  508. /*
  509. * Check if it is our interrupt
  510. */
  511. if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
  512. MFI_STATE_FAULT) {
  513. mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  514. } else
  515. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  516. /*
  517. * Clear the interrupt by writing back the same value
  518. */
  519. writel(status, &regs->outbound_intr_status);
  520. /*
  521. * dummy read to flush PCI
  522. */
  523. readl(&regs->outbound_intr_status);
  524. return mfiStatus;
  525. }
  526. /**
  527. * megasas_fire_cmd_skinny - Sends command to the FW
  528. * @frame_phys_addr : Physical address of cmd
  529. * @frame_count : Number of frames for the command
  530. * @regs : MFI register set
  531. */
  532. static inline void
  533. megasas_fire_cmd_skinny(struct megasas_instance *instance,
  534. dma_addr_t frame_phys_addr,
  535. u32 frame_count,
  536. struct megasas_register_set __iomem *regs)
  537. {
  538. unsigned long flags;
  539. spin_lock_irqsave(&instance->hba_lock, flags);
  540. writel(0, &(regs)->inbound_high_queue_port);
  541. writel((frame_phys_addr | (frame_count<<1))|1,
  542. &(regs)->inbound_low_queue_port);
  543. spin_unlock_irqrestore(&instance->hba_lock, flags);
  544. }
  545. /**
  546. * megasas_check_reset_skinny - For controller reset check
  547. * @regs: MFI register set
  548. */
  549. static int
  550. megasas_check_reset_skinny(struct megasas_instance *instance,
  551. struct megasas_register_set __iomem *regs)
  552. {
  553. return 0;
  554. }
  555. static struct megasas_instance_template megasas_instance_template_skinny = {
  556. .fire_cmd = megasas_fire_cmd_skinny,
  557. .enable_intr = megasas_enable_intr_skinny,
  558. .disable_intr = megasas_disable_intr_skinny,
  559. .clear_intr = megasas_clear_intr_skinny,
  560. .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
  561. .adp_reset = megasas_adp_reset_gen2,
  562. .check_reset = megasas_check_reset_skinny,
  563. .service_isr = megasas_isr,
  564. .tasklet = megasas_complete_cmd_dpc,
  565. .init_adapter = megasas_init_adapter_mfi,
  566. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  567. .issue_dcmd = megasas_issue_dcmd,
  568. };
  569. /**
  570. * The following functions are defined for gen2 (deviceid : 0x78 0x79)
  571. * controllers
  572. */
  573. /**
  574. * megasas_enable_intr_gen2 - Enables interrupts
  575. * @regs: MFI register set
  576. */
  577. static inline void
  578. megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
  579. {
  580. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  581. /* write ~0x00000005 (4 & 1) to the intr mask*/
  582. writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  583. /* Dummy readl to force pci flush */
  584. readl(&regs->outbound_intr_mask);
  585. }
  586. /**
  587. * megasas_disable_intr_gen2 - Disables interrupt
  588. * @regs: MFI register set
  589. */
  590. static inline void
  591. megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
  592. {
  593. u32 mask = 0xFFFFFFFF;
  594. writel(mask, &regs->outbound_intr_mask);
  595. /* Dummy readl to force pci flush */
  596. readl(&regs->outbound_intr_mask);
  597. }
  598. /**
  599. * megasas_read_fw_status_reg_gen2 - returns the current FW status value
  600. * @regs: MFI register set
  601. */
  602. static u32
  603. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
  604. {
  605. return readl(&(regs)->outbound_scratch_pad);
  606. }
  607. /**
  608. * megasas_clear_interrupt_gen2 - Check & clear interrupt
  609. * @regs: MFI register set
  610. */
  611. static int
  612. megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
  613. {
  614. u32 status;
  615. u32 mfiStatus = 0;
  616. /*
  617. * Check if it is our interrupt
  618. */
  619. status = readl(&regs->outbound_intr_status);
  620. if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
  621. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  622. }
  623. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
  624. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  625. }
  626. /*
  627. * Clear the interrupt by writing back the same value
  628. */
  629. if (mfiStatus)
  630. writel(status, &regs->outbound_doorbell_clear);
  631. /* Dummy readl to force pci flush */
  632. readl(&regs->outbound_intr_status);
  633. return mfiStatus;
  634. }
  635. /**
  636. * megasas_fire_cmd_gen2 - Sends command to the FW
  637. * @frame_phys_addr : Physical address of cmd
  638. * @frame_count : Number of frames for the command
  639. * @regs : MFI register set
  640. */
  641. static inline void
  642. megasas_fire_cmd_gen2(struct megasas_instance *instance,
  643. dma_addr_t frame_phys_addr,
  644. u32 frame_count,
  645. struct megasas_register_set __iomem *regs)
  646. {
  647. unsigned long flags;
  648. spin_lock_irqsave(&instance->hba_lock, flags);
  649. writel((frame_phys_addr | (frame_count<<1))|1,
  650. &(regs)->inbound_queue_port);
  651. spin_unlock_irqrestore(&instance->hba_lock, flags);
  652. }
  653. /**
  654. * megasas_adp_reset_gen2 - For controller reset
  655. * @regs: MFI register set
  656. */
  657. static int
  658. megasas_adp_reset_gen2(struct megasas_instance *instance,
  659. struct megasas_register_set __iomem *reg_set)
  660. {
  661. u32 retry = 0 ;
  662. u32 HostDiag;
  663. u32 *seq_offset = &reg_set->seq_offset;
  664. u32 *hostdiag_offset = &reg_set->host_diag;
  665. if (instance->instancet == &megasas_instance_template_skinny) {
  666. seq_offset = &reg_set->fusion_seq_offset;
  667. hostdiag_offset = &reg_set->fusion_host_diag;
  668. }
  669. writel(0, seq_offset);
  670. writel(4, seq_offset);
  671. writel(0xb, seq_offset);
  672. writel(2, seq_offset);
  673. writel(7, seq_offset);
  674. writel(0xd, seq_offset);
  675. msleep(1000);
  676. HostDiag = (u32)readl(hostdiag_offset);
  677. while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
  678. msleep(100);
  679. HostDiag = (u32)readl(hostdiag_offset);
  680. printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
  681. retry, HostDiag);
  682. if (retry++ >= 100)
  683. return 1;
  684. }
  685. printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
  686. writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
  687. ssleep(10);
  688. HostDiag = (u32)readl(hostdiag_offset);
  689. while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
  690. msleep(100);
  691. HostDiag = (u32)readl(hostdiag_offset);
  692. printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
  693. retry, HostDiag);
  694. if (retry++ >= 1000)
  695. return 1;
  696. }
  697. return 0;
  698. }
  699. /**
  700. * megasas_check_reset_gen2 - For controller reset check
  701. * @regs: MFI register set
  702. */
  703. static int
  704. megasas_check_reset_gen2(struct megasas_instance *instance,
  705. struct megasas_register_set __iomem *regs)
  706. {
  707. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  708. return 1;
  709. }
  710. return 0;
  711. }
  712. static struct megasas_instance_template megasas_instance_template_gen2 = {
  713. .fire_cmd = megasas_fire_cmd_gen2,
  714. .enable_intr = megasas_enable_intr_gen2,
  715. .disable_intr = megasas_disable_intr_gen2,
  716. .clear_intr = megasas_clear_intr_gen2,
  717. .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
  718. .adp_reset = megasas_adp_reset_gen2,
  719. .check_reset = megasas_check_reset_gen2,
  720. .service_isr = megasas_isr,
  721. .tasklet = megasas_complete_cmd_dpc,
  722. .init_adapter = megasas_init_adapter_mfi,
  723. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  724. .issue_dcmd = megasas_issue_dcmd,
  725. };
  726. /**
  727. * This is the end of set of functions & definitions
  728. * specific to gen2 (deviceid : 0x78, 0x79) controllers
  729. */
  730. /*
  731. * Template added for TB (Fusion)
  732. */
  733. extern struct megasas_instance_template megasas_instance_template_fusion;
  734. /**
  735. * megasas_issue_polled - Issues a polling command
  736. * @instance: Adapter soft state
  737. * @cmd: Command packet to be issued
  738. *
  739. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  740. */
  741. int
  742. megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
  743. {
  744. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  745. frame_hdr->cmd_status = 0xFF;
  746. frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
  747. /*
  748. * Issue the frame using inbound queue port
  749. */
  750. instance->instancet->issue_dcmd(instance, cmd);
  751. /*
  752. * Wait for cmd_status to change
  753. */
  754. return wait_and_poll(instance, cmd);
  755. }
  756. /**
  757. * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
  758. * @instance: Adapter soft state
  759. * @cmd: Command to be issued
  760. *
  761. * This function waits on an event for the command to be returned from ISR.
  762. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  763. * Used to issue ioctl commands.
  764. */
  765. static int
  766. megasas_issue_blocked_cmd(struct megasas_instance *instance,
  767. struct megasas_cmd *cmd)
  768. {
  769. cmd->cmd_status = ENODATA;
  770. instance->instancet->issue_dcmd(instance, cmd);
  771. wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
  772. return 0;
  773. }
  774. /**
  775. * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
  776. * @instance: Adapter soft state
  777. * @cmd_to_abort: Previously issued cmd to be aborted
  778. *
  779. * MFI firmware can abort previously issued AEN command (automatic event
  780. * notification). The megasas_issue_blocked_abort_cmd() issues such abort
  781. * cmd and waits for return status.
  782. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  783. */
  784. static int
  785. megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
  786. struct megasas_cmd *cmd_to_abort)
  787. {
  788. struct megasas_cmd *cmd;
  789. struct megasas_abort_frame *abort_fr;
  790. cmd = megasas_get_cmd(instance);
  791. if (!cmd)
  792. return -1;
  793. abort_fr = &cmd->frame->abort;
  794. /*
  795. * Prepare and issue the abort frame
  796. */
  797. abort_fr->cmd = MFI_CMD_ABORT;
  798. abort_fr->cmd_status = 0xFF;
  799. abort_fr->flags = 0;
  800. abort_fr->abort_context = cmd_to_abort->index;
  801. abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
  802. abort_fr->abort_mfi_phys_addr_hi = 0;
  803. cmd->sync_cmd = 1;
  804. cmd->cmd_status = 0xFF;
  805. instance->instancet->issue_dcmd(instance, cmd);
  806. /*
  807. * Wait for this cmd to complete
  808. */
  809. wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
  810. cmd->sync_cmd = 0;
  811. megasas_return_cmd(instance, cmd);
  812. return 0;
  813. }
  814. /**
  815. * megasas_make_sgl32 - Prepares 32-bit SGL
  816. * @instance: Adapter soft state
  817. * @scp: SCSI command from the mid-layer
  818. * @mfi_sgl: SGL to be filled in
  819. *
  820. * If successful, this function returns the number of SG elements. Otherwise,
  821. * it returnes -1.
  822. */
  823. static int
  824. megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
  825. union megasas_sgl *mfi_sgl)
  826. {
  827. int i;
  828. int sge_count;
  829. struct scatterlist *os_sgl;
  830. sge_count = scsi_dma_map(scp);
  831. BUG_ON(sge_count < 0);
  832. if (sge_count) {
  833. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  834. mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
  835. mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
  836. }
  837. }
  838. return sge_count;
  839. }
  840. /**
  841. * megasas_make_sgl64 - Prepares 64-bit SGL
  842. * @instance: Adapter soft state
  843. * @scp: SCSI command from the mid-layer
  844. * @mfi_sgl: SGL to be filled in
  845. *
  846. * If successful, this function returns the number of SG elements. Otherwise,
  847. * it returnes -1.
  848. */
  849. static int
  850. megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
  851. union megasas_sgl *mfi_sgl)
  852. {
  853. int i;
  854. int sge_count;
  855. struct scatterlist *os_sgl;
  856. sge_count = scsi_dma_map(scp);
  857. BUG_ON(sge_count < 0);
  858. if (sge_count) {
  859. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  860. mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
  861. mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
  862. }
  863. }
  864. return sge_count;
  865. }
  866. /**
  867. * megasas_make_sgl_skinny - Prepares IEEE SGL
  868. * @instance: Adapter soft state
  869. * @scp: SCSI command from the mid-layer
  870. * @mfi_sgl: SGL to be filled in
  871. *
  872. * If successful, this function returns the number of SG elements. Otherwise,
  873. * it returnes -1.
  874. */
  875. static int
  876. megasas_make_sgl_skinny(struct megasas_instance *instance,
  877. struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
  878. {
  879. int i;
  880. int sge_count;
  881. struct scatterlist *os_sgl;
  882. sge_count = scsi_dma_map(scp);
  883. if (sge_count) {
  884. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  885. mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
  886. mfi_sgl->sge_skinny[i].phys_addr =
  887. sg_dma_address(os_sgl);
  888. mfi_sgl->sge_skinny[i].flag = 0;
  889. }
  890. }
  891. return sge_count;
  892. }
  893. /**
  894. * megasas_get_frame_count - Computes the number of frames
  895. * @frame_type : type of frame- io or pthru frame
  896. * @sge_count : number of sg elements
  897. *
  898. * Returns the number of frames required for numnber of sge's (sge_count)
  899. */
  900. static u32 megasas_get_frame_count(struct megasas_instance *instance,
  901. u8 sge_count, u8 frame_type)
  902. {
  903. int num_cnt;
  904. int sge_bytes;
  905. u32 sge_sz;
  906. u32 frame_count=0;
  907. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  908. sizeof(struct megasas_sge32);
  909. if (instance->flag_ieee) {
  910. sge_sz = sizeof(struct megasas_sge_skinny);
  911. }
  912. /*
  913. * Main frame can contain 2 SGEs for 64-bit SGLs and
  914. * 3 SGEs for 32-bit SGLs for ldio &
  915. * 1 SGEs for 64-bit SGLs and
  916. * 2 SGEs for 32-bit SGLs for pthru frame
  917. */
  918. if (unlikely(frame_type == PTHRU_FRAME)) {
  919. if (instance->flag_ieee == 1) {
  920. num_cnt = sge_count - 1;
  921. } else if (IS_DMA64)
  922. num_cnt = sge_count - 1;
  923. else
  924. num_cnt = sge_count - 2;
  925. } else {
  926. if (instance->flag_ieee == 1) {
  927. num_cnt = sge_count - 1;
  928. } else if (IS_DMA64)
  929. num_cnt = sge_count - 2;
  930. else
  931. num_cnt = sge_count - 3;
  932. }
  933. if(num_cnt>0){
  934. sge_bytes = sge_sz * num_cnt;
  935. frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  936. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
  937. }
  938. /* Main frame */
  939. frame_count +=1;
  940. if (frame_count > 7)
  941. frame_count = 8;
  942. return frame_count;
  943. }
  944. /**
  945. * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
  946. * @instance: Adapter soft state
  947. * @scp: SCSI command
  948. * @cmd: Command to be prepared in
  949. *
  950. * This function prepares CDB commands. These are typcially pass-through
  951. * commands to the devices.
  952. */
  953. static int
  954. megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
  955. struct megasas_cmd *cmd)
  956. {
  957. u32 is_logical;
  958. u32 device_id;
  959. u16 flags = 0;
  960. struct megasas_pthru_frame *pthru;
  961. is_logical = MEGASAS_IS_LOGICAL(scp);
  962. device_id = MEGASAS_DEV_INDEX(instance, scp);
  963. pthru = (struct megasas_pthru_frame *)cmd->frame;
  964. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  965. flags = MFI_FRAME_DIR_WRITE;
  966. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  967. flags = MFI_FRAME_DIR_READ;
  968. else if (scp->sc_data_direction == PCI_DMA_NONE)
  969. flags = MFI_FRAME_DIR_NONE;
  970. if (instance->flag_ieee == 1) {
  971. flags |= MFI_FRAME_IEEE;
  972. }
  973. /*
  974. * Prepare the DCDB frame
  975. */
  976. pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
  977. pthru->cmd_status = 0x0;
  978. pthru->scsi_status = 0x0;
  979. pthru->target_id = device_id;
  980. pthru->lun = scp->device->lun;
  981. pthru->cdb_len = scp->cmd_len;
  982. pthru->timeout = 0;
  983. pthru->pad_0 = 0;
  984. pthru->flags = flags;
  985. pthru->data_xfer_len = scsi_bufflen(scp);
  986. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  987. /*
  988. * If the command is for the tape device, set the
  989. * pthru timeout to the os layer timeout value.
  990. */
  991. if (scp->device->type == TYPE_TAPE) {
  992. if ((scp->request->timeout / HZ) > 0xFFFF)
  993. pthru->timeout = 0xFFFF;
  994. else
  995. pthru->timeout = scp->request->timeout / HZ;
  996. }
  997. /*
  998. * Construct SGL
  999. */
  1000. if (instance->flag_ieee == 1) {
  1001. pthru->flags |= MFI_FRAME_SGL64;
  1002. pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
  1003. &pthru->sgl);
  1004. } else if (IS_DMA64) {
  1005. pthru->flags |= MFI_FRAME_SGL64;
  1006. pthru->sge_count = megasas_make_sgl64(instance, scp,
  1007. &pthru->sgl);
  1008. } else
  1009. pthru->sge_count = megasas_make_sgl32(instance, scp,
  1010. &pthru->sgl);
  1011. if (pthru->sge_count > instance->max_num_sge) {
  1012. printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
  1013. pthru->sge_count);
  1014. return 0;
  1015. }
  1016. /*
  1017. * Sense info specific
  1018. */
  1019. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  1020. pthru->sense_buf_phys_addr_hi = 0;
  1021. pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  1022. /*
  1023. * Compute the total number of frames this command consumes. FW uses
  1024. * this number to pull sufficient number of frames from host memory.
  1025. */
  1026. cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
  1027. PTHRU_FRAME);
  1028. return cmd->frame_count;
  1029. }
  1030. /**
  1031. * megasas_build_ldio - Prepares IOs to logical devices
  1032. * @instance: Adapter soft state
  1033. * @scp: SCSI command
  1034. * @cmd: Command to be prepared
  1035. *
  1036. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  1037. */
  1038. static int
  1039. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1040. struct megasas_cmd *cmd)
  1041. {
  1042. u32 device_id;
  1043. u8 sc = scp->cmnd[0];
  1044. u16 flags = 0;
  1045. struct megasas_io_frame *ldio;
  1046. device_id = MEGASAS_DEV_INDEX(instance, scp);
  1047. ldio = (struct megasas_io_frame *)cmd->frame;
  1048. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1049. flags = MFI_FRAME_DIR_WRITE;
  1050. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1051. flags = MFI_FRAME_DIR_READ;
  1052. if (instance->flag_ieee == 1) {
  1053. flags |= MFI_FRAME_IEEE;
  1054. }
  1055. /*
  1056. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  1057. */
  1058. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  1059. ldio->cmd_status = 0x0;
  1060. ldio->scsi_status = 0x0;
  1061. ldio->target_id = device_id;
  1062. ldio->timeout = 0;
  1063. ldio->reserved_0 = 0;
  1064. ldio->pad_0 = 0;
  1065. ldio->flags = flags;
  1066. ldio->start_lba_hi = 0;
  1067. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  1068. /*
  1069. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1070. */
  1071. if (scp->cmd_len == 6) {
  1072. ldio->lba_count = (u32) scp->cmnd[4];
  1073. ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  1074. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  1075. ldio->start_lba_lo &= 0x1FFFFF;
  1076. }
  1077. /*
  1078. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1079. */
  1080. else if (scp->cmd_len == 10) {
  1081. ldio->lba_count = (u32) scp->cmnd[8] |
  1082. ((u32) scp->cmnd[7] << 8);
  1083. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1084. ((u32) scp->cmnd[3] << 16) |
  1085. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1086. }
  1087. /*
  1088. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1089. */
  1090. else if (scp->cmd_len == 12) {
  1091. ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
  1092. ((u32) scp->cmnd[7] << 16) |
  1093. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1094. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1095. ((u32) scp->cmnd[3] << 16) |
  1096. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1097. }
  1098. /*
  1099. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1100. */
  1101. else if (scp->cmd_len == 16) {
  1102. ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
  1103. ((u32) scp->cmnd[11] << 16) |
  1104. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  1105. ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  1106. ((u32) scp->cmnd[7] << 16) |
  1107. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1108. ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  1109. ((u32) scp->cmnd[3] << 16) |
  1110. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1111. }
  1112. /*
  1113. * Construct SGL
  1114. */
  1115. if (instance->flag_ieee) {
  1116. ldio->flags |= MFI_FRAME_SGL64;
  1117. ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
  1118. &ldio->sgl);
  1119. } else if (IS_DMA64) {
  1120. ldio->flags |= MFI_FRAME_SGL64;
  1121. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  1122. } else
  1123. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  1124. if (ldio->sge_count > instance->max_num_sge) {
  1125. printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
  1126. ldio->sge_count);
  1127. return 0;
  1128. }
  1129. /*
  1130. * Sense info specific
  1131. */
  1132. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  1133. ldio->sense_buf_phys_addr_hi = 0;
  1134. ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  1135. /*
  1136. * Compute the total number of frames this command consumes. FW uses
  1137. * this number to pull sufficient number of frames from host memory.
  1138. */
  1139. cmd->frame_count = megasas_get_frame_count(instance,
  1140. ldio->sge_count, IO_FRAME);
  1141. return cmd->frame_count;
  1142. }
  1143. /**
  1144. * megasas_is_ldio - Checks if the cmd is for logical drive
  1145. * @scmd: SCSI command
  1146. *
  1147. * Called by megasas_queue_command to find out if the command to be queued
  1148. * is a logical drive command
  1149. */
  1150. inline int megasas_is_ldio(struct scsi_cmnd *cmd)
  1151. {
  1152. if (!MEGASAS_IS_LOGICAL(cmd))
  1153. return 0;
  1154. switch (cmd->cmnd[0]) {
  1155. case READ_10:
  1156. case WRITE_10:
  1157. case READ_12:
  1158. case WRITE_12:
  1159. case READ_6:
  1160. case WRITE_6:
  1161. case READ_16:
  1162. case WRITE_16:
  1163. return 1;
  1164. default:
  1165. return 0;
  1166. }
  1167. }
  1168. /**
  1169. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  1170. * in FW
  1171. * @instance: Adapter soft state
  1172. */
  1173. static inline void
  1174. megasas_dump_pending_frames(struct megasas_instance *instance)
  1175. {
  1176. struct megasas_cmd *cmd;
  1177. int i,n;
  1178. union megasas_sgl *mfi_sgl;
  1179. struct megasas_io_frame *ldio;
  1180. struct megasas_pthru_frame *pthru;
  1181. u32 sgcount;
  1182. u32 max_cmd = instance->max_fw_cmds;
  1183. printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  1184. printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  1185. if (IS_DMA64)
  1186. printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  1187. else
  1188. printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  1189. printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  1190. for (i = 0; i < max_cmd; i++) {
  1191. cmd = instance->cmd_list[i];
  1192. if(!cmd->scmd)
  1193. continue;
  1194. printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  1195. if (megasas_is_ldio(cmd->scmd)){
  1196. ldio = (struct megasas_io_frame *)cmd->frame;
  1197. mfi_sgl = &ldio->sgl;
  1198. sgcount = ldio->sge_count;
  1199. printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount);
  1200. }
  1201. else {
  1202. pthru = (struct megasas_pthru_frame *) cmd->frame;
  1203. mfi_sgl = &pthru->sgl;
  1204. sgcount = pthru->sge_count;
  1205. printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount);
  1206. }
  1207. if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
  1208. for (n = 0; n < sgcount; n++){
  1209. if (IS_DMA64)
  1210. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ;
  1211. else
  1212. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
  1213. }
  1214. }
  1215. printk(KERN_ERR "\n");
  1216. } /*for max_cmd*/
  1217. printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  1218. for (i = 0; i < max_cmd; i++) {
  1219. cmd = instance->cmd_list[i];
  1220. if(cmd->sync_cmd == 1){
  1221. printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  1222. }
  1223. }
  1224. printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
  1225. }
  1226. u32
  1227. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  1228. struct scsi_cmnd *scmd)
  1229. {
  1230. struct megasas_cmd *cmd;
  1231. u32 frame_count;
  1232. cmd = megasas_get_cmd(instance);
  1233. if (!cmd)
  1234. return SCSI_MLQUEUE_HOST_BUSY;
  1235. /*
  1236. * Logical drive command
  1237. */
  1238. if (megasas_is_ldio(scmd))
  1239. frame_count = megasas_build_ldio(instance, scmd, cmd);
  1240. else
  1241. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  1242. if (!frame_count)
  1243. goto out_return_cmd;
  1244. cmd->scmd = scmd;
  1245. scmd->SCp.ptr = (char *)cmd;
  1246. /*
  1247. * Issue the command to the FW
  1248. */
  1249. atomic_inc(&instance->fw_outstanding);
  1250. instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
  1251. cmd->frame_count-1, instance->reg_set);
  1252. /*
  1253. * Check if we have pend cmds to be completed
  1254. */
  1255. if (poll_mode_io && atomic_read(&instance->fw_outstanding))
  1256. tasklet_schedule(&instance->isr_tasklet);
  1257. return 0;
  1258. out_return_cmd:
  1259. megasas_return_cmd(instance, cmd);
  1260. return 1;
  1261. }
  1262. /**
  1263. * megasas_queue_command - Queue entry point
  1264. * @scmd: SCSI command to be queued
  1265. * @done: Callback entry point
  1266. */
  1267. static int
  1268. megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
  1269. {
  1270. struct megasas_instance *instance;
  1271. unsigned long flags;
  1272. instance = (struct megasas_instance *)
  1273. scmd->device->host->hostdata;
  1274. if (instance->issuepend_done == 0)
  1275. return SCSI_MLQUEUE_HOST_BUSY;
  1276. spin_lock_irqsave(&instance->hba_lock, flags);
  1277. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1278. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1279. return SCSI_MLQUEUE_HOST_BUSY;
  1280. }
  1281. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1282. scmd->scsi_done = done;
  1283. scmd->result = 0;
  1284. if (MEGASAS_IS_LOGICAL(scmd) &&
  1285. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  1286. scmd->result = DID_BAD_TARGET << 16;
  1287. goto out_done;
  1288. }
  1289. switch (scmd->cmnd[0]) {
  1290. case SYNCHRONIZE_CACHE:
  1291. /*
  1292. * FW takes care of flush cache on its own
  1293. * No need to send it down
  1294. */
  1295. scmd->result = DID_OK << 16;
  1296. goto out_done;
  1297. default:
  1298. break;
  1299. }
  1300. if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
  1301. printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
  1302. return SCSI_MLQUEUE_HOST_BUSY;
  1303. }
  1304. return 0;
  1305. out_done:
  1306. done(scmd);
  1307. return 0;
  1308. }
  1309. static DEF_SCSI_QCMD(megasas_queue_command)
  1310. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1311. {
  1312. int i;
  1313. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1314. if ((megasas_mgmt_info.instance[i]) &&
  1315. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1316. return megasas_mgmt_info.instance[i];
  1317. }
  1318. return NULL;
  1319. }
  1320. static int megasas_slave_configure(struct scsi_device *sdev)
  1321. {
  1322. u16 pd_index = 0;
  1323. struct megasas_instance *instance ;
  1324. instance = megasas_lookup_instance(sdev->host->host_no);
  1325. /*
  1326. * Don't export physical disk devices to the disk driver.
  1327. *
  1328. * FIXME: Currently we don't export them to the midlayer at all.
  1329. * That will be fixed once LSI engineers have audited the
  1330. * firmware for possible issues.
  1331. */
  1332. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1333. sdev->type == TYPE_DISK) {
  1334. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1335. sdev->id;
  1336. if (instance->pd_list[pd_index].driveState ==
  1337. MR_PD_STATE_SYSTEM) {
  1338. blk_queue_rq_timeout(sdev->request_queue,
  1339. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1340. return 0;
  1341. }
  1342. return -ENXIO;
  1343. }
  1344. /*
  1345. * The RAID firmware may require extended timeouts.
  1346. */
  1347. blk_queue_rq_timeout(sdev->request_queue,
  1348. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1349. return 0;
  1350. }
  1351. static int megasas_slave_alloc(struct scsi_device *sdev)
  1352. {
  1353. u16 pd_index = 0;
  1354. struct megasas_instance *instance ;
  1355. instance = megasas_lookup_instance(sdev->host->host_no);
  1356. if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
  1357. (sdev->type == TYPE_DISK)) {
  1358. /*
  1359. * Open the OS scan to the SYSTEM PD
  1360. */
  1361. pd_index =
  1362. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1363. sdev->id;
  1364. if ((instance->pd_list[pd_index].driveState ==
  1365. MR_PD_STATE_SYSTEM) &&
  1366. (instance->pd_list[pd_index].driveType ==
  1367. TYPE_DISK)) {
  1368. return 0;
  1369. }
  1370. return -ENXIO;
  1371. }
  1372. return 0;
  1373. }
  1374. void megaraid_sas_kill_hba(struct megasas_instance *instance)
  1375. {
  1376. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1377. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  1378. (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)) {
  1379. writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
  1380. } else {
  1381. writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
  1382. }
  1383. }
  1384. /**
  1385. * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
  1386. * restored to max value
  1387. * @instance: Adapter soft state
  1388. *
  1389. */
  1390. void
  1391. megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
  1392. {
  1393. unsigned long flags;
  1394. if (instance->flag & MEGASAS_FW_BUSY
  1395. && time_after(jiffies, instance->last_time + 5 * HZ)
  1396. && atomic_read(&instance->fw_outstanding) < 17) {
  1397. spin_lock_irqsave(instance->host->host_lock, flags);
  1398. instance->flag &= ~MEGASAS_FW_BUSY;
  1399. if ((instance->pdev->device ==
  1400. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1401. (instance->pdev->device ==
  1402. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1403. instance->host->can_queue =
  1404. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  1405. } else
  1406. instance->host->can_queue =
  1407. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1408. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1409. }
  1410. }
  1411. /**
  1412. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1413. * @instance_addr: Address of adapter soft state
  1414. *
  1415. * Tasklet to complete cmds
  1416. */
  1417. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1418. {
  1419. u32 producer;
  1420. u32 consumer;
  1421. u32 context;
  1422. struct megasas_cmd *cmd;
  1423. struct megasas_instance *instance =
  1424. (struct megasas_instance *)instance_addr;
  1425. unsigned long flags;
  1426. /* If we have already declared adapter dead, donot complete cmds */
  1427. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
  1428. return;
  1429. spin_lock_irqsave(&instance->completion_lock, flags);
  1430. producer = *instance->producer;
  1431. consumer = *instance->consumer;
  1432. while (consumer != producer) {
  1433. context = instance->reply_queue[consumer];
  1434. if (context >= instance->max_fw_cmds) {
  1435. printk(KERN_ERR "Unexpected context value %x\n",
  1436. context);
  1437. BUG();
  1438. }
  1439. cmd = instance->cmd_list[context];
  1440. megasas_complete_cmd(instance, cmd, DID_OK);
  1441. consumer++;
  1442. if (consumer == (instance->max_fw_cmds + 1)) {
  1443. consumer = 0;
  1444. }
  1445. }
  1446. *instance->consumer = producer;
  1447. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1448. /*
  1449. * Check if we can restore can_queue
  1450. */
  1451. megasas_check_and_restore_queue_depth(instance);
  1452. }
  1453. static void
  1454. megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
  1455. static void
  1456. process_fw_state_change_wq(struct work_struct *work);
  1457. void megasas_do_ocr(struct megasas_instance *instance)
  1458. {
  1459. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  1460. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  1461. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  1462. *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
  1463. }
  1464. instance->instancet->disable_intr(instance->reg_set);
  1465. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  1466. instance->issuepend_done = 0;
  1467. atomic_set(&instance->fw_outstanding, 0);
  1468. megasas_internal_reset_defer_cmds(instance);
  1469. process_fw_state_change_wq(&instance->work_init);
  1470. }
  1471. /**
  1472. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  1473. * @instance: Adapter soft state
  1474. *
  1475. * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
  1476. * complete all its outstanding commands. Returns error if one or more IOs
  1477. * are pending after this time period. It also marks the controller dead.
  1478. */
  1479. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  1480. {
  1481. int i;
  1482. u32 reset_index;
  1483. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  1484. u8 adprecovery;
  1485. unsigned long flags;
  1486. struct list_head clist_local;
  1487. struct megasas_cmd *reset_cmd;
  1488. u32 fw_state;
  1489. u8 kill_adapter_flag;
  1490. spin_lock_irqsave(&instance->hba_lock, flags);
  1491. adprecovery = instance->adprecovery;
  1492. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1493. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1494. INIT_LIST_HEAD(&clist_local);
  1495. spin_lock_irqsave(&instance->hba_lock, flags);
  1496. list_splice_init(&instance->internal_reset_pending_q,
  1497. &clist_local);
  1498. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1499. printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
  1500. for (i = 0; i < wait_time; i++) {
  1501. msleep(1000);
  1502. spin_lock_irqsave(&instance->hba_lock, flags);
  1503. adprecovery = instance->adprecovery;
  1504. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1505. if (adprecovery == MEGASAS_HBA_OPERATIONAL)
  1506. break;
  1507. }
  1508. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1509. printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
  1510. spin_lock_irqsave(&instance->hba_lock, flags);
  1511. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1512. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1513. return FAILED;
  1514. }
  1515. reset_index = 0;
  1516. while (!list_empty(&clist_local)) {
  1517. reset_cmd = list_entry((&clist_local)->next,
  1518. struct megasas_cmd, list);
  1519. list_del_init(&reset_cmd->list);
  1520. if (reset_cmd->scmd) {
  1521. reset_cmd->scmd->result = DID_RESET << 16;
  1522. printk(KERN_NOTICE "%d:%p reset [%02x], %#lx\n",
  1523. reset_index, reset_cmd,
  1524. reset_cmd->scmd->cmnd[0],
  1525. reset_cmd->scmd->serial_number);
  1526. reset_cmd->scmd->scsi_done(reset_cmd->scmd);
  1527. megasas_return_cmd(instance, reset_cmd);
  1528. } else if (reset_cmd->sync_cmd) {
  1529. printk(KERN_NOTICE "megasas:%p synch cmds"
  1530. "reset queue\n",
  1531. reset_cmd);
  1532. reset_cmd->cmd_status = ENODATA;
  1533. instance->instancet->fire_cmd(instance,
  1534. reset_cmd->frame_phys_addr,
  1535. 0, instance->reg_set);
  1536. } else {
  1537. printk(KERN_NOTICE "megasas: %p unexpected"
  1538. "cmds lst\n",
  1539. reset_cmd);
  1540. }
  1541. reset_index++;
  1542. }
  1543. return SUCCESS;
  1544. }
  1545. for (i = 0; i < wait_time; i++) {
  1546. int outstanding = atomic_read(&instance->fw_outstanding);
  1547. if (!outstanding)
  1548. break;
  1549. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  1550. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  1551. "commands to complete\n",i,outstanding);
  1552. /*
  1553. * Call cmd completion routine. Cmd to be
  1554. * be completed directly without depending on isr.
  1555. */
  1556. megasas_complete_cmd_dpc((unsigned long)instance);
  1557. }
  1558. msleep(1000);
  1559. }
  1560. i = 0;
  1561. kill_adapter_flag = 0;
  1562. do {
  1563. fw_state = instance->instancet->read_fw_status_reg(
  1564. instance->reg_set) & MFI_STATE_MASK;
  1565. if ((fw_state == MFI_STATE_FAULT) &&
  1566. (instance->disableOnlineCtrlReset == 0)) {
  1567. if (i == 3) {
  1568. kill_adapter_flag = 2;
  1569. break;
  1570. }
  1571. megasas_do_ocr(instance);
  1572. kill_adapter_flag = 1;
  1573. /* wait for 1 secs to let FW finish the pending cmds */
  1574. msleep(1000);
  1575. }
  1576. i++;
  1577. } while (i <= 3);
  1578. if (atomic_read(&instance->fw_outstanding) &&
  1579. !kill_adapter_flag) {
  1580. if (instance->disableOnlineCtrlReset == 0) {
  1581. megasas_do_ocr(instance);
  1582. /* wait for 5 secs to let FW finish the pending cmds */
  1583. for (i = 0; i < wait_time; i++) {
  1584. int outstanding =
  1585. atomic_read(&instance->fw_outstanding);
  1586. if (!outstanding)
  1587. return SUCCESS;
  1588. msleep(1000);
  1589. }
  1590. }
  1591. }
  1592. if (atomic_read(&instance->fw_outstanding) ||
  1593. (kill_adapter_flag == 2)) {
  1594. printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
  1595. /*
  1596. * Send signal to FW to stop processing any pending cmds.
  1597. * The controller will be taken offline by the OS now.
  1598. */
  1599. if ((instance->pdev->device ==
  1600. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1601. (instance->pdev->device ==
  1602. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1603. writel(MFI_STOP_ADP,
  1604. &instance->reg_set->doorbell);
  1605. } else {
  1606. writel(MFI_STOP_ADP,
  1607. &instance->reg_set->inbound_doorbell);
  1608. }
  1609. megasas_dump_pending_frames(instance);
  1610. spin_lock_irqsave(&instance->hba_lock, flags);
  1611. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1612. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1613. return FAILED;
  1614. }
  1615. printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
  1616. return SUCCESS;
  1617. }
  1618. /**
  1619. * megasas_generic_reset - Generic reset routine
  1620. * @scmd: Mid-layer SCSI command
  1621. *
  1622. * This routine implements a generic reset handler for device, bus and host
  1623. * reset requests. Device, bus and host specific reset handlers can use this
  1624. * function after they do their specific tasks.
  1625. */
  1626. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  1627. {
  1628. int ret_val;
  1629. struct megasas_instance *instance;
  1630. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1631. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
  1632. scmd->serial_number, scmd->cmnd[0], scmd->retries);
  1633. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  1634. printk(KERN_ERR "megasas: cannot recover from previous reset "
  1635. "failures\n");
  1636. return FAILED;
  1637. }
  1638. ret_val = megasas_wait_for_outstanding(instance);
  1639. if (ret_val == SUCCESS)
  1640. printk(KERN_NOTICE "megasas: reset successful \n");
  1641. else
  1642. printk(KERN_ERR "megasas: failed to do reset\n");
  1643. return ret_val;
  1644. }
  1645. /**
  1646. * megasas_reset_timer - quiesce the adapter if required
  1647. * @scmd: scsi cmnd
  1648. *
  1649. * Sets the FW busy flag and reduces the host->can_queue if the
  1650. * cmd has not been completed within the timeout period.
  1651. */
  1652. static enum
  1653. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  1654. {
  1655. struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
  1656. struct megasas_instance *instance;
  1657. unsigned long flags;
  1658. if (time_after(jiffies, scmd->jiffies_at_alloc +
  1659. (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
  1660. return BLK_EH_NOT_HANDLED;
  1661. }
  1662. instance = cmd->instance;
  1663. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  1664. /* FW is busy, throttle IO */
  1665. spin_lock_irqsave(instance->host->host_lock, flags);
  1666. instance->host->can_queue = 16;
  1667. instance->last_time = jiffies;
  1668. instance->flag |= MEGASAS_FW_BUSY;
  1669. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1670. }
  1671. return BLK_EH_RESET_TIMER;
  1672. }
  1673. /**
  1674. * megasas_reset_device - Device reset handler entry point
  1675. */
  1676. static int megasas_reset_device(struct scsi_cmnd *scmd)
  1677. {
  1678. int ret;
  1679. /*
  1680. * First wait for all commands to complete
  1681. */
  1682. ret = megasas_generic_reset(scmd);
  1683. return ret;
  1684. }
  1685. /**
  1686. * megasas_reset_bus_host - Bus & host reset handler entry point
  1687. */
  1688. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  1689. {
  1690. int ret;
  1691. struct megasas_instance *instance;
  1692. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1693. /*
  1694. * First wait for all commands to complete
  1695. */
  1696. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
  1697. ret = megasas_reset_fusion(scmd->device->host);
  1698. else
  1699. ret = megasas_generic_reset(scmd);
  1700. return ret;
  1701. }
  1702. /**
  1703. * megasas_bios_param - Returns disk geometry for a disk
  1704. * @sdev: device handle
  1705. * @bdev: block device
  1706. * @capacity: drive capacity
  1707. * @geom: geometry parameters
  1708. */
  1709. static int
  1710. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1711. sector_t capacity, int geom[])
  1712. {
  1713. int heads;
  1714. int sectors;
  1715. sector_t cylinders;
  1716. unsigned long tmp;
  1717. /* Default heads (64) & sectors (32) */
  1718. heads = 64;
  1719. sectors = 32;
  1720. tmp = heads * sectors;
  1721. cylinders = capacity;
  1722. sector_div(cylinders, tmp);
  1723. /*
  1724. * Handle extended translation size for logical drives > 1Gb
  1725. */
  1726. if (capacity >= 0x200000) {
  1727. heads = 255;
  1728. sectors = 63;
  1729. tmp = heads*sectors;
  1730. cylinders = capacity;
  1731. sector_div(cylinders, tmp);
  1732. }
  1733. geom[0] = heads;
  1734. geom[1] = sectors;
  1735. geom[2] = cylinders;
  1736. return 0;
  1737. }
  1738. static void megasas_aen_polling(struct work_struct *work);
  1739. /**
  1740. * megasas_service_aen - Processes an event notification
  1741. * @instance: Adapter soft state
  1742. * @cmd: AEN command completed by the ISR
  1743. *
  1744. * For AEN, driver sends a command down to FW that is held by the FW till an
  1745. * event occurs. When an event of interest occurs, FW completes the command
  1746. * that it was previously holding.
  1747. *
  1748. * This routines sends SIGIO signal to processes that have registered with the
  1749. * driver for AEN.
  1750. */
  1751. static void
  1752. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1753. {
  1754. unsigned long flags;
  1755. /*
  1756. * Don't signal app if it is just an aborted previously registered aen
  1757. */
  1758. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  1759. spin_lock_irqsave(&poll_aen_lock, flags);
  1760. megasas_poll_wait_aen = 1;
  1761. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1762. wake_up(&megasas_poll_wait);
  1763. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  1764. }
  1765. else
  1766. cmd->abort_aen = 0;
  1767. instance->aen_cmd = NULL;
  1768. megasas_return_cmd(instance, cmd);
  1769. if ((instance->unload == 0) &&
  1770. ((instance->issuepend_done == 1))) {
  1771. struct megasas_aen_event *ev;
  1772. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  1773. if (!ev) {
  1774. printk(KERN_ERR "megasas_service_aen: out of memory\n");
  1775. } else {
  1776. ev->instance = instance;
  1777. instance->ev = ev;
  1778. INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
  1779. schedule_delayed_work(
  1780. (struct delayed_work *)&ev->hotplug_work, 0);
  1781. }
  1782. }
  1783. }
  1784. /*
  1785. * Scsi host template for megaraid_sas driver
  1786. */
  1787. static struct scsi_host_template megasas_template = {
  1788. .module = THIS_MODULE,
  1789. .name = "LSI SAS based MegaRAID driver",
  1790. .proc_name = "megaraid_sas",
  1791. .slave_configure = megasas_slave_configure,
  1792. .slave_alloc = megasas_slave_alloc,
  1793. .queuecommand = megasas_queue_command,
  1794. .eh_device_reset_handler = megasas_reset_device,
  1795. .eh_bus_reset_handler = megasas_reset_bus_host,
  1796. .eh_host_reset_handler = megasas_reset_bus_host,
  1797. .eh_timed_out = megasas_reset_timer,
  1798. .bios_param = megasas_bios_param,
  1799. .use_clustering = ENABLE_CLUSTERING,
  1800. };
  1801. /**
  1802. * megasas_complete_int_cmd - Completes an internal command
  1803. * @instance: Adapter soft state
  1804. * @cmd: Command to be completed
  1805. *
  1806. * The megasas_issue_blocked_cmd() function waits for a command to complete
  1807. * after it issues a command. This function wakes up that waiting routine by
  1808. * calling wake_up() on the wait queue.
  1809. */
  1810. static void
  1811. megasas_complete_int_cmd(struct megasas_instance *instance,
  1812. struct megasas_cmd *cmd)
  1813. {
  1814. cmd->cmd_status = cmd->frame->io.cmd_status;
  1815. if (cmd->cmd_status == ENODATA) {
  1816. cmd->cmd_status = 0;
  1817. }
  1818. wake_up(&instance->int_cmd_wait_q);
  1819. }
  1820. /**
  1821. * megasas_complete_abort - Completes aborting a command
  1822. * @instance: Adapter soft state
  1823. * @cmd: Cmd that was issued to abort another cmd
  1824. *
  1825. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  1826. * after it issues an abort on a previously issued command. This function
  1827. * wakes up all functions waiting on the same wait queue.
  1828. */
  1829. static void
  1830. megasas_complete_abort(struct megasas_instance *instance,
  1831. struct megasas_cmd *cmd)
  1832. {
  1833. if (cmd->sync_cmd) {
  1834. cmd->sync_cmd = 0;
  1835. cmd->cmd_status = 0;
  1836. wake_up(&instance->abort_cmd_wait_q);
  1837. }
  1838. return;
  1839. }
  1840. /**
  1841. * megasas_complete_cmd - Completes a command
  1842. * @instance: Adapter soft state
  1843. * @cmd: Command to be completed
  1844. * @alt_status: If non-zero, use this value as status to
  1845. * SCSI mid-layer instead of the value returned
  1846. * by the FW. This should be used if caller wants
  1847. * an alternate status (as in the case of aborted
  1848. * commands)
  1849. */
  1850. void
  1851. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1852. u8 alt_status)
  1853. {
  1854. int exception = 0;
  1855. struct megasas_header *hdr = &cmd->frame->hdr;
  1856. unsigned long flags;
  1857. struct fusion_context *fusion = instance->ctrl_context;
  1858. /* flag for the retry reset */
  1859. cmd->retry_for_fw_reset = 0;
  1860. if (cmd->scmd)
  1861. cmd->scmd->SCp.ptr = NULL;
  1862. switch (hdr->cmd) {
  1863. case MFI_CMD_PD_SCSI_IO:
  1864. case MFI_CMD_LD_SCSI_IO:
  1865. /*
  1866. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1867. * issued either through an IO path or an IOCTL path. If it
  1868. * was via IOCTL, we will send it to internal completion.
  1869. */
  1870. if (cmd->sync_cmd) {
  1871. cmd->sync_cmd = 0;
  1872. megasas_complete_int_cmd(instance, cmd);
  1873. break;
  1874. }
  1875. case MFI_CMD_LD_READ:
  1876. case MFI_CMD_LD_WRITE:
  1877. if (alt_status) {
  1878. cmd->scmd->result = alt_status << 16;
  1879. exception = 1;
  1880. }
  1881. if (exception) {
  1882. atomic_dec(&instance->fw_outstanding);
  1883. scsi_dma_unmap(cmd->scmd);
  1884. cmd->scmd->scsi_done(cmd->scmd);
  1885. megasas_return_cmd(instance, cmd);
  1886. break;
  1887. }
  1888. switch (hdr->cmd_status) {
  1889. case MFI_STAT_OK:
  1890. cmd->scmd->result = DID_OK << 16;
  1891. break;
  1892. case MFI_STAT_SCSI_IO_FAILED:
  1893. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1894. cmd->scmd->result =
  1895. (DID_ERROR << 16) | hdr->scsi_status;
  1896. break;
  1897. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1898. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1899. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1900. memset(cmd->scmd->sense_buffer, 0,
  1901. SCSI_SENSE_BUFFERSIZE);
  1902. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1903. hdr->sense_len);
  1904. cmd->scmd->result |= DRIVER_SENSE << 24;
  1905. }
  1906. break;
  1907. case MFI_STAT_LD_OFFLINE:
  1908. case MFI_STAT_DEVICE_NOT_FOUND:
  1909. cmd->scmd->result = DID_BAD_TARGET << 16;
  1910. break;
  1911. default:
  1912. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1913. hdr->cmd_status);
  1914. cmd->scmd->result = DID_ERROR << 16;
  1915. break;
  1916. }
  1917. atomic_dec(&instance->fw_outstanding);
  1918. scsi_dma_unmap(cmd->scmd);
  1919. cmd->scmd->scsi_done(cmd->scmd);
  1920. megasas_return_cmd(instance, cmd);
  1921. break;
  1922. case MFI_CMD_SMP:
  1923. case MFI_CMD_STP:
  1924. case MFI_CMD_DCMD:
  1925. /* Check for LD map update */
  1926. if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
  1927. (cmd->frame->dcmd.mbox.b[1] == 1)) {
  1928. spin_lock_irqsave(instance->host->host_lock, flags);
  1929. if (cmd->frame->hdr.cmd_status != 0) {
  1930. if (cmd->frame->hdr.cmd_status !=
  1931. MFI_STAT_NOT_FOUND)
  1932. printk(KERN_WARNING "megasas: map sync"
  1933. "failed, status = 0x%x.\n",
  1934. cmd->frame->hdr.cmd_status);
  1935. else {
  1936. megasas_return_cmd(instance, cmd);
  1937. spin_unlock_irqrestore(
  1938. instance->host->host_lock,
  1939. flags);
  1940. break;
  1941. }
  1942. } else
  1943. instance->map_id++;
  1944. megasas_return_cmd(instance, cmd);
  1945. if (MR_ValidateMapInfo(
  1946. fusion->ld_map[(instance->map_id & 1)],
  1947. fusion->load_balance_info))
  1948. fusion->fast_path_io = 1;
  1949. else
  1950. fusion->fast_path_io = 0;
  1951. megasas_sync_map_info(instance);
  1952. spin_unlock_irqrestore(instance->host->host_lock,
  1953. flags);
  1954. break;
  1955. }
  1956. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  1957. cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
  1958. spin_lock_irqsave(&poll_aen_lock, flags);
  1959. megasas_poll_wait_aen = 0;
  1960. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1961. }
  1962. /*
  1963. * See if got an event notification
  1964. */
  1965. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1966. megasas_service_aen(instance, cmd);
  1967. else
  1968. megasas_complete_int_cmd(instance, cmd);
  1969. break;
  1970. case MFI_CMD_ABORT:
  1971. /*
  1972. * Cmd issued to abort another cmd returned
  1973. */
  1974. megasas_complete_abort(instance, cmd);
  1975. break;
  1976. default:
  1977. printk("megasas: Unknown command completed! [0x%X]\n",
  1978. hdr->cmd);
  1979. break;
  1980. }
  1981. }
  1982. /**
  1983. * megasas_issue_pending_cmds_again - issue all pending cmds
  1984. * in FW again because of the fw reset
  1985. * @instance: Adapter soft state
  1986. */
  1987. static inline void
  1988. megasas_issue_pending_cmds_again(struct megasas_instance *instance)
  1989. {
  1990. struct megasas_cmd *cmd;
  1991. struct list_head clist_local;
  1992. union megasas_evt_class_locale class_locale;
  1993. unsigned long flags;
  1994. u32 seq_num;
  1995. INIT_LIST_HEAD(&clist_local);
  1996. spin_lock_irqsave(&instance->hba_lock, flags);
  1997. list_splice_init(&instance->internal_reset_pending_q, &clist_local);
  1998. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1999. while (!list_empty(&clist_local)) {
  2000. cmd = list_entry((&clist_local)->next,
  2001. struct megasas_cmd, list);
  2002. list_del_init(&cmd->list);
  2003. if (cmd->sync_cmd || cmd->scmd) {
  2004. printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
  2005. "detected to be pending while HBA reset.\n",
  2006. cmd, cmd->scmd, cmd->sync_cmd);
  2007. cmd->retry_for_fw_reset++;
  2008. if (cmd->retry_for_fw_reset == 3) {
  2009. printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
  2010. "was tried multiple times during reset."
  2011. "Shutting down the HBA\n",
  2012. cmd, cmd->scmd, cmd->sync_cmd);
  2013. megaraid_sas_kill_hba(instance);
  2014. instance->adprecovery =
  2015. MEGASAS_HW_CRITICAL_ERROR;
  2016. return;
  2017. }
  2018. }
  2019. if (cmd->sync_cmd == 1) {
  2020. if (cmd->scmd) {
  2021. printk(KERN_NOTICE "megaraid_sas: unexpected"
  2022. "cmd attached to internal command!\n");
  2023. }
  2024. printk(KERN_NOTICE "megasas: %p synchronous cmd"
  2025. "on the internal reset queue,"
  2026. "issue it again.\n", cmd);
  2027. cmd->cmd_status = ENODATA;
  2028. instance->instancet->fire_cmd(instance,
  2029. cmd->frame_phys_addr ,
  2030. 0, instance->reg_set);
  2031. } else if (cmd->scmd) {
  2032. printk(KERN_NOTICE "megasas: %p scsi cmd [%02x],%#lx"
  2033. "detected on the internal queue, issue again.\n",
  2034. cmd, cmd->scmd->cmnd[0], cmd->scmd->serial_number);
  2035. atomic_inc(&instance->fw_outstanding);
  2036. instance->instancet->fire_cmd(instance,
  2037. cmd->frame_phys_addr,
  2038. cmd->frame_count-1, instance->reg_set);
  2039. } else {
  2040. printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
  2041. "internal reset defer list while re-issue!!\n",
  2042. cmd);
  2043. }
  2044. }
  2045. if (instance->aen_cmd) {
  2046. printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
  2047. megasas_return_cmd(instance, instance->aen_cmd);
  2048. instance->aen_cmd = NULL;
  2049. }
  2050. /*
  2051. * Initiate AEN (Asynchronous Event Notification)
  2052. */
  2053. seq_num = instance->last_seq_num;
  2054. class_locale.members.reserved = 0;
  2055. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2056. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2057. megasas_register_aen(instance, seq_num, class_locale.word);
  2058. }
  2059. /**
  2060. * Move the internal reset pending commands to a deferred queue.
  2061. *
  2062. * We move the commands pending at internal reset time to a
  2063. * pending queue. This queue would be flushed after successful
  2064. * completion of the internal reset sequence. if the internal reset
  2065. * did not complete in time, the kernel reset handler would flush
  2066. * these commands.
  2067. **/
  2068. static void
  2069. megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
  2070. {
  2071. struct megasas_cmd *cmd;
  2072. int i;
  2073. u32 max_cmd = instance->max_fw_cmds;
  2074. u32 defer_index;
  2075. unsigned long flags;
  2076. defer_index = 0;
  2077. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  2078. for (i = 0; i < max_cmd; i++) {
  2079. cmd = instance->cmd_list[i];
  2080. if (cmd->sync_cmd == 1 || cmd->scmd) {
  2081. printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
  2082. "on the defer queue as internal\n",
  2083. defer_index, cmd, cmd->sync_cmd, cmd->scmd);
  2084. if (!list_empty(&cmd->list)) {
  2085. printk(KERN_NOTICE "megaraid_sas: ERROR while"
  2086. " moving this cmd:%p, %d %p, it was"
  2087. "discovered on some list?\n",
  2088. cmd, cmd->sync_cmd, cmd->scmd);
  2089. list_del_init(&cmd->list);
  2090. }
  2091. defer_index++;
  2092. list_add_tail(&cmd->list,
  2093. &instance->internal_reset_pending_q);
  2094. }
  2095. }
  2096. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  2097. }
  2098. static void
  2099. process_fw_state_change_wq(struct work_struct *work)
  2100. {
  2101. struct megasas_instance *instance =
  2102. container_of(work, struct megasas_instance, work_init);
  2103. u32 wait;
  2104. unsigned long flags;
  2105. if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
  2106. printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
  2107. instance->adprecovery);
  2108. return ;
  2109. }
  2110. if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
  2111. printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
  2112. "state, restarting it...\n");
  2113. instance->instancet->disable_intr(instance->reg_set);
  2114. atomic_set(&instance->fw_outstanding, 0);
  2115. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2116. instance->instancet->adp_reset(instance, instance->reg_set);
  2117. atomic_set(&instance->fw_reset_no_pci_access, 0 );
  2118. printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
  2119. "initiating next stage...\n");
  2120. printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
  2121. "state 2 starting...\n");
  2122. /*waitting for about 20 second before start the second init*/
  2123. for (wait = 0; wait < 30; wait++) {
  2124. msleep(1000);
  2125. }
  2126. if (megasas_transition_to_ready(instance)) {
  2127. printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
  2128. megaraid_sas_kill_hba(instance);
  2129. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  2130. return ;
  2131. }
  2132. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  2133. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  2134. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
  2135. ) {
  2136. *instance->consumer = *instance->producer;
  2137. } else {
  2138. *instance->consumer = 0;
  2139. *instance->producer = 0;
  2140. }
  2141. megasas_issue_init_mfi(instance);
  2142. spin_lock_irqsave(&instance->hba_lock, flags);
  2143. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  2144. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2145. instance->instancet->enable_intr(instance->reg_set);
  2146. megasas_issue_pending_cmds_again(instance);
  2147. instance->issuepend_done = 1;
  2148. }
  2149. return ;
  2150. }
  2151. /**
  2152. * megasas_deplete_reply_queue - Processes all completed commands
  2153. * @instance: Adapter soft state
  2154. * @alt_status: Alternate status to be returned to
  2155. * SCSI mid-layer instead of the status
  2156. * returned by the FW
  2157. * Note: this must be called with hba lock held
  2158. */
  2159. static int
  2160. megasas_deplete_reply_queue(struct megasas_instance *instance,
  2161. u8 alt_status)
  2162. {
  2163. u32 mfiStatus;
  2164. u32 fw_state;
  2165. if ((mfiStatus = instance->instancet->check_reset(instance,
  2166. instance->reg_set)) == 1) {
  2167. return IRQ_HANDLED;
  2168. }
  2169. if ((mfiStatus = instance->instancet->clear_intr(
  2170. instance->reg_set)
  2171. ) == 0) {
  2172. /* Hardware may not set outbound_intr_status in MSI-X mode */
  2173. if (!instance->msi_flag)
  2174. return IRQ_NONE;
  2175. }
  2176. instance->mfiStatus = mfiStatus;
  2177. if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
  2178. fw_state = instance->instancet->read_fw_status_reg(
  2179. instance->reg_set) & MFI_STATE_MASK;
  2180. if (fw_state != MFI_STATE_FAULT) {
  2181. printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
  2182. fw_state);
  2183. }
  2184. if ((fw_state == MFI_STATE_FAULT) &&
  2185. (instance->disableOnlineCtrlReset == 0)) {
  2186. printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
  2187. if ((instance->pdev->device ==
  2188. PCI_DEVICE_ID_LSI_SAS1064R) ||
  2189. (instance->pdev->device ==
  2190. PCI_DEVICE_ID_DELL_PERC5) ||
  2191. (instance->pdev->device ==
  2192. PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  2193. *instance->consumer =
  2194. MEGASAS_ADPRESET_INPROG_SIGN;
  2195. }
  2196. instance->instancet->disable_intr(instance->reg_set);
  2197. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  2198. instance->issuepend_done = 0;
  2199. atomic_set(&instance->fw_outstanding, 0);
  2200. megasas_internal_reset_defer_cmds(instance);
  2201. printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
  2202. fw_state, instance->adprecovery);
  2203. schedule_work(&instance->work_init);
  2204. return IRQ_HANDLED;
  2205. } else {
  2206. printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
  2207. fw_state, instance->disableOnlineCtrlReset);
  2208. }
  2209. }
  2210. tasklet_schedule(&instance->isr_tasklet);
  2211. return IRQ_HANDLED;
  2212. }
  2213. /**
  2214. * megasas_isr - isr entry point
  2215. */
  2216. static irqreturn_t megasas_isr(int irq, void *devp)
  2217. {
  2218. struct megasas_instance *instance;
  2219. unsigned long flags;
  2220. irqreturn_t rc;
  2221. if (atomic_read(
  2222. &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
  2223. return IRQ_HANDLED;
  2224. instance = (struct megasas_instance *)devp;
  2225. spin_lock_irqsave(&instance->hba_lock, flags);
  2226. rc = megasas_deplete_reply_queue(instance, DID_OK);
  2227. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2228. return rc;
  2229. }
  2230. /**
  2231. * megasas_transition_to_ready - Move the FW to READY state
  2232. * @instance: Adapter soft state
  2233. *
  2234. * During the initialization, FW passes can potentially be in any one of
  2235. * several possible states. If the FW in operational, waiting-for-handshake
  2236. * states, driver must take steps to bring it to ready state. Otherwise, it
  2237. * has to wait for the ready state.
  2238. */
  2239. int
  2240. megasas_transition_to_ready(struct megasas_instance* instance)
  2241. {
  2242. int i;
  2243. u8 max_wait;
  2244. u32 fw_state;
  2245. u32 cur_state;
  2246. u32 abs_state, curr_abs_state;
  2247. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2248. if (fw_state != MFI_STATE_READY)
  2249. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  2250. " state\n");
  2251. while (fw_state != MFI_STATE_READY) {
  2252. abs_state =
  2253. instance->instancet->read_fw_status_reg(instance->reg_set);
  2254. switch (fw_state) {
  2255. case MFI_STATE_FAULT:
  2256. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  2257. max_wait = MEGASAS_RESET_WAIT_TIME;
  2258. cur_state = MFI_STATE_FAULT;
  2259. break;
  2260. case MFI_STATE_WAIT_HANDSHAKE:
  2261. /*
  2262. * Set the CLR bit in inbound doorbell
  2263. */
  2264. if ((instance->pdev->device ==
  2265. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2266. (instance->pdev->device ==
  2267. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2268. (instance->pdev->device ==
  2269. PCI_DEVICE_ID_LSI_FUSION)) {
  2270. writel(
  2271. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2272. &instance->reg_set->doorbell);
  2273. } else {
  2274. writel(
  2275. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2276. &instance->reg_set->inbound_doorbell);
  2277. }
  2278. max_wait = MEGASAS_RESET_WAIT_TIME;
  2279. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  2280. break;
  2281. case MFI_STATE_BOOT_MESSAGE_PENDING:
  2282. if ((instance->pdev->device ==
  2283. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2284. (instance->pdev->device ==
  2285. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2286. (instance->pdev->device ==
  2287. PCI_DEVICE_ID_LSI_FUSION)) {
  2288. writel(MFI_INIT_HOTPLUG,
  2289. &instance->reg_set->doorbell);
  2290. } else
  2291. writel(MFI_INIT_HOTPLUG,
  2292. &instance->reg_set->inbound_doorbell);
  2293. max_wait = MEGASAS_RESET_WAIT_TIME;
  2294. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  2295. break;
  2296. case MFI_STATE_OPERATIONAL:
  2297. /*
  2298. * Bring it to READY state; assuming max wait 10 secs
  2299. */
  2300. instance->instancet->disable_intr(instance->reg_set);
  2301. if ((instance->pdev->device ==
  2302. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2303. (instance->pdev->device ==
  2304. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2305. (instance->pdev->device
  2306. == PCI_DEVICE_ID_LSI_FUSION)) {
  2307. writel(MFI_RESET_FLAGS,
  2308. &instance->reg_set->doorbell);
  2309. if (instance->pdev->device ==
  2310. PCI_DEVICE_ID_LSI_FUSION) {
  2311. for (i = 0; i < (10 * 1000); i += 20) {
  2312. if (readl(
  2313. &instance->
  2314. reg_set->
  2315. doorbell) & 1)
  2316. msleep(20);
  2317. else
  2318. break;
  2319. }
  2320. }
  2321. } else
  2322. writel(MFI_RESET_FLAGS,
  2323. &instance->reg_set->inbound_doorbell);
  2324. max_wait = MEGASAS_RESET_WAIT_TIME;
  2325. cur_state = MFI_STATE_OPERATIONAL;
  2326. break;
  2327. case MFI_STATE_UNDEFINED:
  2328. /*
  2329. * This state should not last for more than 2 seconds
  2330. */
  2331. max_wait = MEGASAS_RESET_WAIT_TIME;
  2332. cur_state = MFI_STATE_UNDEFINED;
  2333. break;
  2334. case MFI_STATE_BB_INIT:
  2335. max_wait = MEGASAS_RESET_WAIT_TIME;
  2336. cur_state = MFI_STATE_BB_INIT;
  2337. break;
  2338. case MFI_STATE_FW_INIT:
  2339. max_wait = MEGASAS_RESET_WAIT_TIME;
  2340. cur_state = MFI_STATE_FW_INIT;
  2341. break;
  2342. case MFI_STATE_FW_INIT_2:
  2343. max_wait = MEGASAS_RESET_WAIT_TIME;
  2344. cur_state = MFI_STATE_FW_INIT_2;
  2345. break;
  2346. case MFI_STATE_DEVICE_SCAN:
  2347. max_wait = MEGASAS_RESET_WAIT_TIME;
  2348. cur_state = MFI_STATE_DEVICE_SCAN;
  2349. break;
  2350. case MFI_STATE_FLUSH_CACHE:
  2351. max_wait = MEGASAS_RESET_WAIT_TIME;
  2352. cur_state = MFI_STATE_FLUSH_CACHE;
  2353. break;
  2354. default:
  2355. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  2356. fw_state);
  2357. return -ENODEV;
  2358. }
  2359. /*
  2360. * The cur_state should not last for more than max_wait secs
  2361. */
  2362. for (i = 0; i < (max_wait * 1000); i++) {
  2363. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  2364. MFI_STATE_MASK ;
  2365. curr_abs_state =
  2366. instance->instancet->read_fw_status_reg(instance->reg_set);
  2367. if (abs_state == curr_abs_state) {
  2368. msleep(1);
  2369. } else
  2370. break;
  2371. }
  2372. /*
  2373. * Return error if fw_state hasn't changed after max_wait
  2374. */
  2375. if (curr_abs_state == abs_state) {
  2376. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  2377. "in %d secs\n", fw_state, max_wait);
  2378. return -ENODEV;
  2379. }
  2380. }
  2381. printk(KERN_INFO "megasas: FW now in Ready state\n");
  2382. return 0;
  2383. }
  2384. /**
  2385. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  2386. * @instance: Adapter soft state
  2387. */
  2388. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  2389. {
  2390. int i;
  2391. u32 max_cmd = instance->max_mfi_cmds;
  2392. struct megasas_cmd *cmd;
  2393. if (!instance->frame_dma_pool)
  2394. return;
  2395. /*
  2396. * Return all frames to pool
  2397. */
  2398. for (i = 0; i < max_cmd; i++) {
  2399. cmd = instance->cmd_list[i];
  2400. if (cmd->frame)
  2401. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  2402. cmd->frame_phys_addr);
  2403. if (cmd->sense)
  2404. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  2405. cmd->sense_phys_addr);
  2406. }
  2407. /*
  2408. * Now destroy the pool itself
  2409. */
  2410. pci_pool_destroy(instance->frame_dma_pool);
  2411. pci_pool_destroy(instance->sense_dma_pool);
  2412. instance->frame_dma_pool = NULL;
  2413. instance->sense_dma_pool = NULL;
  2414. }
  2415. /**
  2416. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  2417. * @instance: Adapter soft state
  2418. *
  2419. * Each command packet has an embedded DMA memory buffer that is used for
  2420. * filling MFI frame and the SG list that immediately follows the frame. This
  2421. * function creates those DMA memory buffers for each command packet by using
  2422. * PCI pool facility.
  2423. */
  2424. static int megasas_create_frame_pool(struct megasas_instance *instance)
  2425. {
  2426. int i;
  2427. u32 max_cmd;
  2428. u32 sge_sz;
  2429. u32 sgl_sz;
  2430. u32 total_sz;
  2431. u32 frame_count;
  2432. struct megasas_cmd *cmd;
  2433. max_cmd = instance->max_mfi_cmds;
  2434. /*
  2435. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  2436. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  2437. */
  2438. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  2439. sizeof(struct megasas_sge32);
  2440. if (instance->flag_ieee) {
  2441. sge_sz = sizeof(struct megasas_sge_skinny);
  2442. }
  2443. /*
  2444. * Calculated the number of 64byte frames required for SGL
  2445. */
  2446. sgl_sz = sge_sz * instance->max_num_sge;
  2447. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  2448. frame_count = 15;
  2449. /*
  2450. * We need one extra frame for the MFI command
  2451. */
  2452. frame_count++;
  2453. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  2454. /*
  2455. * Use DMA pool facility provided by PCI layer
  2456. */
  2457. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  2458. instance->pdev, total_sz, 64,
  2459. 0);
  2460. if (!instance->frame_dma_pool) {
  2461. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  2462. return -ENOMEM;
  2463. }
  2464. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  2465. instance->pdev, 128, 4, 0);
  2466. if (!instance->sense_dma_pool) {
  2467. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  2468. pci_pool_destroy(instance->frame_dma_pool);
  2469. instance->frame_dma_pool = NULL;
  2470. return -ENOMEM;
  2471. }
  2472. /*
  2473. * Allocate and attach a frame to each of the commands in cmd_list.
  2474. * By making cmd->index as the context instead of the &cmd, we can
  2475. * always use 32bit context regardless of the architecture
  2476. */
  2477. for (i = 0; i < max_cmd; i++) {
  2478. cmd = instance->cmd_list[i];
  2479. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  2480. GFP_KERNEL, &cmd->frame_phys_addr);
  2481. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  2482. GFP_KERNEL, &cmd->sense_phys_addr);
  2483. /*
  2484. * megasas_teardown_frame_pool() takes care of freeing
  2485. * whatever has been allocated
  2486. */
  2487. if (!cmd->frame || !cmd->sense) {
  2488. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  2489. megasas_teardown_frame_pool(instance);
  2490. return -ENOMEM;
  2491. }
  2492. memset(cmd->frame, 0, total_sz);
  2493. cmd->frame->io.context = cmd->index;
  2494. cmd->frame->io.pad_0 = 0;
  2495. }
  2496. return 0;
  2497. }
  2498. /**
  2499. * megasas_free_cmds - Free all the cmds in the free cmd pool
  2500. * @instance: Adapter soft state
  2501. */
  2502. void megasas_free_cmds(struct megasas_instance *instance)
  2503. {
  2504. int i;
  2505. /* First free the MFI frame pool */
  2506. megasas_teardown_frame_pool(instance);
  2507. /* Free all the commands in the cmd_list */
  2508. for (i = 0; i < instance->max_mfi_cmds; i++)
  2509. kfree(instance->cmd_list[i]);
  2510. /* Free the cmd_list buffer itself */
  2511. kfree(instance->cmd_list);
  2512. instance->cmd_list = NULL;
  2513. INIT_LIST_HEAD(&instance->cmd_pool);
  2514. }
  2515. /**
  2516. * megasas_alloc_cmds - Allocates the command packets
  2517. * @instance: Adapter soft state
  2518. *
  2519. * Each command that is issued to the FW, whether IO commands from the OS or
  2520. * internal commands like IOCTLs, are wrapped in local data structure called
  2521. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  2522. * the FW.
  2523. *
  2524. * Each frame has a 32-bit field called context (tag). This context is used
  2525. * to get back the megasas_cmd from the frame when a frame gets completed in
  2526. * the ISR. Typically the address of the megasas_cmd itself would be used as
  2527. * the context. But we wanted to keep the differences between 32 and 64 bit
  2528. * systems to the mininum. We always use 32 bit integers for the context. In
  2529. * this driver, the 32 bit values are the indices into an array cmd_list.
  2530. * This array is used only to look up the megasas_cmd given the context. The
  2531. * free commands themselves are maintained in a linked list called cmd_pool.
  2532. */
  2533. int megasas_alloc_cmds(struct megasas_instance *instance)
  2534. {
  2535. int i;
  2536. int j;
  2537. u32 max_cmd;
  2538. struct megasas_cmd *cmd;
  2539. max_cmd = instance->max_mfi_cmds;
  2540. /*
  2541. * instance->cmd_list is an array of struct megasas_cmd pointers.
  2542. * Allocate the dynamic array first and then allocate individual
  2543. * commands.
  2544. */
  2545. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  2546. if (!instance->cmd_list) {
  2547. printk(KERN_DEBUG "megasas: out of memory\n");
  2548. return -ENOMEM;
  2549. }
  2550. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
  2551. for (i = 0; i < max_cmd; i++) {
  2552. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  2553. GFP_KERNEL);
  2554. if (!instance->cmd_list[i]) {
  2555. for (j = 0; j < i; j++)
  2556. kfree(instance->cmd_list[j]);
  2557. kfree(instance->cmd_list);
  2558. instance->cmd_list = NULL;
  2559. return -ENOMEM;
  2560. }
  2561. }
  2562. /*
  2563. * Add all the commands to command pool (instance->cmd_pool)
  2564. */
  2565. for (i = 0; i < max_cmd; i++) {
  2566. cmd = instance->cmd_list[i];
  2567. memset(cmd, 0, sizeof(struct megasas_cmd));
  2568. cmd->index = i;
  2569. cmd->scmd = NULL;
  2570. cmd->instance = instance;
  2571. list_add_tail(&cmd->list, &instance->cmd_pool);
  2572. }
  2573. /*
  2574. * Create a frame pool and assign one frame to each cmd
  2575. */
  2576. if (megasas_create_frame_pool(instance)) {
  2577. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  2578. megasas_free_cmds(instance);
  2579. }
  2580. return 0;
  2581. }
  2582. /*
  2583. * megasas_get_pd_list_info - Returns FW's pd_list structure
  2584. * @instance: Adapter soft state
  2585. * @pd_list: pd_list structure
  2586. *
  2587. * Issues an internal command (DCMD) to get the FW's controller PD
  2588. * list structure. This information is mainly used to find out SYSTEM
  2589. * supported by the FW.
  2590. */
  2591. static int
  2592. megasas_get_pd_list(struct megasas_instance *instance)
  2593. {
  2594. int ret = 0, pd_index = 0;
  2595. struct megasas_cmd *cmd;
  2596. struct megasas_dcmd_frame *dcmd;
  2597. struct MR_PD_LIST *ci;
  2598. struct MR_PD_ADDRESS *pd_addr;
  2599. dma_addr_t ci_h = 0;
  2600. cmd = megasas_get_cmd(instance);
  2601. if (!cmd) {
  2602. printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
  2603. return -ENOMEM;
  2604. }
  2605. dcmd = &cmd->frame->dcmd;
  2606. ci = pci_alloc_consistent(instance->pdev,
  2607. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  2608. if (!ci) {
  2609. printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
  2610. megasas_return_cmd(instance, cmd);
  2611. return -ENOMEM;
  2612. }
  2613. memset(ci, 0, sizeof(*ci));
  2614. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2615. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  2616. dcmd->mbox.b[1] = 0;
  2617. dcmd->cmd = MFI_CMD_DCMD;
  2618. dcmd->cmd_status = 0xFF;
  2619. dcmd->sge_count = 1;
  2620. dcmd->flags = MFI_FRAME_DIR_READ;
  2621. dcmd->timeout = 0;
  2622. dcmd->pad_0 = 0;
  2623. dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2624. dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
  2625. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2626. dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2627. if (!megasas_issue_polled(instance, cmd)) {
  2628. ret = 0;
  2629. } else {
  2630. ret = -1;
  2631. }
  2632. /*
  2633. * the following function will get the instance PD LIST.
  2634. */
  2635. pd_addr = ci->addr;
  2636. if ( ret == 0 &&
  2637. (ci->count <
  2638. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
  2639. memset(instance->pd_list, 0,
  2640. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  2641. for (pd_index = 0; pd_index < ci->count; pd_index++) {
  2642. instance->pd_list[pd_addr->deviceId].tid =
  2643. pd_addr->deviceId;
  2644. instance->pd_list[pd_addr->deviceId].driveType =
  2645. pd_addr->scsiDevType;
  2646. instance->pd_list[pd_addr->deviceId].driveState =
  2647. MR_PD_STATE_SYSTEM;
  2648. pd_addr++;
  2649. }
  2650. }
  2651. pci_free_consistent(instance->pdev,
  2652. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  2653. ci, ci_h);
  2654. megasas_return_cmd(instance, cmd);
  2655. return ret;
  2656. }
  2657. /*
  2658. * megasas_get_ld_list_info - Returns FW's ld_list structure
  2659. * @instance: Adapter soft state
  2660. * @ld_list: ld_list structure
  2661. *
  2662. * Issues an internal command (DCMD) to get the FW's controller PD
  2663. * list structure. This information is mainly used to find out SYSTEM
  2664. * supported by the FW.
  2665. */
  2666. static int
  2667. megasas_get_ld_list(struct megasas_instance *instance)
  2668. {
  2669. int ret = 0, ld_index = 0, ids = 0;
  2670. struct megasas_cmd *cmd;
  2671. struct megasas_dcmd_frame *dcmd;
  2672. struct MR_LD_LIST *ci;
  2673. dma_addr_t ci_h = 0;
  2674. cmd = megasas_get_cmd(instance);
  2675. if (!cmd) {
  2676. printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
  2677. return -ENOMEM;
  2678. }
  2679. dcmd = &cmd->frame->dcmd;
  2680. ci = pci_alloc_consistent(instance->pdev,
  2681. sizeof(struct MR_LD_LIST),
  2682. &ci_h);
  2683. if (!ci) {
  2684. printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
  2685. megasas_return_cmd(instance, cmd);
  2686. return -ENOMEM;
  2687. }
  2688. memset(ci, 0, sizeof(*ci));
  2689. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2690. dcmd->cmd = MFI_CMD_DCMD;
  2691. dcmd->cmd_status = 0xFF;
  2692. dcmd->sge_count = 1;
  2693. dcmd->flags = MFI_FRAME_DIR_READ;
  2694. dcmd->timeout = 0;
  2695. dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
  2696. dcmd->opcode = MR_DCMD_LD_GET_LIST;
  2697. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2698. dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
  2699. dcmd->pad_0 = 0;
  2700. if (!megasas_issue_polled(instance, cmd)) {
  2701. ret = 0;
  2702. } else {
  2703. ret = -1;
  2704. }
  2705. /* the following function will get the instance PD LIST */
  2706. if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
  2707. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2708. for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
  2709. if (ci->ldList[ld_index].state != 0) {
  2710. ids = ci->ldList[ld_index].ref.targetId;
  2711. instance->ld_ids[ids] =
  2712. ci->ldList[ld_index].ref.targetId;
  2713. }
  2714. }
  2715. }
  2716. pci_free_consistent(instance->pdev,
  2717. sizeof(struct MR_LD_LIST),
  2718. ci,
  2719. ci_h);
  2720. megasas_return_cmd(instance, cmd);
  2721. return ret;
  2722. }
  2723. /**
  2724. * megasas_get_controller_info - Returns FW's controller structure
  2725. * @instance: Adapter soft state
  2726. * @ctrl_info: Controller information structure
  2727. *
  2728. * Issues an internal command (DCMD) to get the FW's controller structure.
  2729. * This information is mainly used to find out the maximum IO transfer per
  2730. * command supported by the FW.
  2731. */
  2732. static int
  2733. megasas_get_ctrl_info(struct megasas_instance *instance,
  2734. struct megasas_ctrl_info *ctrl_info)
  2735. {
  2736. int ret = 0;
  2737. struct megasas_cmd *cmd;
  2738. struct megasas_dcmd_frame *dcmd;
  2739. struct megasas_ctrl_info *ci;
  2740. dma_addr_t ci_h = 0;
  2741. cmd = megasas_get_cmd(instance);
  2742. if (!cmd) {
  2743. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  2744. return -ENOMEM;
  2745. }
  2746. dcmd = &cmd->frame->dcmd;
  2747. ci = pci_alloc_consistent(instance->pdev,
  2748. sizeof(struct megasas_ctrl_info), &ci_h);
  2749. if (!ci) {
  2750. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  2751. megasas_return_cmd(instance, cmd);
  2752. return -ENOMEM;
  2753. }
  2754. memset(ci, 0, sizeof(*ci));
  2755. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2756. dcmd->cmd = MFI_CMD_DCMD;
  2757. dcmd->cmd_status = 0xFF;
  2758. dcmd->sge_count = 1;
  2759. dcmd->flags = MFI_FRAME_DIR_READ;
  2760. dcmd->timeout = 0;
  2761. dcmd->pad_0 = 0;
  2762. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  2763. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  2764. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2765. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  2766. if (!megasas_issue_polled(instance, cmd)) {
  2767. ret = 0;
  2768. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  2769. } else {
  2770. ret = -1;
  2771. }
  2772. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  2773. ci, ci_h);
  2774. megasas_return_cmd(instance, cmd);
  2775. return ret;
  2776. }
  2777. /**
  2778. * megasas_issue_init_mfi - Initializes the FW
  2779. * @instance: Adapter soft state
  2780. *
  2781. * Issues the INIT MFI cmd
  2782. */
  2783. static int
  2784. megasas_issue_init_mfi(struct megasas_instance *instance)
  2785. {
  2786. u32 context;
  2787. struct megasas_cmd *cmd;
  2788. struct megasas_init_frame *init_frame;
  2789. struct megasas_init_queue_info *initq_info;
  2790. dma_addr_t init_frame_h;
  2791. dma_addr_t initq_info_h;
  2792. /*
  2793. * Prepare a init frame. Note the init frame points to queue info
  2794. * structure. Each frame has SGL allocated after first 64 bytes. For
  2795. * this frame - since we don't need any SGL - we use SGL's space as
  2796. * queue info structure
  2797. *
  2798. * We will not get a NULL command below. We just created the pool.
  2799. */
  2800. cmd = megasas_get_cmd(instance);
  2801. init_frame = (struct megasas_init_frame *)cmd->frame;
  2802. initq_info = (struct megasas_init_queue_info *)
  2803. ((unsigned long)init_frame + 64);
  2804. init_frame_h = cmd->frame_phys_addr;
  2805. initq_info_h = init_frame_h + 64;
  2806. context = init_frame->context;
  2807. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  2808. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  2809. init_frame->context = context;
  2810. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  2811. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  2812. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  2813. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  2814. init_frame->cmd = MFI_CMD_INIT;
  2815. init_frame->cmd_status = 0xFF;
  2816. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  2817. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  2818. /*
  2819. * disable the intr before firing the init frame to FW
  2820. */
  2821. instance->instancet->disable_intr(instance->reg_set);
  2822. /*
  2823. * Issue the init frame in polled mode
  2824. */
  2825. if (megasas_issue_polled(instance, cmd)) {
  2826. printk(KERN_ERR "megasas: Failed to init firmware\n");
  2827. megasas_return_cmd(instance, cmd);
  2828. goto fail_fw_init;
  2829. }
  2830. megasas_return_cmd(instance, cmd);
  2831. return 0;
  2832. fail_fw_init:
  2833. return -EINVAL;
  2834. }
  2835. /**
  2836. * megasas_start_timer - Initializes a timer object
  2837. * @instance: Adapter soft state
  2838. * @timer: timer object to be initialized
  2839. * @fn: timer function
  2840. * @interval: time interval between timer function call
  2841. */
  2842. static inline void
  2843. megasas_start_timer(struct megasas_instance *instance,
  2844. struct timer_list *timer,
  2845. void *fn, unsigned long interval)
  2846. {
  2847. init_timer(timer);
  2848. timer->expires = jiffies + interval;
  2849. timer->data = (unsigned long)instance;
  2850. timer->function = fn;
  2851. add_timer(timer);
  2852. }
  2853. /**
  2854. * megasas_io_completion_timer - Timer fn
  2855. * @instance_addr: Address of adapter soft state
  2856. *
  2857. * Schedules tasklet for cmd completion
  2858. * if poll_mode_io is set
  2859. */
  2860. static void
  2861. megasas_io_completion_timer(unsigned long instance_addr)
  2862. {
  2863. struct megasas_instance *instance =
  2864. (struct megasas_instance *)instance_addr;
  2865. if (atomic_read(&instance->fw_outstanding))
  2866. tasklet_schedule(&instance->isr_tasklet);
  2867. /* Restart timer */
  2868. if (poll_mode_io)
  2869. mod_timer(&instance->io_completion_timer,
  2870. jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
  2871. }
  2872. static u32
  2873. megasas_init_adapter_mfi(struct megasas_instance *instance)
  2874. {
  2875. struct megasas_register_set __iomem *reg_set;
  2876. u32 context_sz;
  2877. u32 reply_q_sz;
  2878. reg_set = instance->reg_set;
  2879. /*
  2880. * Get various operational parameters from status register
  2881. */
  2882. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  2883. /*
  2884. * Reduce the max supported cmds by 1. This is to ensure that the
  2885. * reply_q_sz (1 more than the max cmd that driver may send)
  2886. * does not exceed max cmds that the FW can support
  2887. */
  2888. instance->max_fw_cmds = instance->max_fw_cmds-1;
  2889. instance->max_mfi_cmds = instance->max_fw_cmds;
  2890. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  2891. 0x10;
  2892. /*
  2893. * Create a pool of commands
  2894. */
  2895. if (megasas_alloc_cmds(instance))
  2896. goto fail_alloc_cmds;
  2897. /*
  2898. * Allocate memory for reply queue. Length of reply queue should
  2899. * be _one_ more than the maximum commands handled by the firmware.
  2900. *
  2901. * Note: When FW completes commands, it places corresponding contex
  2902. * values in this circular reply queue. This circular queue is a fairly
  2903. * typical producer-consumer queue. FW is the producer (of completed
  2904. * commands) and the driver is the consumer.
  2905. */
  2906. context_sz = sizeof(u32);
  2907. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  2908. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  2909. reply_q_sz,
  2910. &instance->reply_queue_h);
  2911. if (!instance->reply_queue) {
  2912. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  2913. goto fail_reply_queue;
  2914. }
  2915. if (megasas_issue_init_mfi(instance))
  2916. goto fail_fw_init;
  2917. instance->fw_support_ieee = 0;
  2918. instance->fw_support_ieee =
  2919. (instance->instancet->read_fw_status_reg(reg_set) &
  2920. 0x04000000);
  2921. printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
  2922. instance->fw_support_ieee);
  2923. if (instance->fw_support_ieee)
  2924. instance->flag_ieee = 1;
  2925. return 0;
  2926. fail_fw_init:
  2927. pci_free_consistent(instance->pdev, reply_q_sz,
  2928. instance->reply_queue, instance->reply_queue_h);
  2929. fail_reply_queue:
  2930. megasas_free_cmds(instance);
  2931. fail_alloc_cmds:
  2932. return 1;
  2933. }
  2934. /**
  2935. * megasas_init_fw - Initializes the FW
  2936. * @instance: Adapter soft state
  2937. *
  2938. * This is the main function for initializing firmware
  2939. */
  2940. static int megasas_init_fw(struct megasas_instance *instance)
  2941. {
  2942. u32 max_sectors_1;
  2943. u32 max_sectors_2;
  2944. u32 tmp_sectors;
  2945. struct megasas_register_set __iomem *reg_set;
  2946. struct megasas_ctrl_info *ctrl_info;
  2947. unsigned long bar_list;
  2948. /* Find first memory bar */
  2949. bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
  2950. instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
  2951. instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
  2952. if (pci_request_selected_regions(instance->pdev, instance->bar,
  2953. "megasas: LSI")) {
  2954. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  2955. return -EBUSY;
  2956. }
  2957. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  2958. if (!instance->reg_set) {
  2959. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  2960. goto fail_ioremap;
  2961. }
  2962. reg_set = instance->reg_set;
  2963. switch (instance->pdev->device) {
  2964. case PCI_DEVICE_ID_LSI_FUSION:
  2965. instance->instancet = &megasas_instance_template_fusion;
  2966. break;
  2967. case PCI_DEVICE_ID_LSI_SAS1078R:
  2968. case PCI_DEVICE_ID_LSI_SAS1078DE:
  2969. instance->instancet = &megasas_instance_template_ppc;
  2970. break;
  2971. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  2972. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  2973. instance->instancet = &megasas_instance_template_gen2;
  2974. break;
  2975. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  2976. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  2977. instance->instancet = &megasas_instance_template_skinny;
  2978. break;
  2979. case PCI_DEVICE_ID_LSI_SAS1064R:
  2980. case PCI_DEVICE_ID_DELL_PERC5:
  2981. default:
  2982. instance->instancet = &megasas_instance_template_xscale;
  2983. break;
  2984. }
  2985. /*
  2986. * We expect the FW state to be READY
  2987. */
  2988. if (megasas_transition_to_ready(instance))
  2989. goto fail_ready_state;
  2990. /* Get operational params, sge flags, send init cmd to controller */
  2991. if (instance->instancet->init_adapter(instance))
  2992. goto fail_init_adapter;
  2993. printk(KERN_ERR "megasas: INIT adapter done\n");
  2994. /** for passthrough
  2995. * the following function will get the PD LIST.
  2996. */
  2997. memset(instance->pd_list, 0 ,
  2998. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  2999. megasas_get_pd_list(instance);
  3000. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  3001. megasas_get_ld_list(instance);
  3002. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  3003. /*
  3004. * Compute the max allowed sectors per IO: The controller info has two
  3005. * limits on max sectors. Driver should use the minimum of these two.
  3006. *
  3007. * 1 << stripe_sz_ops.min = max sectors per strip
  3008. *
  3009. * Note that older firmwares ( < FW ver 30) didn't report information
  3010. * to calculate max_sectors_1. So the number ended up as zero always.
  3011. */
  3012. tmp_sectors = 0;
  3013. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  3014. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  3015. ctrl_info->max_strips_per_io;
  3016. max_sectors_2 = ctrl_info->max_request_size;
  3017. tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
  3018. instance->disableOnlineCtrlReset =
  3019. ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
  3020. }
  3021. instance->max_sectors_per_req = instance->max_num_sge *
  3022. PAGE_SIZE / 512;
  3023. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  3024. instance->max_sectors_per_req = tmp_sectors;
  3025. kfree(ctrl_info);
  3026. /*
  3027. * Setup tasklet for cmd completion
  3028. */
  3029. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3030. (unsigned long)instance);
  3031. /* Initialize the cmd completion timer */
  3032. if (poll_mode_io)
  3033. megasas_start_timer(instance, &instance->io_completion_timer,
  3034. megasas_io_completion_timer,
  3035. MEGASAS_COMPLETION_TIMER_INTERVAL);
  3036. return 0;
  3037. fail_init_adapter:
  3038. fail_ready_state:
  3039. iounmap(instance->reg_set);
  3040. fail_ioremap:
  3041. pci_release_selected_regions(instance->pdev, instance->bar);
  3042. return -EINVAL;
  3043. }
  3044. /**
  3045. * megasas_release_mfi - Reverses the FW initialization
  3046. * @intance: Adapter soft state
  3047. */
  3048. static void megasas_release_mfi(struct megasas_instance *instance)
  3049. {
  3050. u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
  3051. if (instance->reply_queue)
  3052. pci_free_consistent(instance->pdev, reply_q_sz,
  3053. instance->reply_queue, instance->reply_queue_h);
  3054. megasas_free_cmds(instance);
  3055. iounmap(instance->reg_set);
  3056. pci_release_selected_regions(instance->pdev, instance->bar);
  3057. }
  3058. /**
  3059. * megasas_get_seq_num - Gets latest event sequence numbers
  3060. * @instance: Adapter soft state
  3061. * @eli: FW event log sequence numbers information
  3062. *
  3063. * FW maintains a log of all events in a non-volatile area. Upper layers would
  3064. * usually find out the latest sequence number of the events, the seq number at
  3065. * the boot etc. They would "read" all the events below the latest seq number
  3066. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  3067. * number), they would subsribe to AEN (asynchronous event notification) and
  3068. * wait for the events to happen.
  3069. */
  3070. static int
  3071. megasas_get_seq_num(struct megasas_instance *instance,
  3072. struct megasas_evt_log_info *eli)
  3073. {
  3074. struct megasas_cmd *cmd;
  3075. struct megasas_dcmd_frame *dcmd;
  3076. struct megasas_evt_log_info *el_info;
  3077. dma_addr_t el_info_h = 0;
  3078. cmd = megasas_get_cmd(instance);
  3079. if (!cmd) {
  3080. return -ENOMEM;
  3081. }
  3082. dcmd = &cmd->frame->dcmd;
  3083. el_info = pci_alloc_consistent(instance->pdev,
  3084. sizeof(struct megasas_evt_log_info),
  3085. &el_info_h);
  3086. if (!el_info) {
  3087. megasas_return_cmd(instance, cmd);
  3088. return -ENOMEM;
  3089. }
  3090. memset(el_info, 0, sizeof(*el_info));
  3091. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3092. dcmd->cmd = MFI_CMD_DCMD;
  3093. dcmd->cmd_status = 0x0;
  3094. dcmd->sge_count = 1;
  3095. dcmd->flags = MFI_FRAME_DIR_READ;
  3096. dcmd->timeout = 0;
  3097. dcmd->pad_0 = 0;
  3098. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  3099. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  3100. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  3101. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  3102. megasas_issue_blocked_cmd(instance, cmd);
  3103. /*
  3104. * Copy the data back into callers buffer
  3105. */
  3106. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  3107. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  3108. el_info, el_info_h);
  3109. megasas_return_cmd(instance, cmd);
  3110. return 0;
  3111. }
  3112. /**
  3113. * megasas_register_aen - Registers for asynchronous event notification
  3114. * @instance: Adapter soft state
  3115. * @seq_num: The starting sequence number
  3116. * @class_locale: Class of the event
  3117. *
  3118. * This function subscribes for AEN for events beyond the @seq_num. It requests
  3119. * to be notified if and only if the event is of type @class_locale
  3120. */
  3121. static int
  3122. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  3123. u32 class_locale_word)
  3124. {
  3125. int ret_val;
  3126. struct megasas_cmd *cmd;
  3127. struct megasas_dcmd_frame *dcmd;
  3128. union megasas_evt_class_locale curr_aen;
  3129. union megasas_evt_class_locale prev_aen;
  3130. /*
  3131. * If there an AEN pending already (aen_cmd), check if the
  3132. * class_locale of that pending AEN is inclusive of the new
  3133. * AEN request we currently have. If it is, then we don't have
  3134. * to do anything. In other words, whichever events the current
  3135. * AEN request is subscribing to, have already been subscribed
  3136. * to.
  3137. *
  3138. * If the old_cmd is _not_ inclusive, then we have to abort
  3139. * that command, form a class_locale that is superset of both
  3140. * old and current and re-issue to the FW
  3141. */
  3142. curr_aen.word = class_locale_word;
  3143. if (instance->aen_cmd) {
  3144. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  3145. /*
  3146. * A class whose enum value is smaller is inclusive of all
  3147. * higher values. If a PROGRESS (= -1) was previously
  3148. * registered, then a new registration requests for higher
  3149. * classes need not be sent to FW. They are automatically
  3150. * included.
  3151. *
  3152. * Locale numbers don't have such hierarchy. They are bitmap
  3153. * values
  3154. */
  3155. if ((prev_aen.members.class <= curr_aen.members.class) &&
  3156. !((prev_aen.members.locale & curr_aen.members.locale) ^
  3157. curr_aen.members.locale)) {
  3158. /*
  3159. * Previously issued event registration includes
  3160. * current request. Nothing to do.
  3161. */
  3162. return 0;
  3163. } else {
  3164. curr_aen.members.locale |= prev_aen.members.locale;
  3165. if (prev_aen.members.class < curr_aen.members.class)
  3166. curr_aen.members.class = prev_aen.members.class;
  3167. instance->aen_cmd->abort_aen = 1;
  3168. ret_val = megasas_issue_blocked_abort_cmd(instance,
  3169. instance->
  3170. aen_cmd);
  3171. if (ret_val) {
  3172. printk(KERN_DEBUG "megasas: Failed to abort "
  3173. "previous AEN command\n");
  3174. return ret_val;
  3175. }
  3176. }
  3177. }
  3178. cmd = megasas_get_cmd(instance);
  3179. if (!cmd)
  3180. return -ENOMEM;
  3181. dcmd = &cmd->frame->dcmd;
  3182. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  3183. /*
  3184. * Prepare DCMD for aen registration
  3185. */
  3186. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3187. dcmd->cmd = MFI_CMD_DCMD;
  3188. dcmd->cmd_status = 0x0;
  3189. dcmd->sge_count = 1;
  3190. dcmd->flags = MFI_FRAME_DIR_READ;
  3191. dcmd->timeout = 0;
  3192. dcmd->pad_0 = 0;
  3193. instance->last_seq_num = seq_num;
  3194. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  3195. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  3196. dcmd->mbox.w[0] = seq_num;
  3197. dcmd->mbox.w[1] = curr_aen.word;
  3198. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  3199. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  3200. if (instance->aen_cmd != NULL) {
  3201. megasas_return_cmd(instance, cmd);
  3202. return 0;
  3203. }
  3204. /*
  3205. * Store reference to the cmd used to register for AEN. When an
  3206. * application wants us to register for AEN, we have to abort this
  3207. * cmd and re-register with a new EVENT LOCALE supplied by that app
  3208. */
  3209. instance->aen_cmd = cmd;
  3210. /*
  3211. * Issue the aen registration frame
  3212. */
  3213. instance->instancet->issue_dcmd(instance, cmd);
  3214. return 0;
  3215. }
  3216. /**
  3217. * megasas_start_aen - Subscribes to AEN during driver load time
  3218. * @instance: Adapter soft state
  3219. */
  3220. static int megasas_start_aen(struct megasas_instance *instance)
  3221. {
  3222. struct megasas_evt_log_info eli;
  3223. union megasas_evt_class_locale class_locale;
  3224. /*
  3225. * Get the latest sequence number from FW
  3226. */
  3227. memset(&eli, 0, sizeof(eli));
  3228. if (megasas_get_seq_num(instance, &eli))
  3229. return -1;
  3230. /*
  3231. * Register AEN with FW for latest sequence number plus 1
  3232. */
  3233. class_locale.members.reserved = 0;
  3234. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3235. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3236. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  3237. class_locale.word);
  3238. }
  3239. /**
  3240. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  3241. * @instance: Adapter soft state
  3242. */
  3243. static int megasas_io_attach(struct megasas_instance *instance)
  3244. {
  3245. struct Scsi_Host *host = instance->host;
  3246. /*
  3247. * Export parameters required by SCSI mid-layer
  3248. */
  3249. host->irq = instance->pdev->irq;
  3250. host->unique_id = instance->unique_id;
  3251. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3252. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3253. host->can_queue =
  3254. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  3255. } else
  3256. host->can_queue =
  3257. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  3258. host->this_id = instance->init_id;
  3259. host->sg_tablesize = instance->max_num_sge;
  3260. if (instance->fw_support_ieee)
  3261. instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
  3262. /*
  3263. * Check if the module parameter value for max_sectors can be used
  3264. */
  3265. if (max_sectors && max_sectors < instance->max_sectors_per_req)
  3266. instance->max_sectors_per_req = max_sectors;
  3267. else {
  3268. if (max_sectors) {
  3269. if (((instance->pdev->device ==
  3270. PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  3271. (instance->pdev->device ==
  3272. PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
  3273. (max_sectors <= MEGASAS_MAX_SECTORS)) {
  3274. instance->max_sectors_per_req = max_sectors;
  3275. } else {
  3276. printk(KERN_INFO "megasas: max_sectors should be > 0"
  3277. "and <= %d (or < 1MB for GEN2 controller)\n",
  3278. instance->max_sectors_per_req);
  3279. }
  3280. }
  3281. }
  3282. host->max_sectors = instance->max_sectors_per_req;
  3283. host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
  3284. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  3285. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  3286. host->max_lun = MEGASAS_MAX_LUN;
  3287. host->max_cmd_len = 16;
  3288. /* Fusion only supports host reset */
  3289. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
  3290. host->hostt->eh_device_reset_handler = NULL;
  3291. host->hostt->eh_bus_reset_handler = NULL;
  3292. }
  3293. /*
  3294. * Notify the mid-layer about the new controller
  3295. */
  3296. if (scsi_add_host(host, &instance->pdev->dev)) {
  3297. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  3298. return -ENODEV;
  3299. }
  3300. /*
  3301. * Trigger SCSI to scan our drives
  3302. */
  3303. scsi_scan_host(host);
  3304. return 0;
  3305. }
  3306. static int
  3307. megasas_set_dma_mask(struct pci_dev *pdev)
  3308. {
  3309. /*
  3310. * All our contollers are capable of performing 64-bit DMA
  3311. */
  3312. if (IS_DMA64) {
  3313. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  3314. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3315. goto fail_set_dma_mask;
  3316. }
  3317. } else {
  3318. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3319. goto fail_set_dma_mask;
  3320. }
  3321. return 0;
  3322. fail_set_dma_mask:
  3323. return 1;
  3324. }
  3325. /**
  3326. * megasas_probe_one - PCI hotplug entry point
  3327. * @pdev: PCI device structure
  3328. * @id: PCI ids of supported hotplugged adapter
  3329. */
  3330. static int __devinit
  3331. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3332. {
  3333. int rval, pos;
  3334. struct Scsi_Host *host;
  3335. struct megasas_instance *instance;
  3336. u16 control = 0;
  3337. /* Reset MSI-X in the kdump kernel */
  3338. if (reset_devices) {
  3339. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3340. if (pos) {
  3341. pci_read_config_word(pdev, msi_control_reg(pos),
  3342. &control);
  3343. if (control & PCI_MSIX_FLAGS_ENABLE) {
  3344. dev_info(&pdev->dev, "resetting MSI-X\n");
  3345. pci_write_config_word(pdev,
  3346. msi_control_reg(pos),
  3347. control &
  3348. ~PCI_MSIX_FLAGS_ENABLE);
  3349. }
  3350. }
  3351. }
  3352. /*
  3353. * Announce PCI information
  3354. */
  3355. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  3356. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  3357. pdev->subsystem_device);
  3358. printk("bus %d:slot %d:func %d\n",
  3359. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  3360. /*
  3361. * PCI prepping: enable device set bus mastering and dma mask
  3362. */
  3363. rval = pci_enable_device_mem(pdev);
  3364. if (rval) {
  3365. return rval;
  3366. }
  3367. pci_set_master(pdev);
  3368. if (megasas_set_dma_mask(pdev))
  3369. goto fail_set_dma_mask;
  3370. host = scsi_host_alloc(&megasas_template,
  3371. sizeof(struct megasas_instance));
  3372. if (!host) {
  3373. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  3374. goto fail_alloc_instance;
  3375. }
  3376. instance = (struct megasas_instance *)host->hostdata;
  3377. memset(instance, 0, sizeof(*instance));
  3378. atomic_set( &instance->fw_reset_no_pci_access, 0 );
  3379. instance->pdev = pdev;
  3380. switch (instance->pdev->device) {
  3381. case PCI_DEVICE_ID_LSI_FUSION:
  3382. {
  3383. struct fusion_context *fusion;
  3384. instance->ctrl_context =
  3385. kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
  3386. if (!instance->ctrl_context) {
  3387. printk(KERN_DEBUG "megasas: Failed to allocate "
  3388. "memory for Fusion context info\n");
  3389. goto fail_alloc_dma_buf;
  3390. }
  3391. fusion = instance->ctrl_context;
  3392. INIT_LIST_HEAD(&fusion->cmd_pool);
  3393. spin_lock_init(&fusion->cmd_pool_lock);
  3394. }
  3395. break;
  3396. default: /* For all other supported controllers */
  3397. instance->producer =
  3398. pci_alloc_consistent(pdev, sizeof(u32),
  3399. &instance->producer_h);
  3400. instance->consumer =
  3401. pci_alloc_consistent(pdev, sizeof(u32),
  3402. &instance->consumer_h);
  3403. if (!instance->producer || !instance->consumer) {
  3404. printk(KERN_DEBUG "megasas: Failed to allocate"
  3405. "memory for producer, consumer\n");
  3406. goto fail_alloc_dma_buf;
  3407. }
  3408. *instance->producer = 0;
  3409. *instance->consumer = 0;
  3410. break;
  3411. }
  3412. megasas_poll_wait_aen = 0;
  3413. instance->flag_ieee = 0;
  3414. instance->ev = NULL;
  3415. instance->issuepend_done = 1;
  3416. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  3417. megasas_poll_wait_aen = 0;
  3418. instance->evt_detail = pci_alloc_consistent(pdev,
  3419. sizeof(struct
  3420. megasas_evt_detail),
  3421. &instance->evt_detail_h);
  3422. if (!instance->evt_detail) {
  3423. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  3424. "event detail structure\n");
  3425. goto fail_alloc_dma_buf;
  3426. }
  3427. /*
  3428. * Initialize locks and queues
  3429. */
  3430. INIT_LIST_HEAD(&instance->cmd_pool);
  3431. INIT_LIST_HEAD(&instance->internal_reset_pending_q);
  3432. atomic_set(&instance->fw_outstanding,0);
  3433. init_waitqueue_head(&instance->int_cmd_wait_q);
  3434. init_waitqueue_head(&instance->abort_cmd_wait_q);
  3435. spin_lock_init(&instance->cmd_pool_lock);
  3436. spin_lock_init(&instance->hba_lock);
  3437. spin_lock_init(&instance->completion_lock);
  3438. spin_lock_init(&poll_aen_lock);
  3439. mutex_init(&instance->aen_mutex);
  3440. mutex_init(&instance->reset_mutex);
  3441. /*
  3442. * Initialize PCI related and misc parameters
  3443. */
  3444. instance->host = host;
  3445. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  3446. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  3447. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3448. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3449. instance->flag_ieee = 1;
  3450. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  3451. } else
  3452. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  3453. megasas_dbg_lvl = 0;
  3454. instance->flag = 0;
  3455. instance->unload = 1;
  3456. instance->last_time = 0;
  3457. instance->disableOnlineCtrlReset = 1;
  3458. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
  3459. INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
  3460. else
  3461. INIT_WORK(&instance->work_init, process_fw_state_change_wq);
  3462. /* Try to enable MSI-X */
  3463. if ((instance->pdev->device != PCI_DEVICE_ID_LSI_SAS1078R) &&
  3464. (instance->pdev->device != PCI_DEVICE_ID_LSI_SAS1078DE) &&
  3465. (instance->pdev->device != PCI_DEVICE_ID_LSI_VERDE_ZCR) &&
  3466. !msix_disable && !pci_enable_msix(instance->pdev,
  3467. &instance->msixentry, 1))
  3468. instance->msi_flag = 1;
  3469. /*
  3470. * Initialize MFI Firmware
  3471. */
  3472. if (megasas_init_fw(instance))
  3473. goto fail_init_mfi;
  3474. /*
  3475. * Register IRQ
  3476. */
  3477. if (request_irq(instance->msi_flag ? instance->msixentry.vector :
  3478. pdev->irq, instance->instancet->service_isr,
  3479. IRQF_SHARED, "megasas", instance)) {
  3480. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  3481. goto fail_irq;
  3482. }
  3483. instance->instancet->enable_intr(instance->reg_set);
  3484. /*
  3485. * Store instance in PCI softstate
  3486. */
  3487. pci_set_drvdata(pdev, instance);
  3488. /*
  3489. * Add this controller to megasas_mgmt_info structure so that it
  3490. * can be exported to management applications
  3491. */
  3492. megasas_mgmt_info.count++;
  3493. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  3494. megasas_mgmt_info.max_index++;
  3495. /*
  3496. * Initiate AEN (Asynchronous Event Notification)
  3497. */
  3498. if (megasas_start_aen(instance)) {
  3499. printk(KERN_DEBUG "megasas: start aen failed\n");
  3500. goto fail_start_aen;
  3501. }
  3502. /*
  3503. * Register with SCSI mid-layer
  3504. */
  3505. if (megasas_io_attach(instance))
  3506. goto fail_io_attach;
  3507. instance->unload = 0;
  3508. return 0;
  3509. fail_start_aen:
  3510. fail_io_attach:
  3511. megasas_mgmt_info.count--;
  3512. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  3513. megasas_mgmt_info.max_index--;
  3514. pci_set_drvdata(pdev, NULL);
  3515. instance->instancet->disable_intr(instance->reg_set);
  3516. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3517. instance->pdev->irq, instance);
  3518. fail_irq:
  3519. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
  3520. megasas_release_fusion(instance);
  3521. else
  3522. megasas_release_mfi(instance);
  3523. fail_init_mfi:
  3524. if (instance->msi_flag)
  3525. pci_disable_msix(instance->pdev);
  3526. fail_alloc_dma_buf:
  3527. if (instance->evt_detail)
  3528. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3529. instance->evt_detail,
  3530. instance->evt_detail_h);
  3531. if (instance->producer)
  3532. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3533. instance->producer_h);
  3534. if (instance->consumer)
  3535. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3536. instance->consumer_h);
  3537. scsi_host_put(host);
  3538. fail_alloc_instance:
  3539. fail_set_dma_mask:
  3540. pci_disable_device(pdev);
  3541. return -ENODEV;
  3542. }
  3543. /**
  3544. * megasas_flush_cache - Requests FW to flush all its caches
  3545. * @instance: Adapter soft state
  3546. */
  3547. static void megasas_flush_cache(struct megasas_instance *instance)
  3548. {
  3549. struct megasas_cmd *cmd;
  3550. struct megasas_dcmd_frame *dcmd;
  3551. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3552. return;
  3553. cmd = megasas_get_cmd(instance);
  3554. if (!cmd)
  3555. return;
  3556. dcmd = &cmd->frame->dcmd;
  3557. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3558. dcmd->cmd = MFI_CMD_DCMD;
  3559. dcmd->cmd_status = 0x0;
  3560. dcmd->sge_count = 0;
  3561. dcmd->flags = MFI_FRAME_DIR_NONE;
  3562. dcmd->timeout = 0;
  3563. dcmd->pad_0 = 0;
  3564. dcmd->data_xfer_len = 0;
  3565. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  3566. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  3567. megasas_issue_blocked_cmd(instance, cmd);
  3568. megasas_return_cmd(instance, cmd);
  3569. return;
  3570. }
  3571. /**
  3572. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  3573. * @instance: Adapter soft state
  3574. * @opcode: Shutdown/Hibernate
  3575. */
  3576. static void megasas_shutdown_controller(struct megasas_instance *instance,
  3577. u32 opcode)
  3578. {
  3579. struct megasas_cmd *cmd;
  3580. struct megasas_dcmd_frame *dcmd;
  3581. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3582. return;
  3583. cmd = megasas_get_cmd(instance);
  3584. if (!cmd)
  3585. return;
  3586. if (instance->aen_cmd)
  3587. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  3588. if (instance->map_update_cmd)
  3589. megasas_issue_blocked_abort_cmd(instance,
  3590. instance->map_update_cmd);
  3591. dcmd = &cmd->frame->dcmd;
  3592. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3593. dcmd->cmd = MFI_CMD_DCMD;
  3594. dcmd->cmd_status = 0x0;
  3595. dcmd->sge_count = 0;
  3596. dcmd->flags = MFI_FRAME_DIR_NONE;
  3597. dcmd->timeout = 0;
  3598. dcmd->pad_0 = 0;
  3599. dcmd->data_xfer_len = 0;
  3600. dcmd->opcode = opcode;
  3601. megasas_issue_blocked_cmd(instance, cmd);
  3602. megasas_return_cmd(instance, cmd);
  3603. return;
  3604. }
  3605. #ifdef CONFIG_PM
  3606. /**
  3607. * megasas_suspend - driver suspend entry point
  3608. * @pdev: PCI device structure
  3609. * @state: PCI power state to suspend routine
  3610. */
  3611. static int
  3612. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  3613. {
  3614. struct Scsi_Host *host;
  3615. struct megasas_instance *instance;
  3616. instance = pci_get_drvdata(pdev);
  3617. host = instance->host;
  3618. instance->unload = 1;
  3619. if (poll_mode_io)
  3620. del_timer_sync(&instance->io_completion_timer);
  3621. megasas_flush_cache(instance);
  3622. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  3623. /* cancel the delayed work if this work still in queue */
  3624. if (instance->ev != NULL) {
  3625. struct megasas_aen_event *ev = instance->ev;
  3626. cancel_delayed_work_sync(
  3627. (struct delayed_work *)&ev->hotplug_work);
  3628. instance->ev = NULL;
  3629. }
  3630. tasklet_kill(&instance->isr_tasklet);
  3631. pci_set_drvdata(instance->pdev, instance);
  3632. instance->instancet->disable_intr(instance->reg_set);
  3633. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3634. instance->pdev->irq, instance);
  3635. if (instance->msi_flag)
  3636. pci_disable_msix(instance->pdev);
  3637. pci_save_state(pdev);
  3638. pci_disable_device(pdev);
  3639. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  3640. return 0;
  3641. }
  3642. /**
  3643. * megasas_resume- driver resume entry point
  3644. * @pdev: PCI device structure
  3645. */
  3646. static int
  3647. megasas_resume(struct pci_dev *pdev)
  3648. {
  3649. int rval;
  3650. struct Scsi_Host *host;
  3651. struct megasas_instance *instance;
  3652. instance = pci_get_drvdata(pdev);
  3653. host = instance->host;
  3654. pci_set_power_state(pdev, PCI_D0);
  3655. pci_enable_wake(pdev, PCI_D0, 0);
  3656. pci_restore_state(pdev);
  3657. /*
  3658. * PCI prepping: enable device set bus mastering and dma mask
  3659. */
  3660. rval = pci_enable_device_mem(pdev);
  3661. if (rval) {
  3662. printk(KERN_ERR "megasas: Enable device failed\n");
  3663. return rval;
  3664. }
  3665. pci_set_master(pdev);
  3666. if (megasas_set_dma_mask(pdev))
  3667. goto fail_set_dma_mask;
  3668. /* Now re-enable MSI-X */
  3669. if (instance->msi_flag)
  3670. pci_enable_msix(instance->pdev, &instance->msixentry, 1);
  3671. /*
  3672. * Initialize MFI Firmware
  3673. */
  3674. atomic_set(&instance->fw_outstanding, 0);
  3675. /*
  3676. * We expect the FW state to be READY
  3677. */
  3678. if (megasas_transition_to_ready(instance))
  3679. goto fail_ready_state;
  3680. switch (instance->pdev->device) {
  3681. case PCI_DEVICE_ID_LSI_FUSION:
  3682. {
  3683. megasas_reset_reply_desc(instance);
  3684. if (megasas_ioc_init_fusion(instance)) {
  3685. megasas_free_cmds(instance);
  3686. megasas_free_cmds_fusion(instance);
  3687. goto fail_init_mfi;
  3688. }
  3689. if (!megasas_get_map_info(instance))
  3690. megasas_sync_map_info(instance);
  3691. }
  3692. break;
  3693. default:
  3694. *instance->producer = 0;
  3695. *instance->consumer = 0;
  3696. if (megasas_issue_init_mfi(instance))
  3697. goto fail_init_mfi;
  3698. break;
  3699. }
  3700. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3701. (unsigned long)instance);
  3702. /*
  3703. * Register IRQ
  3704. */
  3705. if (request_irq(instance->msi_flag ? instance->msixentry.vector :
  3706. pdev->irq, instance->instancet->service_isr,
  3707. IRQF_SHARED, "megasas", instance)) {
  3708. printk(KERN_ERR "megasas: Failed to register IRQ\n");
  3709. goto fail_irq;
  3710. }
  3711. instance->instancet->enable_intr(instance->reg_set);
  3712. /*
  3713. * Initiate AEN (Asynchronous Event Notification)
  3714. */
  3715. if (megasas_start_aen(instance))
  3716. printk(KERN_ERR "megasas: Start AEN failed\n");
  3717. /* Initialize the cmd completion timer */
  3718. if (poll_mode_io)
  3719. megasas_start_timer(instance, &instance->io_completion_timer,
  3720. megasas_io_completion_timer,
  3721. MEGASAS_COMPLETION_TIMER_INTERVAL);
  3722. instance->unload = 0;
  3723. return 0;
  3724. fail_irq:
  3725. fail_init_mfi:
  3726. if (instance->evt_detail)
  3727. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3728. instance->evt_detail,
  3729. instance->evt_detail_h);
  3730. if (instance->producer)
  3731. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3732. instance->producer_h);
  3733. if (instance->consumer)
  3734. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3735. instance->consumer_h);
  3736. scsi_host_put(host);
  3737. fail_set_dma_mask:
  3738. fail_ready_state:
  3739. pci_disable_device(pdev);
  3740. return -ENODEV;
  3741. }
  3742. #else
  3743. #define megasas_suspend NULL
  3744. #define megasas_resume NULL
  3745. #endif
  3746. /**
  3747. * megasas_detach_one - PCI hot"un"plug entry point
  3748. * @pdev: PCI device structure
  3749. */
  3750. static void __devexit megasas_detach_one(struct pci_dev *pdev)
  3751. {
  3752. int i;
  3753. struct Scsi_Host *host;
  3754. struct megasas_instance *instance;
  3755. struct fusion_context *fusion;
  3756. instance = pci_get_drvdata(pdev);
  3757. instance->unload = 1;
  3758. host = instance->host;
  3759. fusion = instance->ctrl_context;
  3760. if (poll_mode_io)
  3761. del_timer_sync(&instance->io_completion_timer);
  3762. scsi_remove_host(instance->host);
  3763. megasas_flush_cache(instance);
  3764. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3765. /* cancel the delayed work if this work still in queue*/
  3766. if (instance->ev != NULL) {
  3767. struct megasas_aen_event *ev = instance->ev;
  3768. cancel_delayed_work_sync(
  3769. (struct delayed_work *)&ev->hotplug_work);
  3770. instance->ev = NULL;
  3771. }
  3772. tasklet_kill(&instance->isr_tasklet);
  3773. /*
  3774. * Take the instance off the instance array. Note that we will not
  3775. * decrement the max_index. We let this array be sparse array
  3776. */
  3777. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3778. if (megasas_mgmt_info.instance[i] == instance) {
  3779. megasas_mgmt_info.count--;
  3780. megasas_mgmt_info.instance[i] = NULL;
  3781. break;
  3782. }
  3783. }
  3784. pci_set_drvdata(instance->pdev, NULL);
  3785. instance->instancet->disable_intr(instance->reg_set);
  3786. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3787. instance->pdev->irq, instance);
  3788. if (instance->msi_flag)
  3789. pci_disable_msix(instance->pdev);
  3790. switch (instance->pdev->device) {
  3791. case PCI_DEVICE_ID_LSI_FUSION:
  3792. megasas_release_fusion(instance);
  3793. for (i = 0; i < 2 ; i++)
  3794. if (fusion->ld_map[i])
  3795. dma_free_coherent(&instance->pdev->dev,
  3796. fusion->map_sz,
  3797. fusion->ld_map[i],
  3798. fusion->
  3799. ld_map_phys[i]);
  3800. kfree(instance->ctrl_context);
  3801. break;
  3802. default:
  3803. megasas_release_mfi(instance);
  3804. pci_free_consistent(pdev,
  3805. sizeof(struct megasas_evt_detail),
  3806. instance->evt_detail,
  3807. instance->evt_detail_h);
  3808. pci_free_consistent(pdev, sizeof(u32),
  3809. instance->producer,
  3810. instance->producer_h);
  3811. pci_free_consistent(pdev, sizeof(u32),
  3812. instance->consumer,
  3813. instance->consumer_h);
  3814. break;
  3815. }
  3816. scsi_host_put(host);
  3817. pci_set_drvdata(pdev, NULL);
  3818. pci_disable_device(pdev);
  3819. return;
  3820. }
  3821. /**
  3822. * megasas_shutdown - Shutdown entry point
  3823. * @device: Generic device structure
  3824. */
  3825. static void megasas_shutdown(struct pci_dev *pdev)
  3826. {
  3827. struct megasas_instance *instance = pci_get_drvdata(pdev);
  3828. instance->unload = 1;
  3829. megasas_flush_cache(instance);
  3830. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3831. }
  3832. /**
  3833. * megasas_mgmt_open - char node "open" entry point
  3834. */
  3835. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  3836. {
  3837. /*
  3838. * Allow only those users with admin rights
  3839. */
  3840. if (!capable(CAP_SYS_ADMIN))
  3841. return -EACCES;
  3842. return 0;
  3843. }
  3844. /**
  3845. * megasas_mgmt_fasync - Async notifier registration from applications
  3846. *
  3847. * This function adds the calling process to a driver global queue. When an
  3848. * event occurs, SIGIO will be sent to all processes in this queue.
  3849. */
  3850. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  3851. {
  3852. int rc;
  3853. mutex_lock(&megasas_async_queue_mutex);
  3854. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  3855. mutex_unlock(&megasas_async_queue_mutex);
  3856. if (rc >= 0) {
  3857. /* For sanity check when we get ioctl */
  3858. filep->private_data = filep;
  3859. return 0;
  3860. }
  3861. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  3862. return rc;
  3863. }
  3864. /**
  3865. * megasas_mgmt_poll - char node "poll" entry point
  3866. * */
  3867. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  3868. {
  3869. unsigned int mask;
  3870. unsigned long flags;
  3871. poll_wait(file, &megasas_poll_wait, wait);
  3872. spin_lock_irqsave(&poll_aen_lock, flags);
  3873. if (megasas_poll_wait_aen)
  3874. mask = (POLLIN | POLLRDNORM);
  3875. else
  3876. mask = 0;
  3877. spin_unlock_irqrestore(&poll_aen_lock, flags);
  3878. return mask;
  3879. }
  3880. /**
  3881. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  3882. * @instance: Adapter soft state
  3883. * @argp: User's ioctl packet
  3884. */
  3885. static int
  3886. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  3887. struct megasas_iocpacket __user * user_ioc,
  3888. struct megasas_iocpacket *ioc)
  3889. {
  3890. struct megasas_sge32 *kern_sge32;
  3891. struct megasas_cmd *cmd;
  3892. void *kbuff_arr[MAX_IOCTL_SGE];
  3893. dma_addr_t buf_handle = 0;
  3894. int error = 0, i;
  3895. void *sense = NULL;
  3896. dma_addr_t sense_handle;
  3897. unsigned long *sense_ptr;
  3898. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  3899. if (ioc->sge_count > MAX_IOCTL_SGE) {
  3900. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  3901. ioc->sge_count, MAX_IOCTL_SGE);
  3902. return -EINVAL;
  3903. }
  3904. cmd = megasas_get_cmd(instance);
  3905. if (!cmd) {
  3906. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  3907. return -ENOMEM;
  3908. }
  3909. /*
  3910. * User's IOCTL packet has 2 frames (maximum). Copy those two
  3911. * frames into our cmd's frames. cmd->frame's context will get
  3912. * overwritten when we copy from user's frames. So set that value
  3913. * alone separately
  3914. */
  3915. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  3916. cmd->frame->hdr.context = cmd->index;
  3917. cmd->frame->hdr.pad_0 = 0;
  3918. /*
  3919. * The management interface between applications and the fw uses
  3920. * MFI frames. E.g, RAID configuration changes, LD property changes
  3921. * etc are accomplishes through different kinds of MFI frames. The
  3922. * driver needs to care only about substituting user buffers with
  3923. * kernel buffers in SGLs. The location of SGL is embedded in the
  3924. * struct iocpacket itself.
  3925. */
  3926. kern_sge32 = (struct megasas_sge32 *)
  3927. ((unsigned long)cmd->frame + ioc->sgl_off);
  3928. /*
  3929. * For each user buffer, create a mirror buffer and copy in
  3930. */
  3931. for (i = 0; i < ioc->sge_count; i++) {
  3932. if (!ioc->sgl[i].iov_len)
  3933. continue;
  3934. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  3935. ioc->sgl[i].iov_len,
  3936. &buf_handle, GFP_KERNEL);
  3937. if (!kbuff_arr[i]) {
  3938. printk(KERN_DEBUG "megasas: Failed to alloc "
  3939. "kernel SGL buffer for IOCTL \n");
  3940. error = -ENOMEM;
  3941. goto out;
  3942. }
  3943. /*
  3944. * We don't change the dma_coherent_mask, so
  3945. * pci_alloc_consistent only returns 32bit addresses
  3946. */
  3947. kern_sge32[i].phys_addr = (u32) buf_handle;
  3948. kern_sge32[i].length = ioc->sgl[i].iov_len;
  3949. /*
  3950. * We created a kernel buffer corresponding to the
  3951. * user buffer. Now copy in from the user buffer
  3952. */
  3953. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  3954. (u32) (ioc->sgl[i].iov_len))) {
  3955. error = -EFAULT;
  3956. goto out;
  3957. }
  3958. }
  3959. if (ioc->sense_len) {
  3960. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  3961. &sense_handle, GFP_KERNEL);
  3962. if (!sense) {
  3963. error = -ENOMEM;
  3964. goto out;
  3965. }
  3966. sense_ptr =
  3967. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  3968. *sense_ptr = sense_handle;
  3969. }
  3970. /*
  3971. * Set the sync_cmd flag so that the ISR knows not to complete this
  3972. * cmd to the SCSI mid-layer
  3973. */
  3974. cmd->sync_cmd = 1;
  3975. megasas_issue_blocked_cmd(instance, cmd);
  3976. cmd->sync_cmd = 0;
  3977. /*
  3978. * copy out the kernel buffers to user buffers
  3979. */
  3980. for (i = 0; i < ioc->sge_count; i++) {
  3981. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  3982. ioc->sgl[i].iov_len)) {
  3983. error = -EFAULT;
  3984. goto out;
  3985. }
  3986. }
  3987. /*
  3988. * copy out the sense
  3989. */
  3990. if (ioc->sense_len) {
  3991. /*
  3992. * sense_ptr points to the location that has the user
  3993. * sense buffer address
  3994. */
  3995. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  3996. ioc->sense_off);
  3997. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  3998. sense, ioc->sense_len)) {
  3999. printk(KERN_ERR "megasas: Failed to copy out to user "
  4000. "sense data\n");
  4001. error = -EFAULT;
  4002. goto out;
  4003. }
  4004. }
  4005. /*
  4006. * copy the status codes returned by the fw
  4007. */
  4008. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  4009. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  4010. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  4011. error = -EFAULT;
  4012. }
  4013. out:
  4014. if (sense) {
  4015. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  4016. sense, sense_handle);
  4017. }
  4018. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  4019. dma_free_coherent(&instance->pdev->dev,
  4020. kern_sge32[i].length,
  4021. kbuff_arr[i], kern_sge32[i].phys_addr);
  4022. }
  4023. megasas_return_cmd(instance, cmd);
  4024. return error;
  4025. }
  4026. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  4027. {
  4028. struct megasas_iocpacket __user *user_ioc =
  4029. (struct megasas_iocpacket __user *)arg;
  4030. struct megasas_iocpacket *ioc;
  4031. struct megasas_instance *instance;
  4032. int error;
  4033. int i;
  4034. unsigned long flags;
  4035. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4036. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  4037. if (!ioc)
  4038. return -ENOMEM;
  4039. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  4040. error = -EFAULT;
  4041. goto out_kfree_ioc;
  4042. }
  4043. instance = megasas_lookup_instance(ioc->host_no);
  4044. if (!instance) {
  4045. error = -ENODEV;
  4046. goto out_kfree_ioc;
  4047. }
  4048. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4049. printk(KERN_ERR "Controller in crit error\n");
  4050. error = -ENODEV;
  4051. goto out_kfree_ioc;
  4052. }
  4053. if (instance->unload == 1) {
  4054. error = -ENODEV;
  4055. goto out_kfree_ioc;
  4056. }
  4057. /*
  4058. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  4059. */
  4060. if (down_interruptible(&instance->ioctl_sem)) {
  4061. error = -ERESTARTSYS;
  4062. goto out_kfree_ioc;
  4063. }
  4064. for (i = 0; i < wait_time; i++) {
  4065. spin_lock_irqsave(&instance->hba_lock, flags);
  4066. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4067. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4068. break;
  4069. }
  4070. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4071. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4072. printk(KERN_NOTICE "megasas: waiting"
  4073. "for controller reset to finish\n");
  4074. }
  4075. msleep(1000);
  4076. }
  4077. spin_lock_irqsave(&instance->hba_lock, flags);
  4078. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4079. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4080. printk(KERN_ERR "megaraid_sas: timed out while"
  4081. "waiting for HBA to recover\n");
  4082. error = -ENODEV;
  4083. goto out_kfree_ioc;
  4084. }
  4085. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4086. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  4087. up(&instance->ioctl_sem);
  4088. out_kfree_ioc:
  4089. kfree(ioc);
  4090. return error;
  4091. }
  4092. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  4093. {
  4094. struct megasas_instance *instance;
  4095. struct megasas_aen aen;
  4096. int error;
  4097. int i;
  4098. unsigned long flags;
  4099. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4100. if (file->private_data != file) {
  4101. printk(KERN_DEBUG "megasas: fasync_helper was not "
  4102. "called first\n");
  4103. return -EINVAL;
  4104. }
  4105. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  4106. return -EFAULT;
  4107. instance = megasas_lookup_instance(aen.host_no);
  4108. if (!instance)
  4109. return -ENODEV;
  4110. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4111. return -ENODEV;
  4112. }
  4113. if (instance->unload == 1) {
  4114. return -ENODEV;
  4115. }
  4116. for (i = 0; i < wait_time; i++) {
  4117. spin_lock_irqsave(&instance->hba_lock, flags);
  4118. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4119. spin_unlock_irqrestore(&instance->hba_lock,
  4120. flags);
  4121. break;
  4122. }
  4123. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4124. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4125. printk(KERN_NOTICE "megasas: waiting for"
  4126. "controller reset to finish\n");
  4127. }
  4128. msleep(1000);
  4129. }
  4130. spin_lock_irqsave(&instance->hba_lock, flags);
  4131. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4132. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4133. printk(KERN_ERR "megaraid_sas: timed out while waiting"
  4134. "for HBA to recover.\n");
  4135. return -ENODEV;
  4136. }
  4137. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4138. mutex_lock(&instance->aen_mutex);
  4139. error = megasas_register_aen(instance, aen.seq_num,
  4140. aen.class_locale_word);
  4141. mutex_unlock(&instance->aen_mutex);
  4142. return error;
  4143. }
  4144. /**
  4145. * megasas_mgmt_ioctl - char node ioctl entry point
  4146. */
  4147. static long
  4148. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4149. {
  4150. switch (cmd) {
  4151. case MEGASAS_IOC_FIRMWARE:
  4152. return megasas_mgmt_ioctl_fw(file, arg);
  4153. case MEGASAS_IOC_GET_AEN:
  4154. return megasas_mgmt_ioctl_aen(file, arg);
  4155. }
  4156. return -ENOTTY;
  4157. }
  4158. #ifdef CONFIG_COMPAT
  4159. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  4160. {
  4161. struct compat_megasas_iocpacket __user *cioc =
  4162. (struct compat_megasas_iocpacket __user *)arg;
  4163. struct megasas_iocpacket __user *ioc =
  4164. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  4165. int i;
  4166. int error = 0;
  4167. compat_uptr_t ptr;
  4168. if (clear_user(ioc, sizeof(*ioc)))
  4169. return -EFAULT;
  4170. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  4171. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  4172. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  4173. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  4174. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  4175. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  4176. return -EFAULT;
  4177. /*
  4178. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  4179. * sense_len is not null, so prepare the 64bit value under
  4180. * the same condition.
  4181. */
  4182. if (ioc->sense_len) {
  4183. void __user **sense_ioc_ptr =
  4184. (void __user **)(ioc->frame.raw + ioc->sense_off);
  4185. compat_uptr_t *sense_cioc_ptr =
  4186. (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
  4187. if (get_user(ptr, sense_cioc_ptr) ||
  4188. put_user(compat_ptr(ptr), sense_ioc_ptr))
  4189. return -EFAULT;
  4190. }
  4191. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  4192. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  4193. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  4194. copy_in_user(&ioc->sgl[i].iov_len,
  4195. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  4196. return -EFAULT;
  4197. }
  4198. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  4199. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  4200. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  4201. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  4202. return -EFAULT;
  4203. }
  4204. return error;
  4205. }
  4206. static long
  4207. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  4208. unsigned long arg)
  4209. {
  4210. switch (cmd) {
  4211. case MEGASAS_IOC_FIRMWARE32:
  4212. return megasas_mgmt_compat_ioctl_fw(file, arg);
  4213. case MEGASAS_IOC_GET_AEN:
  4214. return megasas_mgmt_ioctl_aen(file, arg);
  4215. }
  4216. return -ENOTTY;
  4217. }
  4218. #endif
  4219. /*
  4220. * File operations structure for management interface
  4221. */
  4222. static const struct file_operations megasas_mgmt_fops = {
  4223. .owner = THIS_MODULE,
  4224. .open = megasas_mgmt_open,
  4225. .fasync = megasas_mgmt_fasync,
  4226. .unlocked_ioctl = megasas_mgmt_ioctl,
  4227. .poll = megasas_mgmt_poll,
  4228. #ifdef CONFIG_COMPAT
  4229. .compat_ioctl = megasas_mgmt_compat_ioctl,
  4230. #endif
  4231. .llseek = noop_llseek,
  4232. };
  4233. /*
  4234. * PCI hotplug support registration structure
  4235. */
  4236. static struct pci_driver megasas_pci_driver = {
  4237. .name = "megaraid_sas",
  4238. .id_table = megasas_pci_table,
  4239. .probe = megasas_probe_one,
  4240. .remove = __devexit_p(megasas_detach_one),
  4241. .suspend = megasas_suspend,
  4242. .resume = megasas_resume,
  4243. .shutdown = megasas_shutdown,
  4244. };
  4245. /*
  4246. * Sysfs driver attributes
  4247. */
  4248. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  4249. {
  4250. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  4251. MEGASAS_VERSION);
  4252. }
  4253. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  4254. static ssize_t
  4255. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  4256. {
  4257. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  4258. MEGASAS_RELDATE);
  4259. }
  4260. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  4261. NULL);
  4262. static ssize_t
  4263. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  4264. {
  4265. return sprintf(buf, "%u\n", support_poll_for_event);
  4266. }
  4267. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  4268. megasas_sysfs_show_support_poll_for_event, NULL);
  4269. static ssize_t
  4270. megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
  4271. {
  4272. return sprintf(buf, "%u\n", support_device_change);
  4273. }
  4274. static DRIVER_ATTR(support_device_change, S_IRUGO,
  4275. megasas_sysfs_show_support_device_change, NULL);
  4276. static ssize_t
  4277. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  4278. {
  4279. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  4280. }
  4281. static ssize_t
  4282. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  4283. {
  4284. int retval = count;
  4285. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  4286. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  4287. retval = -EINVAL;
  4288. }
  4289. return retval;
  4290. }
  4291. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  4292. megasas_sysfs_set_dbg_lvl);
  4293. static ssize_t
  4294. megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
  4295. {
  4296. return sprintf(buf, "%u\n", poll_mode_io);
  4297. }
  4298. static ssize_t
  4299. megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
  4300. const char *buf, size_t count)
  4301. {
  4302. int retval = count;
  4303. int tmp = poll_mode_io;
  4304. int i;
  4305. struct megasas_instance *instance;
  4306. if (sscanf(buf, "%u", &poll_mode_io) < 1) {
  4307. printk(KERN_ERR "megasas: could not set poll_mode_io\n");
  4308. retval = -EINVAL;
  4309. }
  4310. /*
  4311. * Check if poll_mode_io is already set or is same as previous value
  4312. */
  4313. if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
  4314. goto out;
  4315. if (poll_mode_io) {
  4316. /*
  4317. * Start timers for all adapters
  4318. */
  4319. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  4320. instance = megasas_mgmt_info.instance[i];
  4321. if (instance) {
  4322. megasas_start_timer(instance,
  4323. &instance->io_completion_timer,
  4324. megasas_io_completion_timer,
  4325. MEGASAS_COMPLETION_TIMER_INTERVAL);
  4326. }
  4327. }
  4328. } else {
  4329. /*
  4330. * Delete timers for all adapters
  4331. */
  4332. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  4333. instance = megasas_mgmt_info.instance[i];
  4334. if (instance)
  4335. del_timer_sync(&instance->io_completion_timer);
  4336. }
  4337. }
  4338. out:
  4339. return retval;
  4340. }
  4341. static void
  4342. megasas_aen_polling(struct work_struct *work)
  4343. {
  4344. struct megasas_aen_event *ev =
  4345. container_of(work, struct megasas_aen_event, hotplug_work);
  4346. struct megasas_instance *instance = ev->instance;
  4347. union megasas_evt_class_locale class_locale;
  4348. struct Scsi_Host *host;
  4349. struct scsi_device *sdev1;
  4350. u16 pd_index = 0;
  4351. u16 ld_index = 0;
  4352. int i, j, doscan = 0;
  4353. u32 seq_num;
  4354. int error;
  4355. if (!instance) {
  4356. printk(KERN_ERR "invalid instance!\n");
  4357. kfree(ev);
  4358. return;
  4359. }
  4360. instance->ev = NULL;
  4361. host = instance->host;
  4362. if (instance->evt_detail) {
  4363. switch (instance->evt_detail->code) {
  4364. case MR_EVT_PD_INSERTED:
  4365. if (megasas_get_pd_list(instance) == 0) {
  4366. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4367. for (j = 0;
  4368. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4369. j++) {
  4370. pd_index =
  4371. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4372. sdev1 =
  4373. scsi_device_lookup(host, i, j, 0);
  4374. if (instance->pd_list[pd_index].driveState
  4375. == MR_PD_STATE_SYSTEM) {
  4376. if (!sdev1) {
  4377. scsi_add_device(host, i, j, 0);
  4378. }
  4379. if (sdev1)
  4380. scsi_device_put(sdev1);
  4381. }
  4382. }
  4383. }
  4384. }
  4385. doscan = 0;
  4386. break;
  4387. case MR_EVT_PD_REMOVED:
  4388. if (megasas_get_pd_list(instance) == 0) {
  4389. megasas_get_pd_list(instance);
  4390. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4391. for (j = 0;
  4392. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4393. j++) {
  4394. pd_index =
  4395. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4396. sdev1 =
  4397. scsi_device_lookup(host, i, j, 0);
  4398. if (instance->pd_list[pd_index].driveState
  4399. == MR_PD_STATE_SYSTEM) {
  4400. if (sdev1) {
  4401. scsi_device_put(sdev1);
  4402. }
  4403. } else {
  4404. if (sdev1) {
  4405. scsi_remove_device(sdev1);
  4406. scsi_device_put(sdev1);
  4407. }
  4408. }
  4409. }
  4410. }
  4411. }
  4412. doscan = 0;
  4413. break;
  4414. case MR_EVT_LD_OFFLINE:
  4415. case MR_EVT_CFG_CLEARED:
  4416. case MR_EVT_LD_DELETED:
  4417. megasas_get_ld_list(instance);
  4418. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4419. for (j = 0;
  4420. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4421. j++) {
  4422. ld_index =
  4423. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4424. sdev1 = scsi_device_lookup(host,
  4425. i + MEGASAS_MAX_LD_CHANNELS,
  4426. j,
  4427. 0);
  4428. if (instance->ld_ids[ld_index] != 0xff) {
  4429. if (sdev1) {
  4430. scsi_device_put(sdev1);
  4431. }
  4432. } else {
  4433. if (sdev1) {
  4434. scsi_remove_device(sdev1);
  4435. scsi_device_put(sdev1);
  4436. }
  4437. }
  4438. }
  4439. }
  4440. doscan = 0;
  4441. break;
  4442. case MR_EVT_LD_CREATED:
  4443. megasas_get_ld_list(instance);
  4444. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4445. for (j = 0;
  4446. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4447. j++) {
  4448. ld_index =
  4449. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4450. sdev1 = scsi_device_lookup(host,
  4451. i+MEGASAS_MAX_LD_CHANNELS,
  4452. j, 0);
  4453. if (instance->ld_ids[ld_index] !=
  4454. 0xff) {
  4455. if (!sdev1) {
  4456. scsi_add_device(host,
  4457. i + 2,
  4458. j, 0);
  4459. }
  4460. }
  4461. if (sdev1) {
  4462. scsi_device_put(sdev1);
  4463. }
  4464. }
  4465. }
  4466. doscan = 0;
  4467. break;
  4468. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  4469. case MR_EVT_FOREIGN_CFG_IMPORTED:
  4470. case MR_EVT_LD_STATE_CHANGE:
  4471. doscan = 1;
  4472. break;
  4473. default:
  4474. doscan = 0;
  4475. break;
  4476. }
  4477. } else {
  4478. printk(KERN_ERR "invalid evt_detail!\n");
  4479. kfree(ev);
  4480. return;
  4481. }
  4482. if (doscan) {
  4483. printk(KERN_INFO "scanning ...\n");
  4484. megasas_get_pd_list(instance);
  4485. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4486. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4487. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  4488. sdev1 = scsi_device_lookup(host, i, j, 0);
  4489. if (instance->pd_list[pd_index].driveState ==
  4490. MR_PD_STATE_SYSTEM) {
  4491. if (!sdev1) {
  4492. scsi_add_device(host, i, j, 0);
  4493. }
  4494. if (sdev1)
  4495. scsi_device_put(sdev1);
  4496. } else {
  4497. if (sdev1) {
  4498. scsi_remove_device(sdev1);
  4499. scsi_device_put(sdev1);
  4500. }
  4501. }
  4502. }
  4503. }
  4504. megasas_get_ld_list(instance);
  4505. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4506. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4507. ld_index =
  4508. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4509. sdev1 = scsi_device_lookup(host,
  4510. i+MEGASAS_MAX_LD_CHANNELS, j, 0);
  4511. if (instance->ld_ids[ld_index] != 0xff) {
  4512. if (!sdev1) {
  4513. scsi_add_device(host,
  4514. i+2,
  4515. j, 0);
  4516. } else {
  4517. scsi_device_put(sdev1);
  4518. }
  4519. } else {
  4520. if (sdev1) {
  4521. scsi_remove_device(sdev1);
  4522. scsi_device_put(sdev1);
  4523. }
  4524. }
  4525. }
  4526. }
  4527. }
  4528. if ( instance->aen_cmd != NULL ) {
  4529. kfree(ev);
  4530. return ;
  4531. }
  4532. seq_num = instance->evt_detail->seq_num + 1;
  4533. /* Register AEN with FW for latest sequence number plus 1 */
  4534. class_locale.members.reserved = 0;
  4535. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  4536. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  4537. mutex_lock(&instance->aen_mutex);
  4538. error = megasas_register_aen(instance, seq_num,
  4539. class_locale.word);
  4540. mutex_unlock(&instance->aen_mutex);
  4541. if (error)
  4542. printk(KERN_ERR "register aen failed error %x\n", error);
  4543. kfree(ev);
  4544. }
  4545. static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
  4546. megasas_sysfs_show_poll_mode_io,
  4547. megasas_sysfs_set_poll_mode_io);
  4548. /**
  4549. * megasas_init - Driver load entry point
  4550. */
  4551. static int __init megasas_init(void)
  4552. {
  4553. int rval;
  4554. /*
  4555. * Announce driver version and other information
  4556. */
  4557. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  4558. MEGASAS_EXT_VERSION);
  4559. support_poll_for_event = 2;
  4560. support_device_change = 1;
  4561. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  4562. /*
  4563. * Register character device node
  4564. */
  4565. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  4566. if (rval < 0) {
  4567. printk(KERN_DEBUG "megasas: failed to open device node\n");
  4568. return rval;
  4569. }
  4570. megasas_mgmt_majorno = rval;
  4571. /*
  4572. * Register ourselves as PCI hotplug module
  4573. */
  4574. rval = pci_register_driver(&megasas_pci_driver);
  4575. if (rval) {
  4576. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  4577. goto err_pcidrv;
  4578. }
  4579. rval = driver_create_file(&megasas_pci_driver.driver,
  4580. &driver_attr_version);
  4581. if (rval)
  4582. goto err_dcf_attr_ver;
  4583. rval = driver_create_file(&megasas_pci_driver.driver,
  4584. &driver_attr_release_date);
  4585. if (rval)
  4586. goto err_dcf_rel_date;
  4587. rval = driver_create_file(&megasas_pci_driver.driver,
  4588. &driver_attr_support_poll_for_event);
  4589. if (rval)
  4590. goto err_dcf_support_poll_for_event;
  4591. rval = driver_create_file(&megasas_pci_driver.driver,
  4592. &driver_attr_dbg_lvl);
  4593. if (rval)
  4594. goto err_dcf_dbg_lvl;
  4595. rval = driver_create_file(&megasas_pci_driver.driver,
  4596. &driver_attr_poll_mode_io);
  4597. if (rval)
  4598. goto err_dcf_poll_mode_io;
  4599. rval = driver_create_file(&megasas_pci_driver.driver,
  4600. &driver_attr_support_device_change);
  4601. if (rval)
  4602. goto err_dcf_support_device_change;
  4603. return rval;
  4604. err_dcf_support_device_change:
  4605. driver_remove_file(&megasas_pci_driver.driver,
  4606. &driver_attr_poll_mode_io);
  4607. err_dcf_poll_mode_io:
  4608. driver_remove_file(&megasas_pci_driver.driver,
  4609. &driver_attr_dbg_lvl);
  4610. err_dcf_dbg_lvl:
  4611. driver_remove_file(&megasas_pci_driver.driver,
  4612. &driver_attr_support_poll_for_event);
  4613. err_dcf_support_poll_for_event:
  4614. driver_remove_file(&megasas_pci_driver.driver,
  4615. &driver_attr_release_date);
  4616. err_dcf_rel_date:
  4617. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4618. err_dcf_attr_ver:
  4619. pci_unregister_driver(&megasas_pci_driver);
  4620. err_pcidrv:
  4621. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4622. return rval;
  4623. }
  4624. /**
  4625. * megasas_exit - Driver unload entry point
  4626. */
  4627. static void __exit megasas_exit(void)
  4628. {
  4629. driver_remove_file(&megasas_pci_driver.driver,
  4630. &driver_attr_poll_mode_io);
  4631. driver_remove_file(&megasas_pci_driver.driver,
  4632. &driver_attr_dbg_lvl);
  4633. driver_remove_file(&megasas_pci_driver.driver,
  4634. &driver_attr_support_poll_for_event);
  4635. driver_remove_file(&megasas_pci_driver.driver,
  4636. &driver_attr_support_device_change);
  4637. driver_remove_file(&megasas_pci_driver.driver,
  4638. &driver_attr_release_date);
  4639. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4640. pci_unregister_driver(&megasas_pci_driver);
  4641. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4642. }
  4643. module_init(megasas_init);
  4644. module_exit(megasas_exit);