megaraid_sas_base.c 196 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2003-2013 LSI Corporation
  5. * Copyright (c) 2013-2014 Avago Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Authors: Avago Technologies
  21. * Sreenivas Bagalkote
  22. * Sumant Patro
  23. * Bo Yang
  24. * Adam Radford
  25. * Kashyap Desai <kashyap.desai@avagotech.com>
  26. * Sumit Saxena <sumit.saxena@avagotech.com>
  27. *
  28. * Send feedback to: megaraidlinux.pdl@avagotech.com
  29. *
  30. * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
  31. * San Jose, California 95131
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/types.h>
  35. #include <linux/pci.h>
  36. #include <linux/list.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/module.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/delay.h>
  42. #include <linux/uio.h>
  43. #include <linux/slab.h>
  44. #include <asm/uaccess.h>
  45. #include <linux/fs.h>
  46. #include <linux/compat.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/mutex.h>
  49. #include <linux/poll.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_cmnd.h>
  52. #include <scsi/scsi_device.h>
  53. #include <scsi/scsi_host.h>
  54. #include <scsi/scsi_tcq.h>
  55. #include "megaraid_sas_fusion.h"
  56. #include "megaraid_sas.h"
  57. /*
  58. * Number of sectors per IO command
  59. * Will be set in megasas_init_mfi if user does not provide
  60. */
  61. static unsigned int max_sectors;
  62. module_param_named(max_sectors, max_sectors, int, 0);
  63. MODULE_PARM_DESC(max_sectors,
  64. "Maximum number of sectors per IO command");
  65. static int msix_disable;
  66. module_param(msix_disable, int, S_IRUGO);
  67. MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
  68. static unsigned int msix_vectors;
  69. module_param(msix_vectors, int, S_IRUGO);
  70. MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
  71. static int allow_vf_ioctls;
  72. module_param(allow_vf_ioctls, int, S_IRUGO);
  73. MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
  74. static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
  75. module_param(throttlequeuedepth, int, S_IRUGO);
  76. MODULE_PARM_DESC(throttlequeuedepth,
  77. "Adapter queue depth when throttled due to I/O timeout. Default: 16");
  78. unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
  79. module_param(resetwaittime, int, S_IRUGO);
  80. MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
  81. "before resetting adapter. Default: 180");
  82. int smp_affinity_enable = 1;
  83. module_param(smp_affinity_enable, int, S_IRUGO);
  84. MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
  85. int rdpq_enable = 1;
  86. module_param(rdpq_enable, int, S_IRUGO);
  87. MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
  88. unsigned int dual_qdepth_disable;
  89. module_param(dual_qdepth_disable, int, S_IRUGO);
  90. MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
  91. unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
  92. module_param(scmd_timeout, int, S_IRUGO);
  93. MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
  94. MODULE_LICENSE("GPL");
  95. MODULE_VERSION(MEGASAS_VERSION);
  96. MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
  97. MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
  98. int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  99. static int megasas_get_pd_list(struct megasas_instance *instance);
  100. static int megasas_ld_list_query(struct megasas_instance *instance,
  101. u8 query_type);
  102. static int megasas_issue_init_mfi(struct megasas_instance *instance);
  103. static int megasas_register_aen(struct megasas_instance *instance,
  104. u32 seq_num, u32 class_locale_word);
  105. static int
  106. megasas_get_pd_info(struct megasas_instance *instance, u16 device_id);
  107. /*
  108. * PCI ID table for all supported controllers
  109. */
  110. static struct pci_device_id megasas_pci_table[] = {
  111. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
  112. /* xscale IOP */
  113. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
  114. /* ppc IOP */
  115. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
  116. /* ppc IOP */
  117. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
  118. /* gen2*/
  119. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
  120. /* gen2*/
  121. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
  122. /* skinny*/
  123. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
  124. /* skinny*/
  125. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
  126. /* xscale IOP, vega */
  127. {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
  128. /* xscale IOP */
  129. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
  130. /* Fusion */
  131. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
  132. /* Plasma */
  133. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
  134. /* Invader */
  135. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
  136. /* Fury */
  137. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
  138. /* Intruder */
  139. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
  140. /* Intruder 24 port*/
  141. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
  142. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
  143. {}
  144. };
  145. MODULE_DEVICE_TABLE(pci, megasas_pci_table);
  146. static int megasas_mgmt_majorno;
  147. struct megasas_mgmt_info megasas_mgmt_info;
  148. static struct fasync_struct *megasas_async_queue;
  149. static DEFINE_MUTEX(megasas_async_queue_mutex);
  150. static int megasas_poll_wait_aen;
  151. static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
  152. static u32 support_poll_for_event;
  153. u32 megasas_dbg_lvl;
  154. static u32 support_device_change;
  155. /* define lock for aen poll */
  156. spinlock_t poll_aen_lock;
  157. void
  158. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  159. u8 alt_status);
  160. static u32
  161. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
  162. static int
  163. megasas_adp_reset_gen2(struct megasas_instance *instance,
  164. struct megasas_register_set __iomem *reg_set);
  165. static irqreturn_t megasas_isr(int irq, void *devp);
  166. static u32
  167. megasas_init_adapter_mfi(struct megasas_instance *instance);
  168. u32
  169. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  170. struct scsi_cmnd *scmd);
  171. static void megasas_complete_cmd_dpc(unsigned long instance_addr);
  172. int
  173. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  174. int seconds);
  175. void megasas_fusion_ocr_wq(struct work_struct *work);
  176. static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
  177. int initial);
  178. int
  179. megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  180. {
  181. instance->instancet->fire_cmd(instance,
  182. cmd->frame_phys_addr, 0, instance->reg_set);
  183. return 0;
  184. }
  185. /**
  186. * megasas_get_cmd - Get a command from the free pool
  187. * @instance: Adapter soft state
  188. *
  189. * Returns a free command from the pool
  190. */
  191. struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  192. *instance)
  193. {
  194. unsigned long flags;
  195. struct megasas_cmd *cmd = NULL;
  196. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  197. if (!list_empty(&instance->cmd_pool)) {
  198. cmd = list_entry((&instance->cmd_pool)->next,
  199. struct megasas_cmd, list);
  200. list_del_init(&cmd->list);
  201. } else {
  202. dev_err(&instance->pdev->dev, "Command pool empty!\n");
  203. }
  204. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  205. return cmd;
  206. }
  207. /**
  208. * megasas_return_cmd - Return a cmd to free command pool
  209. * @instance: Adapter soft state
  210. * @cmd: Command packet to be returned to free command pool
  211. */
  212. inline void
  213. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  214. {
  215. unsigned long flags;
  216. u32 blk_tags;
  217. struct megasas_cmd_fusion *cmd_fusion;
  218. struct fusion_context *fusion = instance->ctrl_context;
  219. /* This flag is used only for fusion adapter.
  220. * Wait for Interrupt for Polled mode DCMD
  221. */
  222. if (cmd->flags & DRV_DCMD_POLLED_MODE)
  223. return;
  224. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  225. if (fusion) {
  226. blk_tags = instance->max_scsi_cmds + cmd->index;
  227. cmd_fusion = fusion->cmd_list[blk_tags];
  228. megasas_return_cmd_fusion(instance, cmd_fusion);
  229. }
  230. cmd->scmd = NULL;
  231. cmd->frame_count = 0;
  232. cmd->flags = 0;
  233. if (!fusion && reset_devices)
  234. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  235. list_add(&cmd->list, (&instance->cmd_pool)->next);
  236. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  237. }
  238. static const char *
  239. format_timestamp(uint32_t timestamp)
  240. {
  241. static char buffer[32];
  242. if ((timestamp & 0xff000000) == 0xff000000)
  243. snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
  244. 0x00ffffff);
  245. else
  246. snprintf(buffer, sizeof(buffer), "%us", timestamp);
  247. return buffer;
  248. }
  249. static const char *
  250. format_class(int8_t class)
  251. {
  252. static char buffer[6];
  253. switch (class) {
  254. case MFI_EVT_CLASS_DEBUG:
  255. return "debug";
  256. case MFI_EVT_CLASS_PROGRESS:
  257. return "progress";
  258. case MFI_EVT_CLASS_INFO:
  259. return "info";
  260. case MFI_EVT_CLASS_WARNING:
  261. return "WARN";
  262. case MFI_EVT_CLASS_CRITICAL:
  263. return "CRIT";
  264. case MFI_EVT_CLASS_FATAL:
  265. return "FATAL";
  266. case MFI_EVT_CLASS_DEAD:
  267. return "DEAD";
  268. default:
  269. snprintf(buffer, sizeof(buffer), "%d", class);
  270. return buffer;
  271. }
  272. }
  273. /**
  274. * megasas_decode_evt: Decode FW AEN event and print critical event
  275. * for information.
  276. * @instance: Adapter soft state
  277. */
  278. static void
  279. megasas_decode_evt(struct megasas_instance *instance)
  280. {
  281. struct megasas_evt_detail *evt_detail = instance->evt_detail;
  282. union megasas_evt_class_locale class_locale;
  283. class_locale.word = le32_to_cpu(evt_detail->cl.word);
  284. if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
  285. dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
  286. le32_to_cpu(evt_detail->seq_num),
  287. format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
  288. (class_locale.members.locale),
  289. format_class(class_locale.members.class),
  290. evt_detail->description);
  291. }
  292. /**
  293. * The following functions are defined for xscale
  294. * (deviceid : 1064R, PERC5) controllers
  295. */
  296. /**
  297. * megasas_enable_intr_xscale - Enables interrupts
  298. * @regs: MFI register set
  299. */
  300. static inline void
  301. megasas_enable_intr_xscale(struct megasas_instance *instance)
  302. {
  303. struct megasas_register_set __iomem *regs;
  304. regs = instance->reg_set;
  305. writel(0, &(regs)->outbound_intr_mask);
  306. /* Dummy readl to force pci flush */
  307. readl(&regs->outbound_intr_mask);
  308. }
  309. /**
  310. * megasas_disable_intr_xscale -Disables interrupt
  311. * @regs: MFI register set
  312. */
  313. static inline void
  314. megasas_disable_intr_xscale(struct megasas_instance *instance)
  315. {
  316. struct megasas_register_set __iomem *regs;
  317. u32 mask = 0x1f;
  318. regs = instance->reg_set;
  319. writel(mask, &regs->outbound_intr_mask);
  320. /* Dummy readl to force pci flush */
  321. readl(&regs->outbound_intr_mask);
  322. }
  323. /**
  324. * megasas_read_fw_status_reg_xscale - returns the current FW status value
  325. * @regs: MFI register set
  326. */
  327. static u32
  328. megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
  329. {
  330. return readl(&(regs)->outbound_msg_0);
  331. }
  332. /**
  333. * megasas_clear_interrupt_xscale - Check & clear interrupt
  334. * @regs: MFI register set
  335. */
  336. static int
  337. megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
  338. {
  339. u32 status;
  340. u32 mfiStatus = 0;
  341. /*
  342. * Check if it is our interrupt
  343. */
  344. status = readl(&regs->outbound_intr_status);
  345. if (status & MFI_OB_INTR_STATUS_MASK)
  346. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  347. if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
  348. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  349. /*
  350. * Clear the interrupt by writing back the same value
  351. */
  352. if (mfiStatus)
  353. writel(status, &regs->outbound_intr_status);
  354. /* Dummy readl to force pci flush */
  355. readl(&regs->outbound_intr_status);
  356. return mfiStatus;
  357. }
  358. /**
  359. * megasas_fire_cmd_xscale - Sends command to the FW
  360. * @frame_phys_addr : Physical address of cmd
  361. * @frame_count : Number of frames for the command
  362. * @regs : MFI register set
  363. */
  364. static inline void
  365. megasas_fire_cmd_xscale(struct megasas_instance *instance,
  366. dma_addr_t frame_phys_addr,
  367. u32 frame_count,
  368. struct megasas_register_set __iomem *regs)
  369. {
  370. unsigned long flags;
  371. spin_lock_irqsave(&instance->hba_lock, flags);
  372. writel((frame_phys_addr >> 3)|(frame_count),
  373. &(regs)->inbound_queue_port);
  374. spin_unlock_irqrestore(&instance->hba_lock, flags);
  375. }
  376. /**
  377. * megasas_adp_reset_xscale - For controller reset
  378. * @regs: MFI register set
  379. */
  380. static int
  381. megasas_adp_reset_xscale(struct megasas_instance *instance,
  382. struct megasas_register_set __iomem *regs)
  383. {
  384. u32 i;
  385. u32 pcidata;
  386. writel(MFI_ADP_RESET, &regs->inbound_doorbell);
  387. for (i = 0; i < 3; i++)
  388. msleep(1000); /* sleep for 3 secs */
  389. pcidata = 0;
  390. pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
  391. dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
  392. if (pcidata & 0x2) {
  393. dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
  394. pcidata &= ~0x2;
  395. pci_write_config_dword(instance->pdev,
  396. MFI_1068_PCSR_OFFSET, pcidata);
  397. for (i = 0; i < 2; i++)
  398. msleep(1000); /* need to wait 2 secs again */
  399. pcidata = 0;
  400. pci_read_config_dword(instance->pdev,
  401. MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
  402. dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
  403. if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
  404. dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
  405. pcidata = 0;
  406. pci_write_config_dword(instance->pdev,
  407. MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
  408. }
  409. }
  410. return 0;
  411. }
  412. /**
  413. * megasas_check_reset_xscale - For controller reset check
  414. * @regs: MFI register set
  415. */
  416. static int
  417. megasas_check_reset_xscale(struct megasas_instance *instance,
  418. struct megasas_register_set __iomem *regs)
  419. {
  420. if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
  421. (le32_to_cpu(*instance->consumer) ==
  422. MEGASAS_ADPRESET_INPROG_SIGN))
  423. return 1;
  424. return 0;
  425. }
  426. static struct megasas_instance_template megasas_instance_template_xscale = {
  427. .fire_cmd = megasas_fire_cmd_xscale,
  428. .enable_intr = megasas_enable_intr_xscale,
  429. .disable_intr = megasas_disable_intr_xscale,
  430. .clear_intr = megasas_clear_intr_xscale,
  431. .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
  432. .adp_reset = megasas_adp_reset_xscale,
  433. .check_reset = megasas_check_reset_xscale,
  434. .service_isr = megasas_isr,
  435. .tasklet = megasas_complete_cmd_dpc,
  436. .init_adapter = megasas_init_adapter_mfi,
  437. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  438. .issue_dcmd = megasas_issue_dcmd,
  439. };
  440. /**
  441. * This is the end of set of functions & definitions specific
  442. * to xscale (deviceid : 1064R, PERC5) controllers
  443. */
  444. /**
  445. * The following functions are defined for ppc (deviceid : 0x60)
  446. * controllers
  447. */
  448. /**
  449. * megasas_enable_intr_ppc - Enables interrupts
  450. * @regs: MFI register set
  451. */
  452. static inline void
  453. megasas_enable_intr_ppc(struct megasas_instance *instance)
  454. {
  455. struct megasas_register_set __iomem *regs;
  456. regs = instance->reg_set;
  457. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  458. writel(~0x80000000, &(regs)->outbound_intr_mask);
  459. /* Dummy readl to force pci flush */
  460. readl(&regs->outbound_intr_mask);
  461. }
  462. /**
  463. * megasas_disable_intr_ppc - Disable interrupt
  464. * @regs: MFI register set
  465. */
  466. static inline void
  467. megasas_disable_intr_ppc(struct megasas_instance *instance)
  468. {
  469. struct megasas_register_set __iomem *regs;
  470. u32 mask = 0xFFFFFFFF;
  471. regs = instance->reg_set;
  472. writel(mask, &regs->outbound_intr_mask);
  473. /* Dummy readl to force pci flush */
  474. readl(&regs->outbound_intr_mask);
  475. }
  476. /**
  477. * megasas_read_fw_status_reg_ppc - returns the current FW status value
  478. * @regs: MFI register set
  479. */
  480. static u32
  481. megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
  482. {
  483. return readl(&(regs)->outbound_scratch_pad);
  484. }
  485. /**
  486. * megasas_clear_interrupt_ppc - Check & clear interrupt
  487. * @regs: MFI register set
  488. */
  489. static int
  490. megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
  491. {
  492. u32 status, mfiStatus = 0;
  493. /*
  494. * Check if it is our interrupt
  495. */
  496. status = readl(&regs->outbound_intr_status);
  497. if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
  498. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  499. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
  500. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  501. /*
  502. * Clear the interrupt by writing back the same value
  503. */
  504. writel(status, &regs->outbound_doorbell_clear);
  505. /* Dummy readl to force pci flush */
  506. readl(&regs->outbound_doorbell_clear);
  507. return mfiStatus;
  508. }
  509. /**
  510. * megasas_fire_cmd_ppc - Sends command to the FW
  511. * @frame_phys_addr : Physical address of cmd
  512. * @frame_count : Number of frames for the command
  513. * @regs : MFI register set
  514. */
  515. static inline void
  516. megasas_fire_cmd_ppc(struct megasas_instance *instance,
  517. dma_addr_t frame_phys_addr,
  518. u32 frame_count,
  519. struct megasas_register_set __iomem *regs)
  520. {
  521. unsigned long flags;
  522. spin_lock_irqsave(&instance->hba_lock, flags);
  523. writel((frame_phys_addr | (frame_count<<1))|1,
  524. &(regs)->inbound_queue_port);
  525. spin_unlock_irqrestore(&instance->hba_lock, flags);
  526. }
  527. /**
  528. * megasas_check_reset_ppc - For controller reset check
  529. * @regs: MFI register set
  530. */
  531. static int
  532. megasas_check_reset_ppc(struct megasas_instance *instance,
  533. struct megasas_register_set __iomem *regs)
  534. {
  535. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  536. return 1;
  537. return 0;
  538. }
  539. static struct megasas_instance_template megasas_instance_template_ppc = {
  540. .fire_cmd = megasas_fire_cmd_ppc,
  541. .enable_intr = megasas_enable_intr_ppc,
  542. .disable_intr = megasas_disable_intr_ppc,
  543. .clear_intr = megasas_clear_intr_ppc,
  544. .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
  545. .adp_reset = megasas_adp_reset_xscale,
  546. .check_reset = megasas_check_reset_ppc,
  547. .service_isr = megasas_isr,
  548. .tasklet = megasas_complete_cmd_dpc,
  549. .init_adapter = megasas_init_adapter_mfi,
  550. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  551. .issue_dcmd = megasas_issue_dcmd,
  552. };
  553. /**
  554. * megasas_enable_intr_skinny - Enables interrupts
  555. * @regs: MFI register set
  556. */
  557. static inline void
  558. megasas_enable_intr_skinny(struct megasas_instance *instance)
  559. {
  560. struct megasas_register_set __iomem *regs;
  561. regs = instance->reg_set;
  562. writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
  563. writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  564. /* Dummy readl to force pci flush */
  565. readl(&regs->outbound_intr_mask);
  566. }
  567. /**
  568. * megasas_disable_intr_skinny - Disables interrupt
  569. * @regs: MFI register set
  570. */
  571. static inline void
  572. megasas_disable_intr_skinny(struct megasas_instance *instance)
  573. {
  574. struct megasas_register_set __iomem *regs;
  575. u32 mask = 0xFFFFFFFF;
  576. regs = instance->reg_set;
  577. writel(mask, &regs->outbound_intr_mask);
  578. /* Dummy readl to force pci flush */
  579. readl(&regs->outbound_intr_mask);
  580. }
  581. /**
  582. * megasas_read_fw_status_reg_skinny - returns the current FW status value
  583. * @regs: MFI register set
  584. */
  585. static u32
  586. megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
  587. {
  588. return readl(&(regs)->outbound_scratch_pad);
  589. }
  590. /**
  591. * megasas_clear_interrupt_skinny - Check & clear interrupt
  592. * @regs: MFI register set
  593. */
  594. static int
  595. megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
  596. {
  597. u32 status;
  598. u32 mfiStatus = 0;
  599. /*
  600. * Check if it is our interrupt
  601. */
  602. status = readl(&regs->outbound_intr_status);
  603. if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
  604. return 0;
  605. }
  606. /*
  607. * Check if it is our interrupt
  608. */
  609. if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
  610. MFI_STATE_FAULT) {
  611. mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  612. } else
  613. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  614. /*
  615. * Clear the interrupt by writing back the same value
  616. */
  617. writel(status, &regs->outbound_intr_status);
  618. /*
  619. * dummy read to flush PCI
  620. */
  621. readl(&regs->outbound_intr_status);
  622. return mfiStatus;
  623. }
  624. /**
  625. * megasas_fire_cmd_skinny - Sends command to the FW
  626. * @frame_phys_addr : Physical address of cmd
  627. * @frame_count : Number of frames for the command
  628. * @regs : MFI register set
  629. */
  630. static inline void
  631. megasas_fire_cmd_skinny(struct megasas_instance *instance,
  632. dma_addr_t frame_phys_addr,
  633. u32 frame_count,
  634. struct megasas_register_set __iomem *regs)
  635. {
  636. unsigned long flags;
  637. spin_lock_irqsave(&instance->hba_lock, flags);
  638. writel(upper_32_bits(frame_phys_addr),
  639. &(regs)->inbound_high_queue_port);
  640. writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
  641. &(regs)->inbound_low_queue_port);
  642. mmiowb();
  643. spin_unlock_irqrestore(&instance->hba_lock, flags);
  644. }
  645. /**
  646. * megasas_check_reset_skinny - For controller reset check
  647. * @regs: MFI register set
  648. */
  649. static int
  650. megasas_check_reset_skinny(struct megasas_instance *instance,
  651. struct megasas_register_set __iomem *regs)
  652. {
  653. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  654. return 1;
  655. return 0;
  656. }
  657. static struct megasas_instance_template megasas_instance_template_skinny = {
  658. .fire_cmd = megasas_fire_cmd_skinny,
  659. .enable_intr = megasas_enable_intr_skinny,
  660. .disable_intr = megasas_disable_intr_skinny,
  661. .clear_intr = megasas_clear_intr_skinny,
  662. .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
  663. .adp_reset = megasas_adp_reset_gen2,
  664. .check_reset = megasas_check_reset_skinny,
  665. .service_isr = megasas_isr,
  666. .tasklet = megasas_complete_cmd_dpc,
  667. .init_adapter = megasas_init_adapter_mfi,
  668. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  669. .issue_dcmd = megasas_issue_dcmd,
  670. };
  671. /**
  672. * The following functions are defined for gen2 (deviceid : 0x78 0x79)
  673. * controllers
  674. */
  675. /**
  676. * megasas_enable_intr_gen2 - Enables interrupts
  677. * @regs: MFI register set
  678. */
  679. static inline void
  680. megasas_enable_intr_gen2(struct megasas_instance *instance)
  681. {
  682. struct megasas_register_set __iomem *regs;
  683. regs = instance->reg_set;
  684. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  685. /* write ~0x00000005 (4 & 1) to the intr mask*/
  686. writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  687. /* Dummy readl to force pci flush */
  688. readl(&regs->outbound_intr_mask);
  689. }
  690. /**
  691. * megasas_disable_intr_gen2 - Disables interrupt
  692. * @regs: MFI register set
  693. */
  694. static inline void
  695. megasas_disable_intr_gen2(struct megasas_instance *instance)
  696. {
  697. struct megasas_register_set __iomem *regs;
  698. u32 mask = 0xFFFFFFFF;
  699. regs = instance->reg_set;
  700. writel(mask, &regs->outbound_intr_mask);
  701. /* Dummy readl to force pci flush */
  702. readl(&regs->outbound_intr_mask);
  703. }
  704. /**
  705. * megasas_read_fw_status_reg_gen2 - returns the current FW status value
  706. * @regs: MFI register set
  707. */
  708. static u32
  709. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
  710. {
  711. return readl(&(regs)->outbound_scratch_pad);
  712. }
  713. /**
  714. * megasas_clear_interrupt_gen2 - Check & clear interrupt
  715. * @regs: MFI register set
  716. */
  717. static int
  718. megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
  719. {
  720. u32 status;
  721. u32 mfiStatus = 0;
  722. /*
  723. * Check if it is our interrupt
  724. */
  725. status = readl(&regs->outbound_intr_status);
  726. if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
  727. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  728. }
  729. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
  730. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  731. }
  732. /*
  733. * Clear the interrupt by writing back the same value
  734. */
  735. if (mfiStatus)
  736. writel(status, &regs->outbound_doorbell_clear);
  737. /* Dummy readl to force pci flush */
  738. readl(&regs->outbound_intr_status);
  739. return mfiStatus;
  740. }
  741. /**
  742. * megasas_fire_cmd_gen2 - Sends command to the FW
  743. * @frame_phys_addr : Physical address of cmd
  744. * @frame_count : Number of frames for the command
  745. * @regs : MFI register set
  746. */
  747. static inline void
  748. megasas_fire_cmd_gen2(struct megasas_instance *instance,
  749. dma_addr_t frame_phys_addr,
  750. u32 frame_count,
  751. struct megasas_register_set __iomem *regs)
  752. {
  753. unsigned long flags;
  754. spin_lock_irqsave(&instance->hba_lock, flags);
  755. writel((frame_phys_addr | (frame_count<<1))|1,
  756. &(regs)->inbound_queue_port);
  757. spin_unlock_irqrestore(&instance->hba_lock, flags);
  758. }
  759. /**
  760. * megasas_adp_reset_gen2 - For controller reset
  761. * @regs: MFI register set
  762. */
  763. static int
  764. megasas_adp_reset_gen2(struct megasas_instance *instance,
  765. struct megasas_register_set __iomem *reg_set)
  766. {
  767. u32 retry = 0 ;
  768. u32 HostDiag;
  769. u32 __iomem *seq_offset = &reg_set->seq_offset;
  770. u32 __iomem *hostdiag_offset = &reg_set->host_diag;
  771. if (instance->instancet == &megasas_instance_template_skinny) {
  772. seq_offset = &reg_set->fusion_seq_offset;
  773. hostdiag_offset = &reg_set->fusion_host_diag;
  774. }
  775. writel(0, seq_offset);
  776. writel(4, seq_offset);
  777. writel(0xb, seq_offset);
  778. writel(2, seq_offset);
  779. writel(7, seq_offset);
  780. writel(0xd, seq_offset);
  781. msleep(1000);
  782. HostDiag = (u32)readl(hostdiag_offset);
  783. while (!(HostDiag & DIAG_WRITE_ENABLE)) {
  784. msleep(100);
  785. HostDiag = (u32)readl(hostdiag_offset);
  786. dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
  787. retry, HostDiag);
  788. if (retry++ >= 100)
  789. return 1;
  790. }
  791. dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
  792. writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
  793. ssleep(10);
  794. HostDiag = (u32)readl(hostdiag_offset);
  795. while (HostDiag & DIAG_RESET_ADAPTER) {
  796. msleep(100);
  797. HostDiag = (u32)readl(hostdiag_offset);
  798. dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
  799. retry, HostDiag);
  800. if (retry++ >= 1000)
  801. return 1;
  802. }
  803. return 0;
  804. }
  805. /**
  806. * megasas_check_reset_gen2 - For controller reset check
  807. * @regs: MFI register set
  808. */
  809. static int
  810. megasas_check_reset_gen2(struct megasas_instance *instance,
  811. struct megasas_register_set __iomem *regs)
  812. {
  813. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  814. return 1;
  815. return 0;
  816. }
  817. static struct megasas_instance_template megasas_instance_template_gen2 = {
  818. .fire_cmd = megasas_fire_cmd_gen2,
  819. .enable_intr = megasas_enable_intr_gen2,
  820. .disable_intr = megasas_disable_intr_gen2,
  821. .clear_intr = megasas_clear_intr_gen2,
  822. .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
  823. .adp_reset = megasas_adp_reset_gen2,
  824. .check_reset = megasas_check_reset_gen2,
  825. .service_isr = megasas_isr,
  826. .tasklet = megasas_complete_cmd_dpc,
  827. .init_adapter = megasas_init_adapter_mfi,
  828. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  829. .issue_dcmd = megasas_issue_dcmd,
  830. };
  831. /**
  832. * This is the end of set of functions & definitions
  833. * specific to gen2 (deviceid : 0x78, 0x79) controllers
  834. */
  835. /*
  836. * Template added for TB (Fusion)
  837. */
  838. extern struct megasas_instance_template megasas_instance_template_fusion;
  839. /**
  840. * megasas_issue_polled - Issues a polling command
  841. * @instance: Adapter soft state
  842. * @cmd: Command packet to be issued
  843. *
  844. * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
  845. */
  846. int
  847. megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
  848. {
  849. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  850. frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
  851. frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
  852. if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
  853. (instance->instancet->issue_dcmd(instance, cmd))) {
  854. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  855. __func__, __LINE__);
  856. return DCMD_NOT_FIRED;
  857. }
  858. return wait_and_poll(instance, cmd, instance->requestorId ?
  859. MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
  860. }
  861. /**
  862. * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
  863. * @instance: Adapter soft state
  864. * @cmd: Command to be issued
  865. * @timeout: Timeout in seconds
  866. *
  867. * This function waits on an event for the command to be returned from ISR.
  868. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  869. * Used to issue ioctl commands.
  870. */
  871. int
  872. megasas_issue_blocked_cmd(struct megasas_instance *instance,
  873. struct megasas_cmd *cmd, int timeout)
  874. {
  875. int ret = 0;
  876. cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  877. if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
  878. (instance->instancet->issue_dcmd(instance, cmd))) {
  879. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  880. __func__, __LINE__);
  881. return DCMD_NOT_FIRED;
  882. }
  883. if (timeout) {
  884. ret = wait_event_timeout(instance->int_cmd_wait_q,
  885. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
  886. if (!ret) {
  887. dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
  888. __func__, __LINE__);
  889. return DCMD_TIMEOUT;
  890. }
  891. } else
  892. wait_event(instance->int_cmd_wait_q,
  893. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
  894. return (cmd->cmd_status_drv == MFI_STAT_OK) ?
  895. DCMD_SUCCESS : DCMD_FAILED;
  896. }
  897. /**
  898. * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
  899. * @instance: Adapter soft state
  900. * @cmd_to_abort: Previously issued cmd to be aborted
  901. * @timeout: Timeout in seconds
  902. *
  903. * MFI firmware can abort previously issued AEN comamnd (automatic event
  904. * notification). The megasas_issue_blocked_abort_cmd() issues such abort
  905. * cmd and waits for return status.
  906. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  907. */
  908. static int
  909. megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
  910. struct megasas_cmd *cmd_to_abort, int timeout)
  911. {
  912. struct megasas_cmd *cmd;
  913. struct megasas_abort_frame *abort_fr;
  914. int ret = 0;
  915. cmd = megasas_get_cmd(instance);
  916. if (!cmd)
  917. return -1;
  918. abort_fr = &cmd->frame->abort;
  919. /*
  920. * Prepare and issue the abort frame
  921. */
  922. abort_fr->cmd = MFI_CMD_ABORT;
  923. abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
  924. abort_fr->flags = cpu_to_le16(0);
  925. abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
  926. abort_fr->abort_mfi_phys_addr_lo =
  927. cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
  928. abort_fr->abort_mfi_phys_addr_hi =
  929. cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
  930. cmd->sync_cmd = 1;
  931. cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  932. if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
  933. (instance->instancet->issue_dcmd(instance, cmd))) {
  934. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  935. __func__, __LINE__);
  936. return DCMD_NOT_FIRED;
  937. }
  938. if (timeout) {
  939. ret = wait_event_timeout(instance->abort_cmd_wait_q,
  940. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
  941. if (!ret) {
  942. dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
  943. __func__, __LINE__);
  944. return DCMD_TIMEOUT;
  945. }
  946. } else
  947. wait_event(instance->abort_cmd_wait_q,
  948. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
  949. cmd->sync_cmd = 0;
  950. megasas_return_cmd(instance, cmd);
  951. return (cmd->cmd_status_drv == MFI_STAT_OK) ?
  952. DCMD_SUCCESS : DCMD_FAILED;
  953. }
  954. /**
  955. * megasas_make_sgl32 - Prepares 32-bit SGL
  956. * @instance: Adapter soft state
  957. * @scp: SCSI command from the mid-layer
  958. * @mfi_sgl: SGL to be filled in
  959. *
  960. * If successful, this function returns the number of SG elements. Otherwise,
  961. * it returnes -1.
  962. */
  963. static int
  964. megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
  965. union megasas_sgl *mfi_sgl)
  966. {
  967. int i;
  968. int sge_count;
  969. struct scatterlist *os_sgl;
  970. sge_count = scsi_dma_map(scp);
  971. BUG_ON(sge_count < 0);
  972. if (sge_count) {
  973. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  974. mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
  975. mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
  976. }
  977. }
  978. return sge_count;
  979. }
  980. /**
  981. * megasas_make_sgl64 - Prepares 64-bit SGL
  982. * @instance: Adapter soft state
  983. * @scp: SCSI command from the mid-layer
  984. * @mfi_sgl: SGL to be filled in
  985. *
  986. * If successful, this function returns the number of SG elements. Otherwise,
  987. * it returnes -1.
  988. */
  989. static int
  990. megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
  991. union megasas_sgl *mfi_sgl)
  992. {
  993. int i;
  994. int sge_count;
  995. struct scatterlist *os_sgl;
  996. sge_count = scsi_dma_map(scp);
  997. BUG_ON(sge_count < 0);
  998. if (sge_count) {
  999. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1000. mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
  1001. mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
  1002. }
  1003. }
  1004. return sge_count;
  1005. }
  1006. /**
  1007. * megasas_make_sgl_skinny - Prepares IEEE SGL
  1008. * @instance: Adapter soft state
  1009. * @scp: SCSI command from the mid-layer
  1010. * @mfi_sgl: SGL to be filled in
  1011. *
  1012. * If successful, this function returns the number of SG elements. Otherwise,
  1013. * it returnes -1.
  1014. */
  1015. static int
  1016. megasas_make_sgl_skinny(struct megasas_instance *instance,
  1017. struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
  1018. {
  1019. int i;
  1020. int sge_count;
  1021. struct scatterlist *os_sgl;
  1022. sge_count = scsi_dma_map(scp);
  1023. if (sge_count) {
  1024. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1025. mfi_sgl->sge_skinny[i].length =
  1026. cpu_to_le32(sg_dma_len(os_sgl));
  1027. mfi_sgl->sge_skinny[i].phys_addr =
  1028. cpu_to_le64(sg_dma_address(os_sgl));
  1029. mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
  1030. }
  1031. }
  1032. return sge_count;
  1033. }
  1034. /**
  1035. * megasas_get_frame_count - Computes the number of frames
  1036. * @frame_type : type of frame- io or pthru frame
  1037. * @sge_count : number of sg elements
  1038. *
  1039. * Returns the number of frames required for numnber of sge's (sge_count)
  1040. */
  1041. static u32 megasas_get_frame_count(struct megasas_instance *instance,
  1042. u8 sge_count, u8 frame_type)
  1043. {
  1044. int num_cnt;
  1045. int sge_bytes;
  1046. u32 sge_sz;
  1047. u32 frame_count = 0;
  1048. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1049. sizeof(struct megasas_sge32);
  1050. if (instance->flag_ieee) {
  1051. sge_sz = sizeof(struct megasas_sge_skinny);
  1052. }
  1053. /*
  1054. * Main frame can contain 2 SGEs for 64-bit SGLs and
  1055. * 3 SGEs for 32-bit SGLs for ldio &
  1056. * 1 SGEs for 64-bit SGLs and
  1057. * 2 SGEs for 32-bit SGLs for pthru frame
  1058. */
  1059. if (unlikely(frame_type == PTHRU_FRAME)) {
  1060. if (instance->flag_ieee == 1) {
  1061. num_cnt = sge_count - 1;
  1062. } else if (IS_DMA64)
  1063. num_cnt = sge_count - 1;
  1064. else
  1065. num_cnt = sge_count - 2;
  1066. } else {
  1067. if (instance->flag_ieee == 1) {
  1068. num_cnt = sge_count - 1;
  1069. } else if (IS_DMA64)
  1070. num_cnt = sge_count - 2;
  1071. else
  1072. num_cnt = sge_count - 3;
  1073. }
  1074. if (num_cnt > 0) {
  1075. sge_bytes = sge_sz * num_cnt;
  1076. frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  1077. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
  1078. }
  1079. /* Main frame */
  1080. frame_count += 1;
  1081. if (frame_count > 7)
  1082. frame_count = 8;
  1083. return frame_count;
  1084. }
  1085. /**
  1086. * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
  1087. * @instance: Adapter soft state
  1088. * @scp: SCSI command
  1089. * @cmd: Command to be prepared in
  1090. *
  1091. * This function prepares CDB commands. These are typcially pass-through
  1092. * commands to the devices.
  1093. */
  1094. static int
  1095. megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1096. struct megasas_cmd *cmd)
  1097. {
  1098. u32 is_logical;
  1099. u32 device_id;
  1100. u16 flags = 0;
  1101. struct megasas_pthru_frame *pthru;
  1102. is_logical = MEGASAS_IS_LOGICAL(scp);
  1103. device_id = MEGASAS_DEV_INDEX(scp);
  1104. pthru = (struct megasas_pthru_frame *)cmd->frame;
  1105. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1106. flags = MFI_FRAME_DIR_WRITE;
  1107. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1108. flags = MFI_FRAME_DIR_READ;
  1109. else if (scp->sc_data_direction == PCI_DMA_NONE)
  1110. flags = MFI_FRAME_DIR_NONE;
  1111. if (instance->flag_ieee == 1) {
  1112. flags |= MFI_FRAME_IEEE;
  1113. }
  1114. /*
  1115. * Prepare the DCDB frame
  1116. */
  1117. pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
  1118. pthru->cmd_status = 0x0;
  1119. pthru->scsi_status = 0x0;
  1120. pthru->target_id = device_id;
  1121. pthru->lun = scp->device->lun;
  1122. pthru->cdb_len = scp->cmd_len;
  1123. pthru->timeout = 0;
  1124. pthru->pad_0 = 0;
  1125. pthru->flags = cpu_to_le16(flags);
  1126. pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
  1127. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1128. /*
  1129. * If the command is for the tape device, set the
  1130. * pthru timeout to the os layer timeout value.
  1131. */
  1132. if (scp->device->type == TYPE_TAPE) {
  1133. if ((scp->request->timeout / HZ) > 0xFFFF)
  1134. pthru->timeout = cpu_to_le16(0xFFFF);
  1135. else
  1136. pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
  1137. }
  1138. /*
  1139. * Construct SGL
  1140. */
  1141. if (instance->flag_ieee == 1) {
  1142. pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1143. pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
  1144. &pthru->sgl);
  1145. } else if (IS_DMA64) {
  1146. pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1147. pthru->sge_count = megasas_make_sgl64(instance, scp,
  1148. &pthru->sgl);
  1149. } else
  1150. pthru->sge_count = megasas_make_sgl32(instance, scp,
  1151. &pthru->sgl);
  1152. if (pthru->sge_count > instance->max_num_sge) {
  1153. dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
  1154. pthru->sge_count);
  1155. return 0;
  1156. }
  1157. /*
  1158. * Sense info specific
  1159. */
  1160. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  1161. pthru->sense_buf_phys_addr_hi =
  1162. cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
  1163. pthru->sense_buf_phys_addr_lo =
  1164. cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
  1165. /*
  1166. * Compute the total number of frames this command consumes. FW uses
  1167. * this number to pull sufficient number of frames from host memory.
  1168. */
  1169. cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
  1170. PTHRU_FRAME);
  1171. return cmd->frame_count;
  1172. }
  1173. /**
  1174. * megasas_build_ldio - Prepares IOs to logical devices
  1175. * @instance: Adapter soft state
  1176. * @scp: SCSI command
  1177. * @cmd: Command to be prepared
  1178. *
  1179. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  1180. */
  1181. static int
  1182. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1183. struct megasas_cmd *cmd)
  1184. {
  1185. u32 device_id;
  1186. u8 sc = scp->cmnd[0];
  1187. u16 flags = 0;
  1188. struct megasas_io_frame *ldio;
  1189. device_id = MEGASAS_DEV_INDEX(scp);
  1190. ldio = (struct megasas_io_frame *)cmd->frame;
  1191. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1192. flags = MFI_FRAME_DIR_WRITE;
  1193. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1194. flags = MFI_FRAME_DIR_READ;
  1195. if (instance->flag_ieee == 1) {
  1196. flags |= MFI_FRAME_IEEE;
  1197. }
  1198. /*
  1199. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  1200. */
  1201. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  1202. ldio->cmd_status = 0x0;
  1203. ldio->scsi_status = 0x0;
  1204. ldio->target_id = device_id;
  1205. ldio->timeout = 0;
  1206. ldio->reserved_0 = 0;
  1207. ldio->pad_0 = 0;
  1208. ldio->flags = cpu_to_le16(flags);
  1209. ldio->start_lba_hi = 0;
  1210. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  1211. /*
  1212. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1213. */
  1214. if (scp->cmd_len == 6) {
  1215. ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
  1216. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
  1217. ((u32) scp->cmnd[2] << 8) |
  1218. (u32) scp->cmnd[3]);
  1219. ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
  1220. }
  1221. /*
  1222. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1223. */
  1224. else if (scp->cmd_len == 10) {
  1225. ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
  1226. ((u32) scp->cmnd[7] << 8));
  1227. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1228. ((u32) scp->cmnd[3] << 16) |
  1229. ((u32) scp->cmnd[4] << 8) |
  1230. (u32) scp->cmnd[5]);
  1231. }
  1232. /*
  1233. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1234. */
  1235. else if (scp->cmd_len == 12) {
  1236. ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
  1237. ((u32) scp->cmnd[7] << 16) |
  1238. ((u32) scp->cmnd[8] << 8) |
  1239. (u32) scp->cmnd[9]);
  1240. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1241. ((u32) scp->cmnd[3] << 16) |
  1242. ((u32) scp->cmnd[4] << 8) |
  1243. (u32) scp->cmnd[5]);
  1244. }
  1245. /*
  1246. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1247. */
  1248. else if (scp->cmd_len == 16) {
  1249. ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
  1250. ((u32) scp->cmnd[11] << 16) |
  1251. ((u32) scp->cmnd[12] << 8) |
  1252. (u32) scp->cmnd[13]);
  1253. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
  1254. ((u32) scp->cmnd[7] << 16) |
  1255. ((u32) scp->cmnd[8] << 8) |
  1256. (u32) scp->cmnd[9]);
  1257. ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1258. ((u32) scp->cmnd[3] << 16) |
  1259. ((u32) scp->cmnd[4] << 8) |
  1260. (u32) scp->cmnd[5]);
  1261. }
  1262. /*
  1263. * Construct SGL
  1264. */
  1265. if (instance->flag_ieee) {
  1266. ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1267. ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
  1268. &ldio->sgl);
  1269. } else if (IS_DMA64) {
  1270. ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1271. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  1272. } else
  1273. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  1274. if (ldio->sge_count > instance->max_num_sge) {
  1275. dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
  1276. ldio->sge_count);
  1277. return 0;
  1278. }
  1279. /*
  1280. * Sense info specific
  1281. */
  1282. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  1283. ldio->sense_buf_phys_addr_hi = 0;
  1284. ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
  1285. /*
  1286. * Compute the total number of frames this command consumes. FW uses
  1287. * this number to pull sufficient number of frames from host memory.
  1288. */
  1289. cmd->frame_count = megasas_get_frame_count(instance,
  1290. ldio->sge_count, IO_FRAME);
  1291. return cmd->frame_count;
  1292. }
  1293. /**
  1294. * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
  1295. * and whether it's RW or non RW
  1296. * @scmd: SCSI command
  1297. *
  1298. */
  1299. inline int megasas_cmd_type(struct scsi_cmnd *cmd)
  1300. {
  1301. int ret;
  1302. switch (cmd->cmnd[0]) {
  1303. case READ_10:
  1304. case WRITE_10:
  1305. case READ_12:
  1306. case WRITE_12:
  1307. case READ_6:
  1308. case WRITE_6:
  1309. case READ_16:
  1310. case WRITE_16:
  1311. ret = (MEGASAS_IS_LOGICAL(cmd)) ?
  1312. READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
  1313. break;
  1314. default:
  1315. ret = (MEGASAS_IS_LOGICAL(cmd)) ?
  1316. NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
  1317. }
  1318. return ret;
  1319. }
  1320. /**
  1321. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  1322. * in FW
  1323. * @instance: Adapter soft state
  1324. */
  1325. static inline void
  1326. megasas_dump_pending_frames(struct megasas_instance *instance)
  1327. {
  1328. struct megasas_cmd *cmd;
  1329. int i,n;
  1330. union megasas_sgl *mfi_sgl;
  1331. struct megasas_io_frame *ldio;
  1332. struct megasas_pthru_frame *pthru;
  1333. u32 sgcount;
  1334. u32 max_cmd = instance->max_fw_cmds;
  1335. dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  1336. dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  1337. if (IS_DMA64)
  1338. dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  1339. else
  1340. dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  1341. dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  1342. for (i = 0; i < max_cmd; i++) {
  1343. cmd = instance->cmd_list[i];
  1344. if (!cmd->scmd)
  1345. continue;
  1346. dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  1347. if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
  1348. ldio = (struct megasas_io_frame *)cmd->frame;
  1349. mfi_sgl = &ldio->sgl;
  1350. sgcount = ldio->sge_count;
  1351. dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
  1352. " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
  1353. instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
  1354. le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
  1355. le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
  1356. } else {
  1357. pthru = (struct megasas_pthru_frame *) cmd->frame;
  1358. mfi_sgl = &pthru->sgl;
  1359. sgcount = pthru->sge_count;
  1360. dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
  1361. "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
  1362. instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
  1363. pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
  1364. le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
  1365. }
  1366. if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
  1367. for (n = 0; n < sgcount; n++) {
  1368. if (IS_DMA64)
  1369. dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
  1370. le32_to_cpu(mfi_sgl->sge64[n].length),
  1371. le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
  1372. else
  1373. dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
  1374. le32_to_cpu(mfi_sgl->sge32[n].length),
  1375. le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
  1376. }
  1377. }
  1378. } /*for max_cmd*/
  1379. dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  1380. for (i = 0; i < max_cmd; i++) {
  1381. cmd = instance->cmd_list[i];
  1382. if (cmd->sync_cmd == 1)
  1383. dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  1384. }
  1385. dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
  1386. }
  1387. u32
  1388. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  1389. struct scsi_cmnd *scmd)
  1390. {
  1391. struct megasas_cmd *cmd;
  1392. u32 frame_count;
  1393. cmd = megasas_get_cmd(instance);
  1394. if (!cmd)
  1395. return SCSI_MLQUEUE_HOST_BUSY;
  1396. /*
  1397. * Logical drive command
  1398. */
  1399. if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
  1400. frame_count = megasas_build_ldio(instance, scmd, cmd);
  1401. else
  1402. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  1403. if (!frame_count)
  1404. goto out_return_cmd;
  1405. cmd->scmd = scmd;
  1406. scmd->SCp.ptr = (char *)cmd;
  1407. /*
  1408. * Issue the command to the FW
  1409. */
  1410. atomic_inc(&instance->fw_outstanding);
  1411. instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
  1412. cmd->frame_count-1, instance->reg_set);
  1413. return 0;
  1414. out_return_cmd:
  1415. megasas_return_cmd(instance, cmd);
  1416. return SCSI_MLQUEUE_HOST_BUSY;
  1417. }
  1418. /**
  1419. * megasas_queue_command - Queue entry point
  1420. * @scmd: SCSI command to be queued
  1421. * @done: Callback entry point
  1422. */
  1423. static int
  1424. megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
  1425. {
  1426. struct megasas_instance *instance;
  1427. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1428. instance = (struct megasas_instance *)
  1429. scmd->device->host->hostdata;
  1430. if (instance->unload == 1) {
  1431. scmd->result = DID_NO_CONNECT << 16;
  1432. scmd->scsi_done(scmd);
  1433. return 0;
  1434. }
  1435. if (instance->issuepend_done == 0)
  1436. return SCSI_MLQUEUE_HOST_BUSY;
  1437. /* Check for an mpio path and adjust behavior */
  1438. if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
  1439. if (megasas_check_mpio_paths(instance, scmd) ==
  1440. (DID_RESET << 16)) {
  1441. return SCSI_MLQUEUE_HOST_BUSY;
  1442. } else {
  1443. scmd->result = DID_NO_CONNECT << 16;
  1444. scmd->scsi_done(scmd);
  1445. return 0;
  1446. }
  1447. }
  1448. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  1449. scmd->result = DID_NO_CONNECT << 16;
  1450. scmd->scsi_done(scmd);
  1451. return 0;
  1452. }
  1453. mr_device_priv_data = scmd->device->hostdata;
  1454. if (!mr_device_priv_data) {
  1455. scmd->result = DID_NO_CONNECT << 16;
  1456. scmd->scsi_done(scmd);
  1457. return 0;
  1458. }
  1459. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  1460. return SCSI_MLQUEUE_HOST_BUSY;
  1461. if (mr_device_priv_data->tm_busy)
  1462. return SCSI_MLQUEUE_DEVICE_BUSY;
  1463. scmd->result = 0;
  1464. if (MEGASAS_IS_LOGICAL(scmd) &&
  1465. (scmd->device->id >= instance->fw_supported_vd_count ||
  1466. scmd->device->lun)) {
  1467. scmd->result = DID_BAD_TARGET << 16;
  1468. goto out_done;
  1469. }
  1470. switch (scmd->cmnd[0]) {
  1471. case SYNCHRONIZE_CACHE:
  1472. /*
  1473. * FW takes care of flush cache on its own
  1474. * No need to send it down
  1475. */
  1476. scmd->result = DID_OK << 16;
  1477. goto out_done;
  1478. default:
  1479. break;
  1480. }
  1481. return instance->instancet->build_and_issue_cmd(instance, scmd);
  1482. out_done:
  1483. scmd->scsi_done(scmd);
  1484. return 0;
  1485. }
  1486. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1487. {
  1488. int i;
  1489. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1490. if ((megasas_mgmt_info.instance[i]) &&
  1491. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1492. return megasas_mgmt_info.instance[i];
  1493. }
  1494. return NULL;
  1495. }
  1496. /*
  1497. * megasas_update_sdev_properties - Update sdev structure based on controller's FW capabilities
  1498. *
  1499. * @sdev: OS provided scsi device
  1500. *
  1501. * Returns void
  1502. */
  1503. void megasas_update_sdev_properties(struct scsi_device *sdev)
  1504. {
  1505. u16 pd_index = 0;
  1506. u32 device_id, ld;
  1507. struct megasas_instance *instance;
  1508. struct fusion_context *fusion;
  1509. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1510. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  1511. struct MR_LD_RAID *raid;
  1512. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1513. instance = megasas_lookup_instance(sdev->host->host_no);
  1514. fusion = instance->ctrl_context;
  1515. mr_device_priv_data = sdev->hostdata;
  1516. if (!fusion)
  1517. return;
  1518. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1519. instance->use_seqnum_jbod_fp) {
  1520. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1521. sdev->id;
  1522. pd_sync = (void *)fusion->pd_seq_sync
  1523. [(instance->pd_seq_map_id - 1) & 1];
  1524. mr_device_priv_data->is_tm_capable =
  1525. pd_sync->seq[pd_index].capability.tmCapable;
  1526. } else {
  1527. device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
  1528. + sdev->id;
  1529. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1530. ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
  1531. raid = MR_LdRaidGet(ld, local_map_ptr);
  1532. if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
  1533. blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
  1534. mr_device_priv_data->is_tm_capable =
  1535. raid->capability.tmCapable;
  1536. }
  1537. }
  1538. static void megasas_set_device_queue_depth(struct scsi_device *sdev)
  1539. {
  1540. u16 pd_index = 0;
  1541. int ret = DCMD_FAILED;
  1542. struct megasas_instance *instance;
  1543. instance = megasas_lookup_instance(sdev->host->host_no);
  1544. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
  1545. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
  1546. if (instance->pd_info) {
  1547. mutex_lock(&instance->hba_mutex);
  1548. ret = megasas_get_pd_info(instance, pd_index);
  1549. mutex_unlock(&instance->hba_mutex);
  1550. }
  1551. if (ret != DCMD_SUCCESS)
  1552. return;
  1553. if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
  1554. switch (instance->pd_list[pd_index].interface) {
  1555. case SAS_PD:
  1556. scsi_change_queue_depth(sdev, MEGASAS_SAS_QD);
  1557. break;
  1558. case SATA_PD:
  1559. scsi_change_queue_depth(sdev, MEGASAS_SATA_QD);
  1560. break;
  1561. default:
  1562. scsi_change_queue_depth(sdev, MEGASAS_DEFAULT_PD_QD);
  1563. }
  1564. }
  1565. }
  1566. }
  1567. static int megasas_slave_configure(struct scsi_device *sdev)
  1568. {
  1569. u16 pd_index = 0;
  1570. struct megasas_instance *instance;
  1571. instance = megasas_lookup_instance(sdev->host->host_no);
  1572. if (instance->pd_list_not_supported) {
  1573. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1574. sdev->type == TYPE_DISK) {
  1575. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1576. sdev->id;
  1577. if (instance->pd_list[pd_index].driveState !=
  1578. MR_PD_STATE_SYSTEM)
  1579. return -ENXIO;
  1580. }
  1581. }
  1582. megasas_set_device_queue_depth(sdev);
  1583. megasas_update_sdev_properties(sdev);
  1584. /*
  1585. * The RAID firmware may require extended timeouts.
  1586. */
  1587. blk_queue_rq_timeout(sdev->request_queue,
  1588. scmd_timeout * HZ);
  1589. return 0;
  1590. }
  1591. static int megasas_slave_alloc(struct scsi_device *sdev)
  1592. {
  1593. u16 pd_index = 0;
  1594. struct megasas_instance *instance ;
  1595. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1596. instance = megasas_lookup_instance(sdev->host->host_no);
  1597. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
  1598. /*
  1599. * Open the OS scan to the SYSTEM PD
  1600. */
  1601. pd_index =
  1602. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1603. sdev->id;
  1604. if ((instance->pd_list_not_supported ||
  1605. instance->pd_list[pd_index].driveState ==
  1606. MR_PD_STATE_SYSTEM)) {
  1607. goto scan_target;
  1608. }
  1609. return -ENXIO;
  1610. }
  1611. scan_target:
  1612. mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
  1613. GFP_KERNEL);
  1614. if (!mr_device_priv_data)
  1615. return -ENOMEM;
  1616. sdev->hostdata = mr_device_priv_data;
  1617. return 0;
  1618. }
  1619. static void megasas_slave_destroy(struct scsi_device *sdev)
  1620. {
  1621. kfree(sdev->hostdata);
  1622. sdev->hostdata = NULL;
  1623. }
  1624. /*
  1625. * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
  1626. * kill adapter
  1627. * @instance: Adapter soft state
  1628. *
  1629. */
  1630. static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
  1631. {
  1632. int i;
  1633. struct megasas_cmd *cmd_mfi;
  1634. struct megasas_cmd_fusion *cmd_fusion;
  1635. struct fusion_context *fusion = instance->ctrl_context;
  1636. /* Find all outstanding ioctls */
  1637. if (fusion) {
  1638. for (i = 0; i < instance->max_fw_cmds; i++) {
  1639. cmd_fusion = fusion->cmd_list[i];
  1640. if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
  1641. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  1642. if (cmd_mfi->sync_cmd &&
  1643. cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
  1644. megasas_complete_cmd(instance,
  1645. cmd_mfi, DID_OK);
  1646. }
  1647. }
  1648. } else {
  1649. for (i = 0; i < instance->max_fw_cmds; i++) {
  1650. cmd_mfi = instance->cmd_list[i];
  1651. if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
  1652. MFI_CMD_ABORT)
  1653. megasas_complete_cmd(instance, cmd_mfi, DID_OK);
  1654. }
  1655. }
  1656. }
  1657. void megaraid_sas_kill_hba(struct megasas_instance *instance)
  1658. {
  1659. /* Set critical error to block I/O & ioctls in case caller didn't */
  1660. atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
  1661. /* Wait 1 second to ensure IO or ioctls in build have posted */
  1662. msleep(1000);
  1663. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1664. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  1665. (instance->ctrl_context)) {
  1666. writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
  1667. /* Flush */
  1668. readl(&instance->reg_set->doorbell);
  1669. if (instance->requestorId && instance->peerIsPresent)
  1670. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  1671. } else {
  1672. writel(MFI_STOP_ADP,
  1673. &instance->reg_set->inbound_doorbell);
  1674. }
  1675. /* Complete outstanding ioctls when adapter is killed */
  1676. megasas_complete_outstanding_ioctls(instance);
  1677. }
  1678. /**
  1679. * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
  1680. * restored to max value
  1681. * @instance: Adapter soft state
  1682. *
  1683. */
  1684. void
  1685. megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
  1686. {
  1687. unsigned long flags;
  1688. if (instance->flag & MEGASAS_FW_BUSY
  1689. && time_after(jiffies, instance->last_time + 5 * HZ)
  1690. && atomic_read(&instance->fw_outstanding) <
  1691. instance->throttlequeuedepth + 1) {
  1692. spin_lock_irqsave(instance->host->host_lock, flags);
  1693. instance->flag &= ~MEGASAS_FW_BUSY;
  1694. instance->host->can_queue = instance->cur_can_queue;
  1695. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1696. }
  1697. }
  1698. /**
  1699. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1700. * @instance_addr: Address of adapter soft state
  1701. *
  1702. * Tasklet to complete cmds
  1703. */
  1704. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1705. {
  1706. u32 producer;
  1707. u32 consumer;
  1708. u32 context;
  1709. struct megasas_cmd *cmd;
  1710. struct megasas_instance *instance =
  1711. (struct megasas_instance *)instance_addr;
  1712. unsigned long flags;
  1713. /* If we have already declared adapter dead, donot complete cmds */
  1714. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  1715. return;
  1716. spin_lock_irqsave(&instance->completion_lock, flags);
  1717. producer = le32_to_cpu(*instance->producer);
  1718. consumer = le32_to_cpu(*instance->consumer);
  1719. while (consumer != producer) {
  1720. context = le32_to_cpu(instance->reply_queue[consumer]);
  1721. if (context >= instance->max_fw_cmds) {
  1722. dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
  1723. context);
  1724. BUG();
  1725. }
  1726. cmd = instance->cmd_list[context];
  1727. megasas_complete_cmd(instance, cmd, DID_OK);
  1728. consumer++;
  1729. if (consumer == (instance->max_fw_cmds + 1)) {
  1730. consumer = 0;
  1731. }
  1732. }
  1733. *instance->consumer = cpu_to_le32(producer);
  1734. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1735. /*
  1736. * Check if we can restore can_queue
  1737. */
  1738. megasas_check_and_restore_queue_depth(instance);
  1739. }
  1740. /**
  1741. * megasas_start_timer - Initializes a timer object
  1742. * @instance: Adapter soft state
  1743. * @timer: timer object to be initialized
  1744. * @fn: timer function
  1745. * @interval: time interval between timer function call
  1746. *
  1747. */
  1748. void megasas_start_timer(struct megasas_instance *instance,
  1749. struct timer_list *timer,
  1750. void *fn, unsigned long interval)
  1751. {
  1752. init_timer(timer);
  1753. timer->expires = jiffies + interval;
  1754. timer->data = (unsigned long)instance;
  1755. timer->function = fn;
  1756. add_timer(timer);
  1757. }
  1758. static void
  1759. megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
  1760. static void
  1761. process_fw_state_change_wq(struct work_struct *work);
  1762. void megasas_do_ocr(struct megasas_instance *instance)
  1763. {
  1764. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  1765. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  1766. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  1767. *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
  1768. }
  1769. instance->instancet->disable_intr(instance);
  1770. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  1771. instance->issuepend_done = 0;
  1772. atomic_set(&instance->fw_outstanding, 0);
  1773. megasas_internal_reset_defer_cmds(instance);
  1774. process_fw_state_change_wq(&instance->work_init);
  1775. }
  1776. static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
  1777. int initial)
  1778. {
  1779. struct megasas_cmd *cmd;
  1780. struct megasas_dcmd_frame *dcmd;
  1781. struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
  1782. dma_addr_t new_affiliation_111_h;
  1783. int ld, retval = 0;
  1784. u8 thisVf;
  1785. cmd = megasas_get_cmd(instance);
  1786. if (!cmd) {
  1787. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
  1788. "Failed to get cmd for scsi%d\n",
  1789. instance->host->host_no);
  1790. return -ENOMEM;
  1791. }
  1792. dcmd = &cmd->frame->dcmd;
  1793. if (!instance->vf_affiliation_111) {
  1794. dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
  1795. "affiliation for scsi%d\n", instance->host->host_no);
  1796. megasas_return_cmd(instance, cmd);
  1797. return -ENOMEM;
  1798. }
  1799. if (initial)
  1800. memset(instance->vf_affiliation_111, 0,
  1801. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1802. else {
  1803. new_affiliation_111 =
  1804. pci_alloc_consistent(instance->pdev,
  1805. sizeof(struct MR_LD_VF_AFFILIATION_111),
  1806. &new_affiliation_111_h);
  1807. if (!new_affiliation_111) {
  1808. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
  1809. "memory for new affiliation for scsi%d\n",
  1810. instance->host->host_no);
  1811. megasas_return_cmd(instance, cmd);
  1812. return -ENOMEM;
  1813. }
  1814. memset(new_affiliation_111, 0,
  1815. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1816. }
  1817. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1818. dcmd->cmd = MFI_CMD_DCMD;
  1819. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  1820. dcmd->sge_count = 1;
  1821. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
  1822. dcmd->timeout = 0;
  1823. dcmd->pad_0 = 0;
  1824. dcmd->data_xfer_len =
  1825. cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
  1826. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
  1827. if (initial)
  1828. dcmd->sgl.sge32[0].phys_addr =
  1829. cpu_to_le32(instance->vf_affiliation_111_h);
  1830. else
  1831. dcmd->sgl.sge32[0].phys_addr =
  1832. cpu_to_le32(new_affiliation_111_h);
  1833. dcmd->sgl.sge32[0].length = cpu_to_le32(
  1834. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1835. dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
  1836. "scsi%d\n", instance->host->host_no);
  1837. if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
  1838. dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
  1839. " failed with status 0x%x for scsi%d\n",
  1840. dcmd->cmd_status, instance->host->host_no);
  1841. retval = 1; /* Do a scan if we couldn't get affiliation */
  1842. goto out;
  1843. }
  1844. if (!initial) {
  1845. thisVf = new_affiliation_111->thisVf;
  1846. for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
  1847. if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
  1848. new_affiliation_111->map[ld].policy[thisVf]) {
  1849. dev_warn(&instance->pdev->dev, "SR-IOV: "
  1850. "Got new LD/VF affiliation for scsi%d\n",
  1851. instance->host->host_no);
  1852. memcpy(instance->vf_affiliation_111,
  1853. new_affiliation_111,
  1854. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1855. retval = 1;
  1856. goto out;
  1857. }
  1858. }
  1859. out:
  1860. if (new_affiliation_111) {
  1861. pci_free_consistent(instance->pdev,
  1862. sizeof(struct MR_LD_VF_AFFILIATION_111),
  1863. new_affiliation_111,
  1864. new_affiliation_111_h);
  1865. }
  1866. megasas_return_cmd(instance, cmd);
  1867. return retval;
  1868. }
  1869. static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
  1870. int initial)
  1871. {
  1872. struct megasas_cmd *cmd;
  1873. struct megasas_dcmd_frame *dcmd;
  1874. struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
  1875. struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
  1876. dma_addr_t new_affiliation_h;
  1877. int i, j, retval = 0, found = 0, doscan = 0;
  1878. u8 thisVf;
  1879. cmd = megasas_get_cmd(instance);
  1880. if (!cmd) {
  1881. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
  1882. "Failed to get cmd for scsi%d\n",
  1883. instance->host->host_no);
  1884. return -ENOMEM;
  1885. }
  1886. dcmd = &cmd->frame->dcmd;
  1887. if (!instance->vf_affiliation) {
  1888. dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
  1889. "affiliation for scsi%d\n", instance->host->host_no);
  1890. megasas_return_cmd(instance, cmd);
  1891. return -ENOMEM;
  1892. }
  1893. if (initial)
  1894. memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
  1895. sizeof(struct MR_LD_VF_AFFILIATION));
  1896. else {
  1897. new_affiliation =
  1898. pci_alloc_consistent(instance->pdev,
  1899. (MAX_LOGICAL_DRIVES + 1) *
  1900. sizeof(struct MR_LD_VF_AFFILIATION),
  1901. &new_affiliation_h);
  1902. if (!new_affiliation) {
  1903. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
  1904. "memory for new affiliation for scsi%d\n",
  1905. instance->host->host_no);
  1906. megasas_return_cmd(instance, cmd);
  1907. return -ENOMEM;
  1908. }
  1909. memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
  1910. sizeof(struct MR_LD_VF_AFFILIATION));
  1911. }
  1912. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1913. dcmd->cmd = MFI_CMD_DCMD;
  1914. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  1915. dcmd->sge_count = 1;
  1916. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
  1917. dcmd->timeout = 0;
  1918. dcmd->pad_0 = 0;
  1919. dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
  1920. sizeof(struct MR_LD_VF_AFFILIATION));
  1921. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
  1922. if (initial)
  1923. dcmd->sgl.sge32[0].phys_addr =
  1924. cpu_to_le32(instance->vf_affiliation_h);
  1925. else
  1926. dcmd->sgl.sge32[0].phys_addr =
  1927. cpu_to_le32(new_affiliation_h);
  1928. dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
  1929. sizeof(struct MR_LD_VF_AFFILIATION));
  1930. dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
  1931. "scsi%d\n", instance->host->host_no);
  1932. if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
  1933. dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
  1934. " failed with status 0x%x for scsi%d\n",
  1935. dcmd->cmd_status, instance->host->host_no);
  1936. retval = 1; /* Do a scan if we couldn't get affiliation */
  1937. goto out;
  1938. }
  1939. if (!initial) {
  1940. if (!new_affiliation->ldCount) {
  1941. dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
  1942. "affiliation for passive path for scsi%d\n",
  1943. instance->host->host_no);
  1944. retval = 1;
  1945. goto out;
  1946. }
  1947. newmap = new_affiliation->map;
  1948. savedmap = instance->vf_affiliation->map;
  1949. thisVf = new_affiliation->thisVf;
  1950. for (i = 0 ; i < new_affiliation->ldCount; i++) {
  1951. found = 0;
  1952. for (j = 0; j < instance->vf_affiliation->ldCount;
  1953. j++) {
  1954. if (newmap->ref.targetId ==
  1955. savedmap->ref.targetId) {
  1956. found = 1;
  1957. if (newmap->policy[thisVf] !=
  1958. savedmap->policy[thisVf]) {
  1959. doscan = 1;
  1960. goto out;
  1961. }
  1962. }
  1963. savedmap = (struct MR_LD_VF_MAP *)
  1964. ((unsigned char *)savedmap +
  1965. savedmap->size);
  1966. }
  1967. if (!found && newmap->policy[thisVf] !=
  1968. MR_LD_ACCESS_HIDDEN) {
  1969. doscan = 1;
  1970. goto out;
  1971. }
  1972. newmap = (struct MR_LD_VF_MAP *)
  1973. ((unsigned char *)newmap + newmap->size);
  1974. }
  1975. newmap = new_affiliation->map;
  1976. savedmap = instance->vf_affiliation->map;
  1977. for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
  1978. found = 0;
  1979. for (j = 0 ; j < new_affiliation->ldCount; j++) {
  1980. if (savedmap->ref.targetId ==
  1981. newmap->ref.targetId) {
  1982. found = 1;
  1983. if (savedmap->policy[thisVf] !=
  1984. newmap->policy[thisVf]) {
  1985. doscan = 1;
  1986. goto out;
  1987. }
  1988. }
  1989. newmap = (struct MR_LD_VF_MAP *)
  1990. ((unsigned char *)newmap +
  1991. newmap->size);
  1992. }
  1993. if (!found && savedmap->policy[thisVf] !=
  1994. MR_LD_ACCESS_HIDDEN) {
  1995. doscan = 1;
  1996. goto out;
  1997. }
  1998. savedmap = (struct MR_LD_VF_MAP *)
  1999. ((unsigned char *)savedmap +
  2000. savedmap->size);
  2001. }
  2002. }
  2003. out:
  2004. if (doscan) {
  2005. dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
  2006. "affiliation for scsi%d\n", instance->host->host_no);
  2007. memcpy(instance->vf_affiliation, new_affiliation,
  2008. new_affiliation->size);
  2009. retval = 1;
  2010. }
  2011. if (new_affiliation)
  2012. pci_free_consistent(instance->pdev,
  2013. (MAX_LOGICAL_DRIVES + 1) *
  2014. sizeof(struct MR_LD_VF_AFFILIATION),
  2015. new_affiliation, new_affiliation_h);
  2016. megasas_return_cmd(instance, cmd);
  2017. return retval;
  2018. }
  2019. /* This function will get the current SR-IOV LD/VF affiliation */
  2020. static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
  2021. int initial)
  2022. {
  2023. int retval;
  2024. if (instance->PlasmaFW111)
  2025. retval = megasas_get_ld_vf_affiliation_111(instance, initial);
  2026. else
  2027. retval = megasas_get_ld_vf_affiliation_12(instance, initial);
  2028. return retval;
  2029. }
  2030. /* This function will tell FW to start the SR-IOV heartbeat */
  2031. int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
  2032. int initial)
  2033. {
  2034. struct megasas_cmd *cmd;
  2035. struct megasas_dcmd_frame *dcmd;
  2036. int retval = 0;
  2037. cmd = megasas_get_cmd(instance);
  2038. if (!cmd) {
  2039. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
  2040. "Failed to get cmd for scsi%d\n",
  2041. instance->host->host_no);
  2042. return -ENOMEM;
  2043. }
  2044. dcmd = &cmd->frame->dcmd;
  2045. if (initial) {
  2046. instance->hb_host_mem =
  2047. pci_zalloc_consistent(instance->pdev,
  2048. sizeof(struct MR_CTRL_HB_HOST_MEM),
  2049. &instance->hb_host_mem_h);
  2050. if (!instance->hb_host_mem) {
  2051. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
  2052. " memory for heartbeat host memory for scsi%d\n",
  2053. instance->host->host_no);
  2054. retval = -ENOMEM;
  2055. goto out;
  2056. }
  2057. }
  2058. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2059. dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
  2060. dcmd->cmd = MFI_CMD_DCMD;
  2061. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  2062. dcmd->sge_count = 1;
  2063. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
  2064. dcmd->timeout = 0;
  2065. dcmd->pad_0 = 0;
  2066. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
  2067. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
  2068. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
  2069. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
  2070. dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
  2071. instance->host->host_no);
  2072. if (instance->ctrl_context && !instance->mask_interrupts)
  2073. retval = megasas_issue_blocked_cmd(instance, cmd,
  2074. MEGASAS_ROUTINE_WAIT_TIME_VF);
  2075. else
  2076. retval = megasas_issue_polled(instance, cmd);
  2077. if (retval) {
  2078. dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
  2079. "_MEM_ALLOC DCMD %s for scsi%d\n",
  2080. (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
  2081. "timed out" : "failed", instance->host->host_no);
  2082. retval = 1;
  2083. }
  2084. out:
  2085. megasas_return_cmd(instance, cmd);
  2086. return retval;
  2087. }
  2088. /* Handler for SR-IOV heartbeat */
  2089. void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
  2090. {
  2091. struct megasas_instance *instance =
  2092. (struct megasas_instance *)instance_addr;
  2093. if (instance->hb_host_mem->HB.fwCounter !=
  2094. instance->hb_host_mem->HB.driverCounter) {
  2095. instance->hb_host_mem->HB.driverCounter =
  2096. instance->hb_host_mem->HB.fwCounter;
  2097. mod_timer(&instance->sriov_heartbeat_timer,
  2098. jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  2099. } else {
  2100. dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
  2101. "completed for scsi%d\n", instance->host->host_no);
  2102. schedule_work(&instance->work_init);
  2103. }
  2104. }
  2105. /**
  2106. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  2107. * @instance: Adapter soft state
  2108. *
  2109. * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
  2110. * complete all its outstanding commands. Returns error if one or more IOs
  2111. * are pending after this time period. It also marks the controller dead.
  2112. */
  2113. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  2114. {
  2115. int i, sl, outstanding;
  2116. u32 reset_index;
  2117. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  2118. unsigned long flags;
  2119. struct list_head clist_local;
  2120. struct megasas_cmd *reset_cmd;
  2121. u32 fw_state;
  2122. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2123. dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
  2124. __func__, __LINE__);
  2125. return FAILED;
  2126. }
  2127. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2128. INIT_LIST_HEAD(&clist_local);
  2129. spin_lock_irqsave(&instance->hba_lock, flags);
  2130. list_splice_init(&instance->internal_reset_pending_q,
  2131. &clist_local);
  2132. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2133. dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
  2134. for (i = 0; i < wait_time; i++) {
  2135. msleep(1000);
  2136. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
  2137. break;
  2138. }
  2139. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2140. dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
  2141. atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
  2142. return FAILED;
  2143. }
  2144. reset_index = 0;
  2145. while (!list_empty(&clist_local)) {
  2146. reset_cmd = list_entry((&clist_local)->next,
  2147. struct megasas_cmd, list);
  2148. list_del_init(&reset_cmd->list);
  2149. if (reset_cmd->scmd) {
  2150. reset_cmd->scmd->result = DID_RESET << 16;
  2151. dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
  2152. reset_index, reset_cmd,
  2153. reset_cmd->scmd->cmnd[0]);
  2154. reset_cmd->scmd->scsi_done(reset_cmd->scmd);
  2155. megasas_return_cmd(instance, reset_cmd);
  2156. } else if (reset_cmd->sync_cmd) {
  2157. dev_notice(&instance->pdev->dev, "%p synch cmds"
  2158. "reset queue\n",
  2159. reset_cmd);
  2160. reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  2161. instance->instancet->fire_cmd(instance,
  2162. reset_cmd->frame_phys_addr,
  2163. 0, instance->reg_set);
  2164. } else {
  2165. dev_notice(&instance->pdev->dev, "%p unexpected"
  2166. "cmds lst\n",
  2167. reset_cmd);
  2168. }
  2169. reset_index++;
  2170. }
  2171. return SUCCESS;
  2172. }
  2173. for (i = 0; i < resetwaittime; i++) {
  2174. outstanding = atomic_read(&instance->fw_outstanding);
  2175. if (!outstanding)
  2176. break;
  2177. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  2178. dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
  2179. "commands to complete\n",i,outstanding);
  2180. /*
  2181. * Call cmd completion routine. Cmd to be
  2182. * be completed directly without depending on isr.
  2183. */
  2184. megasas_complete_cmd_dpc((unsigned long)instance);
  2185. }
  2186. msleep(1000);
  2187. }
  2188. i = 0;
  2189. outstanding = atomic_read(&instance->fw_outstanding);
  2190. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2191. if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
  2192. goto no_outstanding;
  2193. if (instance->disableOnlineCtrlReset)
  2194. goto kill_hba_and_failed;
  2195. do {
  2196. if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
  2197. dev_info(&instance->pdev->dev,
  2198. "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
  2199. __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
  2200. if (i == 3)
  2201. goto kill_hba_and_failed;
  2202. megasas_do_ocr(instance);
  2203. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2204. dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
  2205. __func__, __LINE__);
  2206. return FAILED;
  2207. }
  2208. dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
  2209. __func__, __LINE__);
  2210. for (sl = 0; sl < 10; sl++)
  2211. msleep(500);
  2212. outstanding = atomic_read(&instance->fw_outstanding);
  2213. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2214. if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
  2215. goto no_outstanding;
  2216. }
  2217. i++;
  2218. } while (i <= 3);
  2219. no_outstanding:
  2220. dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
  2221. __func__, __LINE__);
  2222. return SUCCESS;
  2223. kill_hba_and_failed:
  2224. /* Reset not supported, kill adapter */
  2225. dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
  2226. " disableOnlineCtrlReset %d fw_outstanding %d \n",
  2227. __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
  2228. atomic_read(&instance->fw_outstanding));
  2229. megasas_dump_pending_frames(instance);
  2230. megaraid_sas_kill_hba(instance);
  2231. return FAILED;
  2232. }
  2233. /**
  2234. * megasas_generic_reset - Generic reset routine
  2235. * @scmd: Mid-layer SCSI command
  2236. *
  2237. * This routine implements a generic reset handler for device, bus and host
  2238. * reset requests. Device, bus and host specific reset handlers can use this
  2239. * function after they do their specific tasks.
  2240. */
  2241. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  2242. {
  2243. int ret_val;
  2244. struct megasas_instance *instance;
  2245. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2246. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
  2247. scmd->cmnd[0], scmd->retries);
  2248. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2249. dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
  2250. return FAILED;
  2251. }
  2252. ret_val = megasas_wait_for_outstanding(instance);
  2253. if (ret_val == SUCCESS)
  2254. dev_notice(&instance->pdev->dev, "reset successful\n");
  2255. else
  2256. dev_err(&instance->pdev->dev, "failed to do reset\n");
  2257. return ret_val;
  2258. }
  2259. /**
  2260. * megasas_reset_timer - quiesce the adapter if required
  2261. * @scmd: scsi cmnd
  2262. *
  2263. * Sets the FW busy flag and reduces the host->can_queue if the
  2264. * cmd has not been completed within the timeout period.
  2265. */
  2266. static enum
  2267. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  2268. {
  2269. struct megasas_instance *instance;
  2270. unsigned long flags;
  2271. if (time_after(jiffies, scmd->jiffies_at_alloc +
  2272. (scmd_timeout * 2) * HZ)) {
  2273. return BLK_EH_NOT_HANDLED;
  2274. }
  2275. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2276. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  2277. /* FW is busy, throttle IO */
  2278. spin_lock_irqsave(instance->host->host_lock, flags);
  2279. instance->host->can_queue = instance->throttlequeuedepth;
  2280. instance->last_time = jiffies;
  2281. instance->flag |= MEGASAS_FW_BUSY;
  2282. spin_unlock_irqrestore(instance->host->host_lock, flags);
  2283. }
  2284. return BLK_EH_RESET_TIMER;
  2285. }
  2286. /**
  2287. * megasas_reset_bus_host - Bus & host reset handler entry point
  2288. */
  2289. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  2290. {
  2291. int ret;
  2292. struct megasas_instance *instance;
  2293. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2294. /*
  2295. * First wait for all commands to complete
  2296. */
  2297. if (instance->ctrl_context)
  2298. ret = megasas_reset_fusion(scmd->device->host, 1);
  2299. else
  2300. ret = megasas_generic_reset(scmd);
  2301. return ret;
  2302. }
  2303. /**
  2304. * megasas_task_abort - Issues task abort request to firmware
  2305. * (supported only for fusion adapters)
  2306. * @scmd: SCSI command pointer
  2307. */
  2308. static int megasas_task_abort(struct scsi_cmnd *scmd)
  2309. {
  2310. int ret;
  2311. struct megasas_instance *instance;
  2312. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2313. if (instance->ctrl_context)
  2314. ret = megasas_task_abort_fusion(scmd);
  2315. else {
  2316. sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
  2317. ret = FAILED;
  2318. }
  2319. return ret;
  2320. }
  2321. /**
  2322. * megasas_reset_target: Issues target reset request to firmware
  2323. * (supported only for fusion adapters)
  2324. * @scmd: SCSI command pointer
  2325. */
  2326. static int megasas_reset_target(struct scsi_cmnd *scmd)
  2327. {
  2328. int ret;
  2329. struct megasas_instance *instance;
  2330. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2331. if (instance->ctrl_context)
  2332. ret = megasas_reset_target_fusion(scmd);
  2333. else {
  2334. sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
  2335. ret = FAILED;
  2336. }
  2337. return ret;
  2338. }
  2339. /**
  2340. * megasas_bios_param - Returns disk geometry for a disk
  2341. * @sdev: device handle
  2342. * @bdev: block device
  2343. * @capacity: drive capacity
  2344. * @geom: geometry parameters
  2345. */
  2346. static int
  2347. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  2348. sector_t capacity, int geom[])
  2349. {
  2350. int heads;
  2351. int sectors;
  2352. sector_t cylinders;
  2353. unsigned long tmp;
  2354. /* Default heads (64) & sectors (32) */
  2355. heads = 64;
  2356. sectors = 32;
  2357. tmp = heads * sectors;
  2358. cylinders = capacity;
  2359. sector_div(cylinders, tmp);
  2360. /*
  2361. * Handle extended translation size for logical drives > 1Gb
  2362. */
  2363. if (capacity >= 0x200000) {
  2364. heads = 255;
  2365. sectors = 63;
  2366. tmp = heads*sectors;
  2367. cylinders = capacity;
  2368. sector_div(cylinders, tmp);
  2369. }
  2370. geom[0] = heads;
  2371. geom[1] = sectors;
  2372. geom[2] = cylinders;
  2373. return 0;
  2374. }
  2375. static void megasas_aen_polling(struct work_struct *work);
  2376. /**
  2377. * megasas_service_aen - Processes an event notification
  2378. * @instance: Adapter soft state
  2379. * @cmd: AEN command completed by the ISR
  2380. *
  2381. * For AEN, driver sends a command down to FW that is held by the FW till an
  2382. * event occurs. When an event of interest occurs, FW completes the command
  2383. * that it was previously holding.
  2384. *
  2385. * This routines sends SIGIO signal to processes that have registered with the
  2386. * driver for AEN.
  2387. */
  2388. static void
  2389. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  2390. {
  2391. unsigned long flags;
  2392. /*
  2393. * Don't signal app if it is just an aborted previously registered aen
  2394. */
  2395. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  2396. spin_lock_irqsave(&poll_aen_lock, flags);
  2397. megasas_poll_wait_aen = 1;
  2398. spin_unlock_irqrestore(&poll_aen_lock, flags);
  2399. wake_up(&megasas_poll_wait);
  2400. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  2401. }
  2402. else
  2403. cmd->abort_aen = 0;
  2404. instance->aen_cmd = NULL;
  2405. megasas_return_cmd(instance, cmd);
  2406. if ((instance->unload == 0) &&
  2407. ((instance->issuepend_done == 1))) {
  2408. struct megasas_aen_event *ev;
  2409. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2410. if (!ev) {
  2411. dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
  2412. } else {
  2413. ev->instance = instance;
  2414. instance->ev = ev;
  2415. INIT_DELAYED_WORK(&ev->hotplug_work,
  2416. megasas_aen_polling);
  2417. schedule_delayed_work(&ev->hotplug_work, 0);
  2418. }
  2419. }
  2420. }
  2421. static ssize_t
  2422. megasas_fw_crash_buffer_store(struct device *cdev,
  2423. struct device_attribute *attr, const char *buf, size_t count)
  2424. {
  2425. struct Scsi_Host *shost = class_to_shost(cdev);
  2426. struct megasas_instance *instance =
  2427. (struct megasas_instance *) shost->hostdata;
  2428. int val = 0;
  2429. unsigned long flags;
  2430. if (kstrtoint(buf, 0, &val) != 0)
  2431. return -EINVAL;
  2432. spin_lock_irqsave(&instance->crashdump_lock, flags);
  2433. instance->fw_crash_buffer_offset = val;
  2434. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2435. return strlen(buf);
  2436. }
  2437. static ssize_t
  2438. megasas_fw_crash_buffer_show(struct device *cdev,
  2439. struct device_attribute *attr, char *buf)
  2440. {
  2441. struct Scsi_Host *shost = class_to_shost(cdev);
  2442. struct megasas_instance *instance =
  2443. (struct megasas_instance *) shost->hostdata;
  2444. u32 size;
  2445. unsigned long buff_addr;
  2446. unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
  2447. unsigned long src_addr;
  2448. unsigned long flags;
  2449. u32 buff_offset;
  2450. spin_lock_irqsave(&instance->crashdump_lock, flags);
  2451. buff_offset = instance->fw_crash_buffer_offset;
  2452. if (!instance->crash_dump_buf &&
  2453. !((instance->fw_crash_state == AVAILABLE) ||
  2454. (instance->fw_crash_state == COPYING))) {
  2455. dev_err(&instance->pdev->dev,
  2456. "Firmware crash dump is not available\n");
  2457. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2458. return -EINVAL;
  2459. }
  2460. buff_addr = (unsigned long) buf;
  2461. if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
  2462. dev_err(&instance->pdev->dev,
  2463. "Firmware crash dump offset is out of range\n");
  2464. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2465. return 0;
  2466. }
  2467. size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
  2468. size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
  2469. src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
  2470. (buff_offset % dmachunk);
  2471. memcpy(buf, (void *)src_addr, size);
  2472. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2473. return size;
  2474. }
  2475. static ssize_t
  2476. megasas_fw_crash_buffer_size_show(struct device *cdev,
  2477. struct device_attribute *attr, char *buf)
  2478. {
  2479. struct Scsi_Host *shost = class_to_shost(cdev);
  2480. struct megasas_instance *instance =
  2481. (struct megasas_instance *) shost->hostdata;
  2482. return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
  2483. ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
  2484. }
  2485. static ssize_t
  2486. megasas_fw_crash_state_store(struct device *cdev,
  2487. struct device_attribute *attr, const char *buf, size_t count)
  2488. {
  2489. struct Scsi_Host *shost = class_to_shost(cdev);
  2490. struct megasas_instance *instance =
  2491. (struct megasas_instance *) shost->hostdata;
  2492. int val = 0;
  2493. unsigned long flags;
  2494. if (kstrtoint(buf, 0, &val) != 0)
  2495. return -EINVAL;
  2496. if ((val <= AVAILABLE || val > COPY_ERROR)) {
  2497. dev_err(&instance->pdev->dev, "application updates invalid "
  2498. "firmware crash state\n");
  2499. return -EINVAL;
  2500. }
  2501. instance->fw_crash_state = val;
  2502. if ((val == COPIED) || (val == COPY_ERROR)) {
  2503. spin_lock_irqsave(&instance->crashdump_lock, flags);
  2504. megasas_free_host_crash_buffer(instance);
  2505. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2506. if (val == COPY_ERROR)
  2507. dev_info(&instance->pdev->dev, "application failed to "
  2508. "copy Firmware crash dump\n");
  2509. else
  2510. dev_info(&instance->pdev->dev, "Firmware crash dump "
  2511. "copied successfully\n");
  2512. }
  2513. return strlen(buf);
  2514. }
  2515. static ssize_t
  2516. megasas_fw_crash_state_show(struct device *cdev,
  2517. struct device_attribute *attr, char *buf)
  2518. {
  2519. struct Scsi_Host *shost = class_to_shost(cdev);
  2520. struct megasas_instance *instance =
  2521. (struct megasas_instance *) shost->hostdata;
  2522. return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
  2523. }
  2524. static ssize_t
  2525. megasas_page_size_show(struct device *cdev,
  2526. struct device_attribute *attr, char *buf)
  2527. {
  2528. return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
  2529. }
  2530. static ssize_t
  2531. megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
  2532. char *buf)
  2533. {
  2534. struct Scsi_Host *shost = class_to_shost(cdev);
  2535. struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
  2536. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
  2537. }
  2538. static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
  2539. megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
  2540. static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
  2541. megasas_fw_crash_buffer_size_show, NULL);
  2542. static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
  2543. megasas_fw_crash_state_show, megasas_fw_crash_state_store);
  2544. static DEVICE_ATTR(page_size, S_IRUGO,
  2545. megasas_page_size_show, NULL);
  2546. static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
  2547. megasas_ldio_outstanding_show, NULL);
  2548. struct device_attribute *megaraid_host_attrs[] = {
  2549. &dev_attr_fw_crash_buffer_size,
  2550. &dev_attr_fw_crash_buffer,
  2551. &dev_attr_fw_crash_state,
  2552. &dev_attr_page_size,
  2553. &dev_attr_ldio_outstanding,
  2554. NULL,
  2555. };
  2556. /*
  2557. * Scsi host template for megaraid_sas driver
  2558. */
  2559. static struct scsi_host_template megasas_template = {
  2560. .module = THIS_MODULE,
  2561. .name = "Avago SAS based MegaRAID driver",
  2562. .proc_name = "megaraid_sas",
  2563. .slave_configure = megasas_slave_configure,
  2564. .slave_alloc = megasas_slave_alloc,
  2565. .slave_destroy = megasas_slave_destroy,
  2566. .queuecommand = megasas_queue_command,
  2567. .eh_target_reset_handler = megasas_reset_target,
  2568. .eh_abort_handler = megasas_task_abort,
  2569. .eh_host_reset_handler = megasas_reset_bus_host,
  2570. .eh_timed_out = megasas_reset_timer,
  2571. .shost_attrs = megaraid_host_attrs,
  2572. .bios_param = megasas_bios_param,
  2573. .use_clustering = ENABLE_CLUSTERING,
  2574. .change_queue_depth = scsi_change_queue_depth,
  2575. .no_write_same = 1,
  2576. };
  2577. /**
  2578. * megasas_complete_int_cmd - Completes an internal command
  2579. * @instance: Adapter soft state
  2580. * @cmd: Command to be completed
  2581. *
  2582. * The megasas_issue_blocked_cmd() function waits for a command to complete
  2583. * after it issues a command. This function wakes up that waiting routine by
  2584. * calling wake_up() on the wait queue.
  2585. */
  2586. static void
  2587. megasas_complete_int_cmd(struct megasas_instance *instance,
  2588. struct megasas_cmd *cmd)
  2589. {
  2590. cmd->cmd_status_drv = cmd->frame->io.cmd_status;
  2591. wake_up(&instance->int_cmd_wait_q);
  2592. }
  2593. /**
  2594. * megasas_complete_abort - Completes aborting a command
  2595. * @instance: Adapter soft state
  2596. * @cmd: Cmd that was issued to abort another cmd
  2597. *
  2598. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  2599. * after it issues an abort on a previously issued command. This function
  2600. * wakes up all functions waiting on the same wait queue.
  2601. */
  2602. static void
  2603. megasas_complete_abort(struct megasas_instance *instance,
  2604. struct megasas_cmd *cmd)
  2605. {
  2606. if (cmd->sync_cmd) {
  2607. cmd->sync_cmd = 0;
  2608. cmd->cmd_status_drv = 0;
  2609. wake_up(&instance->abort_cmd_wait_q);
  2610. }
  2611. }
  2612. /**
  2613. * megasas_complete_cmd - Completes a command
  2614. * @instance: Adapter soft state
  2615. * @cmd: Command to be completed
  2616. * @alt_status: If non-zero, use this value as status to
  2617. * SCSI mid-layer instead of the value returned
  2618. * by the FW. This should be used if caller wants
  2619. * an alternate status (as in the case of aborted
  2620. * commands)
  2621. */
  2622. void
  2623. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  2624. u8 alt_status)
  2625. {
  2626. int exception = 0;
  2627. struct megasas_header *hdr = &cmd->frame->hdr;
  2628. unsigned long flags;
  2629. struct fusion_context *fusion = instance->ctrl_context;
  2630. u32 opcode, status;
  2631. /* flag for the retry reset */
  2632. cmd->retry_for_fw_reset = 0;
  2633. if (cmd->scmd)
  2634. cmd->scmd->SCp.ptr = NULL;
  2635. switch (hdr->cmd) {
  2636. case MFI_CMD_INVALID:
  2637. /* Some older 1068 controller FW may keep a pended
  2638. MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
  2639. when booting the kdump kernel. Ignore this command to
  2640. prevent a kernel panic on shutdown of the kdump kernel. */
  2641. dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
  2642. "completed\n");
  2643. dev_warn(&instance->pdev->dev, "If you have a controller "
  2644. "other than PERC5, please upgrade your firmware\n");
  2645. break;
  2646. case MFI_CMD_PD_SCSI_IO:
  2647. case MFI_CMD_LD_SCSI_IO:
  2648. /*
  2649. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  2650. * issued either through an IO path or an IOCTL path. If it
  2651. * was via IOCTL, we will send it to internal completion.
  2652. */
  2653. if (cmd->sync_cmd) {
  2654. cmd->sync_cmd = 0;
  2655. megasas_complete_int_cmd(instance, cmd);
  2656. break;
  2657. }
  2658. case MFI_CMD_LD_READ:
  2659. case MFI_CMD_LD_WRITE:
  2660. if (alt_status) {
  2661. cmd->scmd->result = alt_status << 16;
  2662. exception = 1;
  2663. }
  2664. if (exception) {
  2665. atomic_dec(&instance->fw_outstanding);
  2666. scsi_dma_unmap(cmd->scmd);
  2667. cmd->scmd->scsi_done(cmd->scmd);
  2668. megasas_return_cmd(instance, cmd);
  2669. break;
  2670. }
  2671. switch (hdr->cmd_status) {
  2672. case MFI_STAT_OK:
  2673. cmd->scmd->result = DID_OK << 16;
  2674. break;
  2675. case MFI_STAT_SCSI_IO_FAILED:
  2676. case MFI_STAT_LD_INIT_IN_PROGRESS:
  2677. cmd->scmd->result =
  2678. (DID_ERROR << 16) | hdr->scsi_status;
  2679. break;
  2680. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  2681. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  2682. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  2683. memset(cmd->scmd->sense_buffer, 0,
  2684. SCSI_SENSE_BUFFERSIZE);
  2685. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  2686. hdr->sense_len);
  2687. cmd->scmd->result |= DRIVER_SENSE << 24;
  2688. }
  2689. break;
  2690. case MFI_STAT_LD_OFFLINE:
  2691. case MFI_STAT_DEVICE_NOT_FOUND:
  2692. cmd->scmd->result = DID_BAD_TARGET << 16;
  2693. break;
  2694. default:
  2695. dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
  2696. hdr->cmd_status);
  2697. cmd->scmd->result = DID_ERROR << 16;
  2698. break;
  2699. }
  2700. atomic_dec(&instance->fw_outstanding);
  2701. scsi_dma_unmap(cmd->scmd);
  2702. cmd->scmd->scsi_done(cmd->scmd);
  2703. megasas_return_cmd(instance, cmd);
  2704. break;
  2705. case MFI_CMD_SMP:
  2706. case MFI_CMD_STP:
  2707. case MFI_CMD_DCMD:
  2708. opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
  2709. /* Check for LD map update */
  2710. if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
  2711. && (cmd->frame->dcmd.mbox.b[1] == 1)) {
  2712. fusion->fast_path_io = 0;
  2713. spin_lock_irqsave(instance->host->host_lock, flags);
  2714. instance->map_update_cmd = NULL;
  2715. if (cmd->frame->hdr.cmd_status != 0) {
  2716. if (cmd->frame->hdr.cmd_status !=
  2717. MFI_STAT_NOT_FOUND)
  2718. dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
  2719. cmd->frame->hdr.cmd_status);
  2720. else {
  2721. megasas_return_cmd(instance, cmd);
  2722. spin_unlock_irqrestore(
  2723. instance->host->host_lock,
  2724. flags);
  2725. break;
  2726. }
  2727. } else
  2728. instance->map_id++;
  2729. megasas_return_cmd(instance, cmd);
  2730. /*
  2731. * Set fast path IO to ZERO.
  2732. * Validate Map will set proper value.
  2733. * Meanwhile all IOs will go as LD IO.
  2734. */
  2735. if (MR_ValidateMapInfo(instance))
  2736. fusion->fast_path_io = 1;
  2737. else
  2738. fusion->fast_path_io = 0;
  2739. megasas_sync_map_info(instance);
  2740. spin_unlock_irqrestore(instance->host->host_lock,
  2741. flags);
  2742. break;
  2743. }
  2744. if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  2745. opcode == MR_DCMD_CTRL_EVENT_GET) {
  2746. spin_lock_irqsave(&poll_aen_lock, flags);
  2747. megasas_poll_wait_aen = 0;
  2748. spin_unlock_irqrestore(&poll_aen_lock, flags);
  2749. }
  2750. /* FW has an updated PD sequence */
  2751. if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
  2752. (cmd->frame->dcmd.mbox.b[0] == 1)) {
  2753. spin_lock_irqsave(instance->host->host_lock, flags);
  2754. status = cmd->frame->hdr.cmd_status;
  2755. instance->jbod_seq_cmd = NULL;
  2756. megasas_return_cmd(instance, cmd);
  2757. if (status == MFI_STAT_OK) {
  2758. instance->pd_seq_map_id++;
  2759. /* Re-register a pd sync seq num cmd */
  2760. if (megasas_sync_pd_seq_num(instance, true))
  2761. instance->use_seqnum_jbod_fp = false;
  2762. } else
  2763. instance->use_seqnum_jbod_fp = false;
  2764. spin_unlock_irqrestore(instance->host->host_lock, flags);
  2765. break;
  2766. }
  2767. /*
  2768. * See if got an event notification
  2769. */
  2770. if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
  2771. megasas_service_aen(instance, cmd);
  2772. else
  2773. megasas_complete_int_cmd(instance, cmd);
  2774. break;
  2775. case MFI_CMD_ABORT:
  2776. /*
  2777. * Cmd issued to abort another cmd returned
  2778. */
  2779. megasas_complete_abort(instance, cmd);
  2780. break;
  2781. default:
  2782. dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
  2783. hdr->cmd);
  2784. break;
  2785. }
  2786. }
  2787. /**
  2788. * megasas_issue_pending_cmds_again - issue all pending cmds
  2789. * in FW again because of the fw reset
  2790. * @instance: Adapter soft state
  2791. */
  2792. static inline void
  2793. megasas_issue_pending_cmds_again(struct megasas_instance *instance)
  2794. {
  2795. struct megasas_cmd *cmd;
  2796. struct list_head clist_local;
  2797. union megasas_evt_class_locale class_locale;
  2798. unsigned long flags;
  2799. u32 seq_num;
  2800. INIT_LIST_HEAD(&clist_local);
  2801. spin_lock_irqsave(&instance->hba_lock, flags);
  2802. list_splice_init(&instance->internal_reset_pending_q, &clist_local);
  2803. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2804. while (!list_empty(&clist_local)) {
  2805. cmd = list_entry((&clist_local)->next,
  2806. struct megasas_cmd, list);
  2807. list_del_init(&cmd->list);
  2808. if (cmd->sync_cmd || cmd->scmd) {
  2809. dev_notice(&instance->pdev->dev, "command %p, %p:%d"
  2810. "detected to be pending while HBA reset\n",
  2811. cmd, cmd->scmd, cmd->sync_cmd);
  2812. cmd->retry_for_fw_reset++;
  2813. if (cmd->retry_for_fw_reset == 3) {
  2814. dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
  2815. "was tried multiple times during reset."
  2816. "Shutting down the HBA\n",
  2817. cmd, cmd->scmd, cmd->sync_cmd);
  2818. instance->instancet->disable_intr(instance);
  2819. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2820. megaraid_sas_kill_hba(instance);
  2821. return;
  2822. }
  2823. }
  2824. if (cmd->sync_cmd == 1) {
  2825. if (cmd->scmd) {
  2826. dev_notice(&instance->pdev->dev, "unexpected"
  2827. "cmd attached to internal command!\n");
  2828. }
  2829. dev_notice(&instance->pdev->dev, "%p synchronous cmd"
  2830. "on the internal reset queue,"
  2831. "issue it again.\n", cmd);
  2832. cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  2833. instance->instancet->fire_cmd(instance,
  2834. cmd->frame_phys_addr,
  2835. 0, instance->reg_set);
  2836. } else if (cmd->scmd) {
  2837. dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
  2838. "detected on the internal queue, issue again.\n",
  2839. cmd, cmd->scmd->cmnd[0]);
  2840. atomic_inc(&instance->fw_outstanding);
  2841. instance->instancet->fire_cmd(instance,
  2842. cmd->frame_phys_addr,
  2843. cmd->frame_count-1, instance->reg_set);
  2844. } else {
  2845. dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
  2846. "internal reset defer list while re-issue!!\n",
  2847. cmd);
  2848. }
  2849. }
  2850. if (instance->aen_cmd) {
  2851. dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
  2852. megasas_return_cmd(instance, instance->aen_cmd);
  2853. instance->aen_cmd = NULL;
  2854. }
  2855. /*
  2856. * Initiate AEN (Asynchronous Event Notification)
  2857. */
  2858. seq_num = instance->last_seq_num;
  2859. class_locale.members.reserved = 0;
  2860. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2861. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2862. megasas_register_aen(instance, seq_num, class_locale.word);
  2863. }
  2864. /**
  2865. * Move the internal reset pending commands to a deferred queue.
  2866. *
  2867. * We move the commands pending at internal reset time to a
  2868. * pending queue. This queue would be flushed after successful
  2869. * completion of the internal reset sequence. if the internal reset
  2870. * did not complete in time, the kernel reset handler would flush
  2871. * these commands.
  2872. **/
  2873. static void
  2874. megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
  2875. {
  2876. struct megasas_cmd *cmd;
  2877. int i;
  2878. u32 max_cmd = instance->max_fw_cmds;
  2879. u32 defer_index;
  2880. unsigned long flags;
  2881. defer_index = 0;
  2882. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  2883. for (i = 0; i < max_cmd; i++) {
  2884. cmd = instance->cmd_list[i];
  2885. if (cmd->sync_cmd == 1 || cmd->scmd) {
  2886. dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
  2887. "on the defer queue as internal\n",
  2888. defer_index, cmd, cmd->sync_cmd, cmd->scmd);
  2889. if (!list_empty(&cmd->list)) {
  2890. dev_notice(&instance->pdev->dev, "ERROR while"
  2891. " moving this cmd:%p, %d %p, it was"
  2892. "discovered on some list?\n",
  2893. cmd, cmd->sync_cmd, cmd->scmd);
  2894. list_del_init(&cmd->list);
  2895. }
  2896. defer_index++;
  2897. list_add_tail(&cmd->list,
  2898. &instance->internal_reset_pending_q);
  2899. }
  2900. }
  2901. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  2902. }
  2903. static void
  2904. process_fw_state_change_wq(struct work_struct *work)
  2905. {
  2906. struct megasas_instance *instance =
  2907. container_of(work, struct megasas_instance, work_init);
  2908. u32 wait;
  2909. unsigned long flags;
  2910. if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
  2911. dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
  2912. atomic_read(&instance->adprecovery));
  2913. return ;
  2914. }
  2915. if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
  2916. dev_notice(&instance->pdev->dev, "FW detected to be in fault"
  2917. "state, restarting it...\n");
  2918. instance->instancet->disable_intr(instance);
  2919. atomic_set(&instance->fw_outstanding, 0);
  2920. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2921. instance->instancet->adp_reset(instance, instance->reg_set);
  2922. atomic_set(&instance->fw_reset_no_pci_access, 0);
  2923. dev_notice(&instance->pdev->dev, "FW restarted successfully,"
  2924. "initiating next stage...\n");
  2925. dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
  2926. "state 2 starting...\n");
  2927. /* waiting for about 20 second before start the second init */
  2928. for (wait = 0; wait < 30; wait++) {
  2929. msleep(1000);
  2930. }
  2931. if (megasas_transition_to_ready(instance, 1)) {
  2932. dev_notice(&instance->pdev->dev, "adapter not ready\n");
  2933. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2934. megaraid_sas_kill_hba(instance);
  2935. return ;
  2936. }
  2937. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  2938. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  2939. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
  2940. ) {
  2941. *instance->consumer = *instance->producer;
  2942. } else {
  2943. *instance->consumer = 0;
  2944. *instance->producer = 0;
  2945. }
  2946. megasas_issue_init_mfi(instance);
  2947. spin_lock_irqsave(&instance->hba_lock, flags);
  2948. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  2949. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2950. instance->instancet->enable_intr(instance);
  2951. megasas_issue_pending_cmds_again(instance);
  2952. instance->issuepend_done = 1;
  2953. }
  2954. }
  2955. /**
  2956. * megasas_deplete_reply_queue - Processes all completed commands
  2957. * @instance: Adapter soft state
  2958. * @alt_status: Alternate status to be returned to
  2959. * SCSI mid-layer instead of the status
  2960. * returned by the FW
  2961. * Note: this must be called with hba lock held
  2962. */
  2963. static int
  2964. megasas_deplete_reply_queue(struct megasas_instance *instance,
  2965. u8 alt_status)
  2966. {
  2967. u32 mfiStatus;
  2968. u32 fw_state;
  2969. if ((mfiStatus = instance->instancet->check_reset(instance,
  2970. instance->reg_set)) == 1) {
  2971. return IRQ_HANDLED;
  2972. }
  2973. if ((mfiStatus = instance->instancet->clear_intr(
  2974. instance->reg_set)
  2975. ) == 0) {
  2976. /* Hardware may not set outbound_intr_status in MSI-X mode */
  2977. if (!instance->msix_vectors)
  2978. return IRQ_NONE;
  2979. }
  2980. instance->mfiStatus = mfiStatus;
  2981. if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
  2982. fw_state = instance->instancet->read_fw_status_reg(
  2983. instance->reg_set) & MFI_STATE_MASK;
  2984. if (fw_state != MFI_STATE_FAULT) {
  2985. dev_notice(&instance->pdev->dev, "fw state:%x\n",
  2986. fw_state);
  2987. }
  2988. if ((fw_state == MFI_STATE_FAULT) &&
  2989. (instance->disableOnlineCtrlReset == 0)) {
  2990. dev_notice(&instance->pdev->dev, "wait adp restart\n");
  2991. if ((instance->pdev->device ==
  2992. PCI_DEVICE_ID_LSI_SAS1064R) ||
  2993. (instance->pdev->device ==
  2994. PCI_DEVICE_ID_DELL_PERC5) ||
  2995. (instance->pdev->device ==
  2996. PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  2997. *instance->consumer =
  2998. cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
  2999. }
  3000. instance->instancet->disable_intr(instance);
  3001. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  3002. instance->issuepend_done = 0;
  3003. atomic_set(&instance->fw_outstanding, 0);
  3004. megasas_internal_reset_defer_cmds(instance);
  3005. dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
  3006. fw_state, atomic_read(&instance->adprecovery));
  3007. schedule_work(&instance->work_init);
  3008. return IRQ_HANDLED;
  3009. } else {
  3010. dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
  3011. fw_state, instance->disableOnlineCtrlReset);
  3012. }
  3013. }
  3014. tasklet_schedule(&instance->isr_tasklet);
  3015. return IRQ_HANDLED;
  3016. }
  3017. /**
  3018. * megasas_isr - isr entry point
  3019. */
  3020. static irqreturn_t megasas_isr(int irq, void *devp)
  3021. {
  3022. struct megasas_irq_context *irq_context = devp;
  3023. struct megasas_instance *instance = irq_context->instance;
  3024. unsigned long flags;
  3025. irqreturn_t rc;
  3026. if (atomic_read(&instance->fw_reset_no_pci_access))
  3027. return IRQ_HANDLED;
  3028. spin_lock_irqsave(&instance->hba_lock, flags);
  3029. rc = megasas_deplete_reply_queue(instance, DID_OK);
  3030. spin_unlock_irqrestore(&instance->hba_lock, flags);
  3031. return rc;
  3032. }
  3033. /**
  3034. * megasas_transition_to_ready - Move the FW to READY state
  3035. * @instance: Adapter soft state
  3036. *
  3037. * During the initialization, FW passes can potentially be in any one of
  3038. * several possible states. If the FW in operational, waiting-for-handshake
  3039. * states, driver must take steps to bring it to ready state. Otherwise, it
  3040. * has to wait for the ready state.
  3041. */
  3042. int
  3043. megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
  3044. {
  3045. int i;
  3046. u8 max_wait;
  3047. u32 fw_state;
  3048. u32 cur_state;
  3049. u32 abs_state, curr_abs_state;
  3050. abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
  3051. fw_state = abs_state & MFI_STATE_MASK;
  3052. if (fw_state != MFI_STATE_READY)
  3053. dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
  3054. " state\n");
  3055. while (fw_state != MFI_STATE_READY) {
  3056. switch (fw_state) {
  3057. case MFI_STATE_FAULT:
  3058. dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
  3059. if (ocr) {
  3060. max_wait = MEGASAS_RESET_WAIT_TIME;
  3061. cur_state = MFI_STATE_FAULT;
  3062. break;
  3063. } else
  3064. return -ENODEV;
  3065. case MFI_STATE_WAIT_HANDSHAKE:
  3066. /*
  3067. * Set the CLR bit in inbound doorbell
  3068. */
  3069. if ((instance->pdev->device ==
  3070. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3071. (instance->pdev->device ==
  3072. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  3073. (instance->ctrl_context))
  3074. writel(
  3075. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  3076. &instance->reg_set->doorbell);
  3077. else
  3078. writel(
  3079. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  3080. &instance->reg_set->inbound_doorbell);
  3081. max_wait = MEGASAS_RESET_WAIT_TIME;
  3082. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  3083. break;
  3084. case MFI_STATE_BOOT_MESSAGE_PENDING:
  3085. if ((instance->pdev->device ==
  3086. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3087. (instance->pdev->device ==
  3088. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  3089. (instance->ctrl_context))
  3090. writel(MFI_INIT_HOTPLUG,
  3091. &instance->reg_set->doorbell);
  3092. else
  3093. writel(MFI_INIT_HOTPLUG,
  3094. &instance->reg_set->inbound_doorbell);
  3095. max_wait = MEGASAS_RESET_WAIT_TIME;
  3096. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  3097. break;
  3098. case MFI_STATE_OPERATIONAL:
  3099. /*
  3100. * Bring it to READY state; assuming max wait 10 secs
  3101. */
  3102. instance->instancet->disable_intr(instance);
  3103. if ((instance->pdev->device ==
  3104. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3105. (instance->pdev->device ==
  3106. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  3107. (instance->ctrl_context)) {
  3108. writel(MFI_RESET_FLAGS,
  3109. &instance->reg_set->doorbell);
  3110. if (instance->ctrl_context) {
  3111. for (i = 0; i < (10 * 1000); i += 20) {
  3112. if (readl(
  3113. &instance->
  3114. reg_set->
  3115. doorbell) & 1)
  3116. msleep(20);
  3117. else
  3118. break;
  3119. }
  3120. }
  3121. } else
  3122. writel(MFI_RESET_FLAGS,
  3123. &instance->reg_set->inbound_doorbell);
  3124. max_wait = MEGASAS_RESET_WAIT_TIME;
  3125. cur_state = MFI_STATE_OPERATIONAL;
  3126. break;
  3127. case MFI_STATE_UNDEFINED:
  3128. /*
  3129. * This state should not last for more than 2 seconds
  3130. */
  3131. max_wait = MEGASAS_RESET_WAIT_TIME;
  3132. cur_state = MFI_STATE_UNDEFINED;
  3133. break;
  3134. case MFI_STATE_BB_INIT:
  3135. max_wait = MEGASAS_RESET_WAIT_TIME;
  3136. cur_state = MFI_STATE_BB_INIT;
  3137. break;
  3138. case MFI_STATE_FW_INIT:
  3139. max_wait = MEGASAS_RESET_WAIT_TIME;
  3140. cur_state = MFI_STATE_FW_INIT;
  3141. break;
  3142. case MFI_STATE_FW_INIT_2:
  3143. max_wait = MEGASAS_RESET_WAIT_TIME;
  3144. cur_state = MFI_STATE_FW_INIT_2;
  3145. break;
  3146. case MFI_STATE_DEVICE_SCAN:
  3147. max_wait = MEGASAS_RESET_WAIT_TIME;
  3148. cur_state = MFI_STATE_DEVICE_SCAN;
  3149. break;
  3150. case MFI_STATE_FLUSH_CACHE:
  3151. max_wait = MEGASAS_RESET_WAIT_TIME;
  3152. cur_state = MFI_STATE_FLUSH_CACHE;
  3153. break;
  3154. default:
  3155. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
  3156. fw_state);
  3157. return -ENODEV;
  3158. }
  3159. /*
  3160. * The cur_state should not last for more than max_wait secs
  3161. */
  3162. for (i = 0; i < (max_wait * 1000); i++) {
  3163. curr_abs_state = instance->instancet->
  3164. read_fw_status_reg(instance->reg_set);
  3165. if (abs_state == curr_abs_state) {
  3166. msleep(1);
  3167. } else
  3168. break;
  3169. }
  3170. /*
  3171. * Return error if fw_state hasn't changed after max_wait
  3172. */
  3173. if (curr_abs_state == abs_state) {
  3174. dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
  3175. "in %d secs\n", fw_state, max_wait);
  3176. return -ENODEV;
  3177. }
  3178. abs_state = curr_abs_state;
  3179. fw_state = curr_abs_state & MFI_STATE_MASK;
  3180. }
  3181. dev_info(&instance->pdev->dev, "FW now in Ready state\n");
  3182. return 0;
  3183. }
  3184. /**
  3185. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  3186. * @instance: Adapter soft state
  3187. */
  3188. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  3189. {
  3190. int i;
  3191. u32 max_cmd = instance->max_mfi_cmds;
  3192. struct megasas_cmd *cmd;
  3193. if (!instance->frame_dma_pool)
  3194. return;
  3195. /*
  3196. * Return all frames to pool
  3197. */
  3198. for (i = 0; i < max_cmd; i++) {
  3199. cmd = instance->cmd_list[i];
  3200. if (cmd->frame)
  3201. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  3202. cmd->frame_phys_addr);
  3203. if (cmd->sense)
  3204. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  3205. cmd->sense_phys_addr);
  3206. }
  3207. /*
  3208. * Now destroy the pool itself
  3209. */
  3210. pci_pool_destroy(instance->frame_dma_pool);
  3211. pci_pool_destroy(instance->sense_dma_pool);
  3212. instance->frame_dma_pool = NULL;
  3213. instance->sense_dma_pool = NULL;
  3214. }
  3215. /**
  3216. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  3217. * @instance: Adapter soft state
  3218. *
  3219. * Each command packet has an embedded DMA memory buffer that is used for
  3220. * filling MFI frame and the SG list that immediately follows the frame. This
  3221. * function creates those DMA memory buffers for each command packet by using
  3222. * PCI pool facility.
  3223. */
  3224. static int megasas_create_frame_pool(struct megasas_instance *instance)
  3225. {
  3226. int i;
  3227. u32 max_cmd;
  3228. u32 sge_sz;
  3229. u32 total_sz;
  3230. u32 frame_count;
  3231. struct megasas_cmd *cmd;
  3232. max_cmd = instance->max_mfi_cmds;
  3233. /*
  3234. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  3235. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  3236. */
  3237. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  3238. sizeof(struct megasas_sge32);
  3239. if (instance->flag_ieee)
  3240. sge_sz = sizeof(struct megasas_sge_skinny);
  3241. /*
  3242. * For MFI controllers.
  3243. * max_num_sge = 60
  3244. * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
  3245. * Total 960 byte (15 MFI frame of 64 byte)
  3246. *
  3247. * Fusion adapter require only 3 extra frame.
  3248. * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
  3249. * max_sge_sz = 12 byte (sizeof megasas_sge64)
  3250. * Total 192 byte (3 MFI frame of 64 byte)
  3251. */
  3252. frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
  3253. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  3254. /*
  3255. * Use DMA pool facility provided by PCI layer
  3256. */
  3257. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  3258. instance->pdev, total_sz, 256, 0);
  3259. if (!instance->frame_dma_pool) {
  3260. dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
  3261. return -ENOMEM;
  3262. }
  3263. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  3264. instance->pdev, 128, 4, 0);
  3265. if (!instance->sense_dma_pool) {
  3266. dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
  3267. pci_pool_destroy(instance->frame_dma_pool);
  3268. instance->frame_dma_pool = NULL;
  3269. return -ENOMEM;
  3270. }
  3271. /*
  3272. * Allocate and attach a frame to each of the commands in cmd_list.
  3273. * By making cmd->index as the context instead of the &cmd, we can
  3274. * always use 32bit context regardless of the architecture
  3275. */
  3276. for (i = 0; i < max_cmd; i++) {
  3277. cmd = instance->cmd_list[i];
  3278. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  3279. GFP_KERNEL, &cmd->frame_phys_addr);
  3280. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  3281. GFP_KERNEL, &cmd->sense_phys_addr);
  3282. /*
  3283. * megasas_teardown_frame_pool() takes care of freeing
  3284. * whatever has been allocated
  3285. */
  3286. if (!cmd->frame || !cmd->sense) {
  3287. dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
  3288. megasas_teardown_frame_pool(instance);
  3289. return -ENOMEM;
  3290. }
  3291. memset(cmd->frame, 0, total_sz);
  3292. cmd->frame->io.context = cpu_to_le32(cmd->index);
  3293. cmd->frame->io.pad_0 = 0;
  3294. if (!instance->ctrl_context && reset_devices)
  3295. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  3296. }
  3297. return 0;
  3298. }
  3299. /**
  3300. * megasas_free_cmds - Free all the cmds in the free cmd pool
  3301. * @instance: Adapter soft state
  3302. */
  3303. void megasas_free_cmds(struct megasas_instance *instance)
  3304. {
  3305. int i;
  3306. /* First free the MFI frame pool */
  3307. megasas_teardown_frame_pool(instance);
  3308. /* Free all the commands in the cmd_list */
  3309. for (i = 0; i < instance->max_mfi_cmds; i++)
  3310. kfree(instance->cmd_list[i]);
  3311. /* Free the cmd_list buffer itself */
  3312. kfree(instance->cmd_list);
  3313. instance->cmd_list = NULL;
  3314. INIT_LIST_HEAD(&instance->cmd_pool);
  3315. }
  3316. /**
  3317. * megasas_alloc_cmds - Allocates the command packets
  3318. * @instance: Adapter soft state
  3319. *
  3320. * Each command that is issued to the FW, whether IO commands from the OS or
  3321. * internal commands like IOCTLs, are wrapped in local data structure called
  3322. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  3323. * the FW.
  3324. *
  3325. * Each frame has a 32-bit field called context (tag). This context is used
  3326. * to get back the megasas_cmd from the frame when a frame gets completed in
  3327. * the ISR. Typically the address of the megasas_cmd itself would be used as
  3328. * the context. But we wanted to keep the differences between 32 and 64 bit
  3329. * systems to the mininum. We always use 32 bit integers for the context. In
  3330. * this driver, the 32 bit values are the indices into an array cmd_list.
  3331. * This array is used only to look up the megasas_cmd given the context. The
  3332. * free commands themselves are maintained in a linked list called cmd_pool.
  3333. */
  3334. int megasas_alloc_cmds(struct megasas_instance *instance)
  3335. {
  3336. int i;
  3337. int j;
  3338. u32 max_cmd;
  3339. struct megasas_cmd *cmd;
  3340. struct fusion_context *fusion;
  3341. fusion = instance->ctrl_context;
  3342. max_cmd = instance->max_mfi_cmds;
  3343. /*
  3344. * instance->cmd_list is an array of struct megasas_cmd pointers.
  3345. * Allocate the dynamic array first and then allocate individual
  3346. * commands.
  3347. */
  3348. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  3349. if (!instance->cmd_list) {
  3350. dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
  3351. return -ENOMEM;
  3352. }
  3353. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
  3354. for (i = 0; i < max_cmd; i++) {
  3355. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  3356. GFP_KERNEL);
  3357. if (!instance->cmd_list[i]) {
  3358. for (j = 0; j < i; j++)
  3359. kfree(instance->cmd_list[j]);
  3360. kfree(instance->cmd_list);
  3361. instance->cmd_list = NULL;
  3362. return -ENOMEM;
  3363. }
  3364. }
  3365. for (i = 0; i < max_cmd; i++) {
  3366. cmd = instance->cmd_list[i];
  3367. memset(cmd, 0, sizeof(struct megasas_cmd));
  3368. cmd->index = i;
  3369. cmd->scmd = NULL;
  3370. cmd->instance = instance;
  3371. list_add_tail(&cmd->list, &instance->cmd_pool);
  3372. }
  3373. /*
  3374. * Create a frame pool and assign one frame to each cmd
  3375. */
  3376. if (megasas_create_frame_pool(instance)) {
  3377. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
  3378. megasas_free_cmds(instance);
  3379. }
  3380. return 0;
  3381. }
  3382. /*
  3383. * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
  3384. * @instance: Adapter soft state
  3385. *
  3386. * Return 0 for only Fusion adapter, if driver load/unload is not in progress
  3387. * or FW is not under OCR.
  3388. */
  3389. inline int
  3390. dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
  3391. if (!instance->ctrl_context)
  3392. return KILL_ADAPTER;
  3393. else if (instance->unload ||
  3394. test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
  3395. return IGNORE_TIMEOUT;
  3396. else
  3397. return INITIATE_OCR;
  3398. }
  3399. static int
  3400. megasas_get_pd_info(struct megasas_instance *instance, u16 device_id)
  3401. {
  3402. int ret;
  3403. struct megasas_cmd *cmd;
  3404. struct megasas_dcmd_frame *dcmd;
  3405. cmd = megasas_get_cmd(instance);
  3406. if (!cmd) {
  3407. dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
  3408. return -ENOMEM;
  3409. }
  3410. dcmd = &cmd->frame->dcmd;
  3411. memset(instance->pd_info, 0, sizeof(*instance->pd_info));
  3412. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3413. dcmd->mbox.s[0] = cpu_to_le16(device_id);
  3414. dcmd->cmd = MFI_CMD_DCMD;
  3415. dcmd->cmd_status = 0xFF;
  3416. dcmd->sge_count = 1;
  3417. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3418. dcmd->timeout = 0;
  3419. dcmd->pad_0 = 0;
  3420. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
  3421. dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
  3422. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
  3423. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
  3424. if (instance->ctrl_context && !instance->mask_interrupts)
  3425. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3426. else
  3427. ret = megasas_issue_polled(instance, cmd);
  3428. switch (ret) {
  3429. case DCMD_SUCCESS:
  3430. instance->pd_list[device_id].interface =
  3431. instance->pd_info->state.ddf.pdType.intf;
  3432. break;
  3433. case DCMD_TIMEOUT:
  3434. switch (dcmd_timeout_ocr_possible(instance)) {
  3435. case INITIATE_OCR:
  3436. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3437. megasas_reset_fusion(instance->host,
  3438. MFI_IO_TIMEOUT_OCR);
  3439. break;
  3440. case KILL_ADAPTER:
  3441. megaraid_sas_kill_hba(instance);
  3442. break;
  3443. case IGNORE_TIMEOUT:
  3444. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3445. __func__, __LINE__);
  3446. break;
  3447. }
  3448. break;
  3449. }
  3450. if (ret != DCMD_TIMEOUT)
  3451. megasas_return_cmd(instance, cmd);
  3452. return ret;
  3453. }
  3454. /*
  3455. * megasas_get_pd_list_info - Returns FW's pd_list structure
  3456. * @instance: Adapter soft state
  3457. * @pd_list: pd_list structure
  3458. *
  3459. * Issues an internal command (DCMD) to get the FW's controller PD
  3460. * list structure. This information is mainly used to find out SYSTEM
  3461. * supported by the FW.
  3462. */
  3463. static int
  3464. megasas_get_pd_list(struct megasas_instance *instance)
  3465. {
  3466. int ret = 0, pd_index = 0;
  3467. struct megasas_cmd *cmd;
  3468. struct megasas_dcmd_frame *dcmd;
  3469. struct MR_PD_LIST *ci;
  3470. struct MR_PD_ADDRESS *pd_addr;
  3471. dma_addr_t ci_h = 0;
  3472. if (instance->pd_list_not_supported) {
  3473. dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
  3474. "not supported by firmware\n");
  3475. return ret;
  3476. }
  3477. cmd = megasas_get_cmd(instance);
  3478. if (!cmd) {
  3479. dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
  3480. return -ENOMEM;
  3481. }
  3482. dcmd = &cmd->frame->dcmd;
  3483. ci = pci_alloc_consistent(instance->pdev,
  3484. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  3485. if (!ci) {
  3486. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
  3487. megasas_return_cmd(instance, cmd);
  3488. return -ENOMEM;
  3489. }
  3490. memset(ci, 0, sizeof(*ci));
  3491. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3492. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  3493. dcmd->mbox.b[1] = 0;
  3494. dcmd->cmd = MFI_CMD_DCMD;
  3495. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3496. dcmd->sge_count = 1;
  3497. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3498. dcmd->timeout = 0;
  3499. dcmd->pad_0 = 0;
  3500. dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
  3501. dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
  3502. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  3503. dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
  3504. if (instance->ctrl_context && !instance->mask_interrupts)
  3505. ret = megasas_issue_blocked_cmd(instance, cmd,
  3506. MFI_IO_TIMEOUT_SECS);
  3507. else
  3508. ret = megasas_issue_polled(instance, cmd);
  3509. switch (ret) {
  3510. case DCMD_FAILED:
  3511. dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
  3512. "failed/not supported by firmware\n");
  3513. if (instance->ctrl_context)
  3514. megaraid_sas_kill_hba(instance);
  3515. else
  3516. instance->pd_list_not_supported = 1;
  3517. break;
  3518. case DCMD_TIMEOUT:
  3519. switch (dcmd_timeout_ocr_possible(instance)) {
  3520. case INITIATE_OCR:
  3521. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3522. /*
  3523. * DCMD failed from AEN path.
  3524. * AEN path already hold reset_mutex to avoid PCI access
  3525. * while OCR is in progress.
  3526. */
  3527. mutex_unlock(&instance->reset_mutex);
  3528. megasas_reset_fusion(instance->host,
  3529. MFI_IO_TIMEOUT_OCR);
  3530. mutex_lock(&instance->reset_mutex);
  3531. break;
  3532. case KILL_ADAPTER:
  3533. megaraid_sas_kill_hba(instance);
  3534. break;
  3535. case IGNORE_TIMEOUT:
  3536. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
  3537. __func__, __LINE__);
  3538. break;
  3539. }
  3540. break;
  3541. case DCMD_SUCCESS:
  3542. pd_addr = ci->addr;
  3543. if ((le32_to_cpu(ci->count) >
  3544. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
  3545. break;
  3546. memset(instance->local_pd_list, 0,
  3547. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  3548. for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
  3549. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
  3550. le16_to_cpu(pd_addr->deviceId);
  3551. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
  3552. pd_addr->scsiDevType;
  3553. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
  3554. MR_PD_STATE_SYSTEM;
  3555. pd_addr++;
  3556. }
  3557. memcpy(instance->pd_list, instance->local_pd_list,
  3558. sizeof(instance->pd_list));
  3559. break;
  3560. }
  3561. pci_free_consistent(instance->pdev,
  3562. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  3563. ci, ci_h);
  3564. if (ret != DCMD_TIMEOUT)
  3565. megasas_return_cmd(instance, cmd);
  3566. return ret;
  3567. }
  3568. /*
  3569. * megasas_get_ld_list_info - Returns FW's ld_list structure
  3570. * @instance: Adapter soft state
  3571. * @ld_list: ld_list structure
  3572. *
  3573. * Issues an internal command (DCMD) to get the FW's controller PD
  3574. * list structure. This information is mainly used to find out SYSTEM
  3575. * supported by the FW.
  3576. */
  3577. static int
  3578. megasas_get_ld_list(struct megasas_instance *instance)
  3579. {
  3580. int ret = 0, ld_index = 0, ids = 0;
  3581. struct megasas_cmd *cmd;
  3582. struct megasas_dcmd_frame *dcmd;
  3583. struct MR_LD_LIST *ci;
  3584. dma_addr_t ci_h = 0;
  3585. u32 ld_count;
  3586. cmd = megasas_get_cmd(instance);
  3587. if (!cmd) {
  3588. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
  3589. return -ENOMEM;
  3590. }
  3591. dcmd = &cmd->frame->dcmd;
  3592. ci = pci_alloc_consistent(instance->pdev,
  3593. sizeof(struct MR_LD_LIST),
  3594. &ci_h);
  3595. if (!ci) {
  3596. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
  3597. megasas_return_cmd(instance, cmd);
  3598. return -ENOMEM;
  3599. }
  3600. memset(ci, 0, sizeof(*ci));
  3601. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3602. if (instance->supportmax256vd)
  3603. dcmd->mbox.b[0] = 1;
  3604. dcmd->cmd = MFI_CMD_DCMD;
  3605. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3606. dcmd->sge_count = 1;
  3607. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3608. dcmd->timeout = 0;
  3609. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
  3610. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
  3611. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  3612. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
  3613. dcmd->pad_0 = 0;
  3614. if (instance->ctrl_context && !instance->mask_interrupts)
  3615. ret = megasas_issue_blocked_cmd(instance, cmd,
  3616. MFI_IO_TIMEOUT_SECS);
  3617. else
  3618. ret = megasas_issue_polled(instance, cmd);
  3619. ld_count = le32_to_cpu(ci->ldCount);
  3620. switch (ret) {
  3621. case DCMD_FAILED:
  3622. megaraid_sas_kill_hba(instance);
  3623. break;
  3624. case DCMD_TIMEOUT:
  3625. switch (dcmd_timeout_ocr_possible(instance)) {
  3626. case INITIATE_OCR:
  3627. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3628. /*
  3629. * DCMD failed from AEN path.
  3630. * AEN path already hold reset_mutex to avoid PCI access
  3631. * while OCR is in progress.
  3632. */
  3633. mutex_unlock(&instance->reset_mutex);
  3634. megasas_reset_fusion(instance->host,
  3635. MFI_IO_TIMEOUT_OCR);
  3636. mutex_lock(&instance->reset_mutex);
  3637. break;
  3638. case KILL_ADAPTER:
  3639. megaraid_sas_kill_hba(instance);
  3640. break;
  3641. case IGNORE_TIMEOUT:
  3642. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3643. __func__, __LINE__);
  3644. break;
  3645. }
  3646. break;
  3647. case DCMD_SUCCESS:
  3648. if (ld_count > instance->fw_supported_vd_count)
  3649. break;
  3650. memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
  3651. for (ld_index = 0; ld_index < ld_count; ld_index++) {
  3652. if (ci->ldList[ld_index].state != 0) {
  3653. ids = ci->ldList[ld_index].ref.targetId;
  3654. instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
  3655. }
  3656. }
  3657. break;
  3658. }
  3659. pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
  3660. if (ret != DCMD_TIMEOUT)
  3661. megasas_return_cmd(instance, cmd);
  3662. return ret;
  3663. }
  3664. /**
  3665. * megasas_ld_list_query - Returns FW's ld_list structure
  3666. * @instance: Adapter soft state
  3667. * @ld_list: ld_list structure
  3668. *
  3669. * Issues an internal command (DCMD) to get the FW's controller PD
  3670. * list structure. This information is mainly used to find out SYSTEM
  3671. * supported by the FW.
  3672. */
  3673. static int
  3674. megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
  3675. {
  3676. int ret = 0, ld_index = 0, ids = 0;
  3677. struct megasas_cmd *cmd;
  3678. struct megasas_dcmd_frame *dcmd;
  3679. struct MR_LD_TARGETID_LIST *ci;
  3680. dma_addr_t ci_h = 0;
  3681. u32 tgtid_count;
  3682. cmd = megasas_get_cmd(instance);
  3683. if (!cmd) {
  3684. dev_warn(&instance->pdev->dev,
  3685. "megasas_ld_list_query: Failed to get cmd\n");
  3686. return -ENOMEM;
  3687. }
  3688. dcmd = &cmd->frame->dcmd;
  3689. ci = pci_alloc_consistent(instance->pdev,
  3690. sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
  3691. if (!ci) {
  3692. dev_warn(&instance->pdev->dev,
  3693. "Failed to alloc mem for ld_list_query\n");
  3694. megasas_return_cmd(instance, cmd);
  3695. return -ENOMEM;
  3696. }
  3697. memset(ci, 0, sizeof(*ci));
  3698. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3699. dcmd->mbox.b[0] = query_type;
  3700. if (instance->supportmax256vd)
  3701. dcmd->mbox.b[2] = 1;
  3702. dcmd->cmd = MFI_CMD_DCMD;
  3703. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3704. dcmd->sge_count = 1;
  3705. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3706. dcmd->timeout = 0;
  3707. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
  3708. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
  3709. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  3710. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
  3711. dcmd->pad_0 = 0;
  3712. if (instance->ctrl_context && !instance->mask_interrupts)
  3713. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3714. else
  3715. ret = megasas_issue_polled(instance, cmd);
  3716. switch (ret) {
  3717. case DCMD_FAILED:
  3718. dev_info(&instance->pdev->dev,
  3719. "DCMD not supported by firmware - %s %d\n",
  3720. __func__, __LINE__);
  3721. ret = megasas_get_ld_list(instance);
  3722. break;
  3723. case DCMD_TIMEOUT:
  3724. switch (dcmd_timeout_ocr_possible(instance)) {
  3725. case INITIATE_OCR:
  3726. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3727. /*
  3728. * DCMD failed from AEN path.
  3729. * AEN path already hold reset_mutex to avoid PCI access
  3730. * while OCR is in progress.
  3731. */
  3732. mutex_unlock(&instance->reset_mutex);
  3733. megasas_reset_fusion(instance->host,
  3734. MFI_IO_TIMEOUT_OCR);
  3735. mutex_lock(&instance->reset_mutex);
  3736. break;
  3737. case KILL_ADAPTER:
  3738. megaraid_sas_kill_hba(instance);
  3739. break;
  3740. case IGNORE_TIMEOUT:
  3741. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3742. __func__, __LINE__);
  3743. break;
  3744. }
  3745. break;
  3746. case DCMD_SUCCESS:
  3747. tgtid_count = le32_to_cpu(ci->count);
  3748. if ((tgtid_count > (instance->fw_supported_vd_count)))
  3749. break;
  3750. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  3751. for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
  3752. ids = ci->targetId[ld_index];
  3753. instance->ld_ids[ids] = ci->targetId[ld_index];
  3754. }
  3755. break;
  3756. }
  3757. pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
  3758. ci, ci_h);
  3759. if (ret != DCMD_TIMEOUT)
  3760. megasas_return_cmd(instance, cmd);
  3761. return ret;
  3762. }
  3763. /*
  3764. * megasas_update_ext_vd_details : Update details w.r.t Extended VD
  3765. * instance : Controller's instance
  3766. */
  3767. static void megasas_update_ext_vd_details(struct megasas_instance *instance)
  3768. {
  3769. struct fusion_context *fusion;
  3770. u32 old_map_sz;
  3771. u32 new_map_sz;
  3772. fusion = instance->ctrl_context;
  3773. /* For MFI based controllers return dummy success */
  3774. if (!fusion)
  3775. return;
  3776. instance->supportmax256vd =
  3777. instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
  3778. /* Below is additional check to address future FW enhancement */
  3779. if (instance->ctrl_info->max_lds > 64)
  3780. instance->supportmax256vd = 1;
  3781. instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
  3782. * MEGASAS_MAX_DEV_PER_CHANNEL;
  3783. instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
  3784. * MEGASAS_MAX_DEV_PER_CHANNEL;
  3785. if (instance->supportmax256vd) {
  3786. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
  3787. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  3788. } else {
  3789. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
  3790. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  3791. }
  3792. dev_info(&instance->pdev->dev,
  3793. "firmware type\t: %s\n",
  3794. instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
  3795. "Legacy(64 VD) firmware");
  3796. old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
  3797. (sizeof(struct MR_LD_SPAN_MAP) *
  3798. (instance->fw_supported_vd_count - 1));
  3799. new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
  3800. fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
  3801. (sizeof(struct MR_LD_SPAN_MAP) *
  3802. (instance->drv_supported_vd_count - 1));
  3803. fusion->max_map_sz = max(old_map_sz, new_map_sz);
  3804. if (instance->supportmax256vd)
  3805. fusion->current_map_sz = new_map_sz;
  3806. else
  3807. fusion->current_map_sz = old_map_sz;
  3808. }
  3809. /**
  3810. * megasas_get_controller_info - Returns FW's controller structure
  3811. * @instance: Adapter soft state
  3812. *
  3813. * Issues an internal command (DCMD) to get the FW's controller structure.
  3814. * This information is mainly used to find out the maximum IO transfer per
  3815. * command supported by the FW.
  3816. */
  3817. int
  3818. megasas_get_ctrl_info(struct megasas_instance *instance)
  3819. {
  3820. int ret = 0;
  3821. struct megasas_cmd *cmd;
  3822. struct megasas_dcmd_frame *dcmd;
  3823. struct megasas_ctrl_info *ci;
  3824. struct megasas_ctrl_info *ctrl_info;
  3825. dma_addr_t ci_h = 0;
  3826. ctrl_info = instance->ctrl_info;
  3827. cmd = megasas_get_cmd(instance);
  3828. if (!cmd) {
  3829. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
  3830. return -ENOMEM;
  3831. }
  3832. dcmd = &cmd->frame->dcmd;
  3833. ci = pci_alloc_consistent(instance->pdev,
  3834. sizeof(struct megasas_ctrl_info), &ci_h);
  3835. if (!ci) {
  3836. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
  3837. megasas_return_cmd(instance, cmd);
  3838. return -ENOMEM;
  3839. }
  3840. memset(ci, 0, sizeof(*ci));
  3841. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3842. dcmd->cmd = MFI_CMD_DCMD;
  3843. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3844. dcmd->sge_count = 1;
  3845. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3846. dcmd->timeout = 0;
  3847. dcmd->pad_0 = 0;
  3848. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
  3849. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
  3850. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  3851. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
  3852. dcmd->mbox.b[0] = 1;
  3853. if (instance->ctrl_context && !instance->mask_interrupts)
  3854. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3855. else
  3856. ret = megasas_issue_polled(instance, cmd);
  3857. switch (ret) {
  3858. case DCMD_SUCCESS:
  3859. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  3860. /* Save required controller information in
  3861. * CPU endianness format.
  3862. */
  3863. le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
  3864. le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
  3865. le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
  3866. /* Update the latest Ext VD info.
  3867. * From Init path, store current firmware details.
  3868. * From OCR path, detect any firmware properties changes.
  3869. * in case of Firmware upgrade without system reboot.
  3870. */
  3871. megasas_update_ext_vd_details(instance);
  3872. instance->use_seqnum_jbod_fp =
  3873. ctrl_info->adapterOperations3.useSeqNumJbodFP;
  3874. /*Check whether controller is iMR or MR */
  3875. instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
  3876. dev_info(&instance->pdev->dev,
  3877. "controller type\t: %s(%dMB)\n",
  3878. instance->is_imr ? "iMR" : "MR",
  3879. le16_to_cpu(ctrl_info->memory_size));
  3880. instance->disableOnlineCtrlReset =
  3881. ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
  3882. instance->secure_jbod_support =
  3883. ctrl_info->adapterOperations3.supportSecurityonJBOD;
  3884. dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
  3885. instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
  3886. dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
  3887. instance->secure_jbod_support ? "Yes" : "No");
  3888. break;
  3889. case DCMD_TIMEOUT:
  3890. switch (dcmd_timeout_ocr_possible(instance)) {
  3891. case INITIATE_OCR:
  3892. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3893. megasas_reset_fusion(instance->host,
  3894. MFI_IO_TIMEOUT_OCR);
  3895. break;
  3896. case KILL_ADAPTER:
  3897. megaraid_sas_kill_hba(instance);
  3898. break;
  3899. case IGNORE_TIMEOUT:
  3900. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3901. __func__, __LINE__);
  3902. break;
  3903. }
  3904. case DCMD_FAILED:
  3905. megaraid_sas_kill_hba(instance);
  3906. break;
  3907. }
  3908. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  3909. ci, ci_h);
  3910. megasas_return_cmd(instance, cmd);
  3911. return ret;
  3912. }
  3913. /*
  3914. * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
  3915. * to firmware
  3916. *
  3917. * @instance: Adapter soft state
  3918. * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
  3919. MR_CRASH_BUF_TURN_OFF = 0
  3920. MR_CRASH_BUF_TURN_ON = 1
  3921. * @return 0 on success non-zero on failure.
  3922. * Issues an internal command (DCMD) to set parameters for crash dump feature.
  3923. * Driver will send address of crash dump DMA buffer and set mbox to tell FW
  3924. * that driver supports crash dump feature. This DCMD will be sent only if
  3925. * crash dump feature is supported by the FW.
  3926. *
  3927. */
  3928. int megasas_set_crash_dump_params(struct megasas_instance *instance,
  3929. u8 crash_buf_state)
  3930. {
  3931. int ret = 0;
  3932. struct megasas_cmd *cmd;
  3933. struct megasas_dcmd_frame *dcmd;
  3934. cmd = megasas_get_cmd(instance);
  3935. if (!cmd) {
  3936. dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
  3937. return -ENOMEM;
  3938. }
  3939. dcmd = &cmd->frame->dcmd;
  3940. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3941. dcmd->mbox.b[0] = crash_buf_state;
  3942. dcmd->cmd = MFI_CMD_DCMD;
  3943. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3944. dcmd->sge_count = 1;
  3945. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  3946. dcmd->timeout = 0;
  3947. dcmd->pad_0 = 0;
  3948. dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
  3949. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
  3950. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
  3951. dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
  3952. if (instance->ctrl_context && !instance->mask_interrupts)
  3953. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3954. else
  3955. ret = megasas_issue_polled(instance, cmd);
  3956. if (ret == DCMD_TIMEOUT) {
  3957. switch (dcmd_timeout_ocr_possible(instance)) {
  3958. case INITIATE_OCR:
  3959. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3960. megasas_reset_fusion(instance->host,
  3961. MFI_IO_TIMEOUT_OCR);
  3962. break;
  3963. case KILL_ADAPTER:
  3964. megaraid_sas_kill_hba(instance);
  3965. break;
  3966. case IGNORE_TIMEOUT:
  3967. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3968. __func__, __LINE__);
  3969. break;
  3970. }
  3971. } else
  3972. megasas_return_cmd(instance, cmd);
  3973. return ret;
  3974. }
  3975. /**
  3976. * megasas_issue_init_mfi - Initializes the FW
  3977. * @instance: Adapter soft state
  3978. *
  3979. * Issues the INIT MFI cmd
  3980. */
  3981. static int
  3982. megasas_issue_init_mfi(struct megasas_instance *instance)
  3983. {
  3984. __le32 context;
  3985. struct megasas_cmd *cmd;
  3986. struct megasas_init_frame *init_frame;
  3987. struct megasas_init_queue_info *initq_info;
  3988. dma_addr_t init_frame_h;
  3989. dma_addr_t initq_info_h;
  3990. /*
  3991. * Prepare a init frame. Note the init frame points to queue info
  3992. * structure. Each frame has SGL allocated after first 64 bytes. For
  3993. * this frame - since we don't need any SGL - we use SGL's space as
  3994. * queue info structure
  3995. *
  3996. * We will not get a NULL command below. We just created the pool.
  3997. */
  3998. cmd = megasas_get_cmd(instance);
  3999. init_frame = (struct megasas_init_frame *)cmd->frame;
  4000. initq_info = (struct megasas_init_queue_info *)
  4001. ((unsigned long)init_frame + 64);
  4002. init_frame_h = cmd->frame_phys_addr;
  4003. initq_info_h = init_frame_h + 64;
  4004. context = init_frame->context;
  4005. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  4006. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  4007. init_frame->context = context;
  4008. initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
  4009. initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
  4010. initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
  4011. initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
  4012. init_frame->cmd = MFI_CMD_INIT;
  4013. init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
  4014. init_frame->queue_info_new_phys_addr_lo =
  4015. cpu_to_le32(lower_32_bits(initq_info_h));
  4016. init_frame->queue_info_new_phys_addr_hi =
  4017. cpu_to_le32(upper_32_bits(initq_info_h));
  4018. init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
  4019. /*
  4020. * disable the intr before firing the init frame to FW
  4021. */
  4022. instance->instancet->disable_intr(instance);
  4023. /*
  4024. * Issue the init frame in polled mode
  4025. */
  4026. if (megasas_issue_polled(instance, cmd)) {
  4027. dev_err(&instance->pdev->dev, "Failed to init firmware\n");
  4028. megasas_return_cmd(instance, cmd);
  4029. goto fail_fw_init;
  4030. }
  4031. megasas_return_cmd(instance, cmd);
  4032. return 0;
  4033. fail_fw_init:
  4034. return -EINVAL;
  4035. }
  4036. static u32
  4037. megasas_init_adapter_mfi(struct megasas_instance *instance)
  4038. {
  4039. struct megasas_register_set __iomem *reg_set;
  4040. u32 context_sz;
  4041. u32 reply_q_sz;
  4042. reg_set = instance->reg_set;
  4043. /*
  4044. * Get various operational parameters from status register
  4045. */
  4046. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  4047. /*
  4048. * Reduce the max supported cmds by 1. This is to ensure that the
  4049. * reply_q_sz (1 more than the max cmd that driver may send)
  4050. * does not exceed max cmds that the FW can support
  4051. */
  4052. instance->max_fw_cmds = instance->max_fw_cmds-1;
  4053. instance->max_mfi_cmds = instance->max_fw_cmds;
  4054. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  4055. 0x10;
  4056. /*
  4057. * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
  4058. * are reserved for IOCTL + driver's internal DCMDs.
  4059. */
  4060. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  4061. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  4062. instance->max_scsi_cmds = (instance->max_fw_cmds -
  4063. MEGASAS_SKINNY_INT_CMDS);
  4064. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  4065. } else {
  4066. instance->max_scsi_cmds = (instance->max_fw_cmds -
  4067. MEGASAS_INT_CMDS);
  4068. sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
  4069. }
  4070. instance->cur_can_queue = instance->max_scsi_cmds;
  4071. /*
  4072. * Create a pool of commands
  4073. */
  4074. if (megasas_alloc_cmds(instance))
  4075. goto fail_alloc_cmds;
  4076. /*
  4077. * Allocate memory for reply queue. Length of reply queue should
  4078. * be _one_ more than the maximum commands handled by the firmware.
  4079. *
  4080. * Note: When FW completes commands, it places corresponding contex
  4081. * values in this circular reply queue. This circular queue is a fairly
  4082. * typical producer-consumer queue. FW is the producer (of completed
  4083. * commands) and the driver is the consumer.
  4084. */
  4085. context_sz = sizeof(u32);
  4086. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  4087. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  4088. reply_q_sz,
  4089. &instance->reply_queue_h);
  4090. if (!instance->reply_queue) {
  4091. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
  4092. goto fail_reply_queue;
  4093. }
  4094. if (megasas_issue_init_mfi(instance))
  4095. goto fail_fw_init;
  4096. if (megasas_get_ctrl_info(instance)) {
  4097. dev_err(&instance->pdev->dev, "(%d): Could get controller info "
  4098. "Fail from %s %d\n", instance->unique_id,
  4099. __func__, __LINE__);
  4100. goto fail_fw_init;
  4101. }
  4102. instance->fw_support_ieee = 0;
  4103. instance->fw_support_ieee =
  4104. (instance->instancet->read_fw_status_reg(reg_set) &
  4105. 0x04000000);
  4106. dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
  4107. instance->fw_support_ieee);
  4108. if (instance->fw_support_ieee)
  4109. instance->flag_ieee = 1;
  4110. return 0;
  4111. fail_fw_init:
  4112. pci_free_consistent(instance->pdev, reply_q_sz,
  4113. instance->reply_queue, instance->reply_queue_h);
  4114. fail_reply_queue:
  4115. megasas_free_cmds(instance);
  4116. fail_alloc_cmds:
  4117. return 1;
  4118. }
  4119. /*
  4120. * megasas_setup_irqs_msix - register legacy interrupts.
  4121. * @instance: Adapter soft state
  4122. *
  4123. * Do not enable interrupt, only setup ISRs.
  4124. *
  4125. * Return 0 on success.
  4126. */
  4127. static int
  4128. megasas_setup_irqs_ioapic(struct megasas_instance *instance)
  4129. {
  4130. struct pci_dev *pdev;
  4131. pdev = instance->pdev;
  4132. instance->irq_context[0].instance = instance;
  4133. instance->irq_context[0].MSIxIndex = 0;
  4134. if (request_irq(pdev->irq, instance->instancet->service_isr,
  4135. IRQF_SHARED, "megasas", &instance->irq_context[0])) {
  4136. dev_err(&instance->pdev->dev,
  4137. "Failed to register IRQ from %s %d\n",
  4138. __func__, __LINE__);
  4139. return -1;
  4140. }
  4141. return 0;
  4142. }
  4143. /**
  4144. * megasas_setup_irqs_msix - register MSI-x interrupts.
  4145. * @instance: Adapter soft state
  4146. * @is_probe: Driver probe check
  4147. *
  4148. * Do not enable interrupt, only setup ISRs.
  4149. *
  4150. * Return 0 on success.
  4151. */
  4152. static int
  4153. megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
  4154. {
  4155. int i, j, cpu;
  4156. struct pci_dev *pdev;
  4157. pdev = instance->pdev;
  4158. /* Try MSI-x */
  4159. cpu = cpumask_first(cpu_online_mask);
  4160. for (i = 0; i < instance->msix_vectors; i++) {
  4161. instance->irq_context[i].instance = instance;
  4162. instance->irq_context[i].MSIxIndex = i;
  4163. if (request_irq(instance->msixentry[i].vector,
  4164. instance->instancet->service_isr, 0, "megasas",
  4165. &instance->irq_context[i])) {
  4166. dev_err(&instance->pdev->dev,
  4167. "Failed to register IRQ for vector %d.\n", i);
  4168. for (j = 0; j < i; j++) {
  4169. if (smp_affinity_enable)
  4170. irq_set_affinity_hint(
  4171. instance->msixentry[j].vector, NULL);
  4172. free_irq(instance->msixentry[j].vector,
  4173. &instance->irq_context[j]);
  4174. }
  4175. /* Retry irq register for IO_APIC*/
  4176. instance->msix_vectors = 0;
  4177. if (is_probe)
  4178. return megasas_setup_irqs_ioapic(instance);
  4179. else
  4180. return -1;
  4181. }
  4182. if (smp_affinity_enable) {
  4183. if (irq_set_affinity_hint(instance->msixentry[i].vector,
  4184. get_cpu_mask(cpu)))
  4185. dev_err(&instance->pdev->dev,
  4186. "Failed to set affinity hint"
  4187. " for cpu %d\n", cpu);
  4188. cpu = cpumask_next(cpu, cpu_online_mask);
  4189. }
  4190. }
  4191. return 0;
  4192. }
  4193. /*
  4194. * megasas_destroy_irqs- unregister interrupts.
  4195. * @instance: Adapter soft state
  4196. * return: void
  4197. */
  4198. static void
  4199. megasas_destroy_irqs(struct megasas_instance *instance) {
  4200. int i;
  4201. if (instance->msix_vectors)
  4202. for (i = 0; i < instance->msix_vectors; i++) {
  4203. if (smp_affinity_enable)
  4204. irq_set_affinity_hint(
  4205. instance->msixentry[i].vector, NULL);
  4206. free_irq(instance->msixentry[i].vector,
  4207. &instance->irq_context[i]);
  4208. }
  4209. else
  4210. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  4211. }
  4212. /**
  4213. * megasas_setup_jbod_map - setup jbod map for FP seq_number.
  4214. * @instance: Adapter soft state
  4215. * @is_probe: Driver probe check
  4216. *
  4217. * Return 0 on success.
  4218. */
  4219. void
  4220. megasas_setup_jbod_map(struct megasas_instance *instance)
  4221. {
  4222. int i;
  4223. struct fusion_context *fusion = instance->ctrl_context;
  4224. u32 pd_seq_map_sz;
  4225. pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
  4226. (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
  4227. if (reset_devices || !fusion ||
  4228. !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
  4229. dev_info(&instance->pdev->dev,
  4230. "Jbod map is not supported %s %d\n",
  4231. __func__, __LINE__);
  4232. instance->use_seqnum_jbod_fp = false;
  4233. return;
  4234. }
  4235. if (fusion->pd_seq_sync[0])
  4236. goto skip_alloc;
  4237. for (i = 0; i < JBOD_MAPS_COUNT; i++) {
  4238. fusion->pd_seq_sync[i] = dma_alloc_coherent
  4239. (&instance->pdev->dev, pd_seq_map_sz,
  4240. &fusion->pd_seq_phys[i], GFP_KERNEL);
  4241. if (!fusion->pd_seq_sync[i]) {
  4242. dev_err(&instance->pdev->dev,
  4243. "Failed to allocate memory from %s %d\n",
  4244. __func__, __LINE__);
  4245. if (i == 1) {
  4246. dma_free_coherent(&instance->pdev->dev,
  4247. pd_seq_map_sz, fusion->pd_seq_sync[0],
  4248. fusion->pd_seq_phys[0]);
  4249. fusion->pd_seq_sync[0] = NULL;
  4250. }
  4251. instance->use_seqnum_jbod_fp = false;
  4252. return;
  4253. }
  4254. }
  4255. skip_alloc:
  4256. if (!megasas_sync_pd_seq_num(instance, false) &&
  4257. !megasas_sync_pd_seq_num(instance, true))
  4258. instance->use_seqnum_jbod_fp = true;
  4259. else
  4260. instance->use_seqnum_jbod_fp = false;
  4261. }
  4262. /**
  4263. * megasas_init_fw - Initializes the FW
  4264. * @instance: Adapter soft state
  4265. *
  4266. * This is the main function for initializing firmware
  4267. */
  4268. static int megasas_init_fw(struct megasas_instance *instance)
  4269. {
  4270. u32 max_sectors_1;
  4271. u32 max_sectors_2;
  4272. u32 tmp_sectors, msix_enable, scratch_pad_2;
  4273. resource_size_t base_addr;
  4274. struct megasas_register_set __iomem *reg_set;
  4275. struct megasas_ctrl_info *ctrl_info = NULL;
  4276. unsigned long bar_list;
  4277. int i, loop, fw_msix_count = 0;
  4278. struct IOV_111 *iovPtr;
  4279. struct fusion_context *fusion;
  4280. fusion = instance->ctrl_context;
  4281. /* Find first memory bar */
  4282. bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
  4283. instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
  4284. if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
  4285. "megasas: LSI")) {
  4286. dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
  4287. return -EBUSY;
  4288. }
  4289. base_addr = pci_resource_start(instance->pdev, instance->bar);
  4290. instance->reg_set = ioremap_nocache(base_addr, 8192);
  4291. if (!instance->reg_set) {
  4292. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
  4293. goto fail_ioremap;
  4294. }
  4295. reg_set = instance->reg_set;
  4296. switch (instance->pdev->device) {
  4297. case PCI_DEVICE_ID_LSI_FUSION:
  4298. case PCI_DEVICE_ID_LSI_PLASMA:
  4299. case PCI_DEVICE_ID_LSI_INVADER:
  4300. case PCI_DEVICE_ID_LSI_FURY:
  4301. case PCI_DEVICE_ID_LSI_INTRUDER:
  4302. case PCI_DEVICE_ID_LSI_INTRUDER_24:
  4303. case PCI_DEVICE_ID_LSI_CUTLASS_52:
  4304. case PCI_DEVICE_ID_LSI_CUTLASS_53:
  4305. instance->instancet = &megasas_instance_template_fusion;
  4306. break;
  4307. case PCI_DEVICE_ID_LSI_SAS1078R:
  4308. case PCI_DEVICE_ID_LSI_SAS1078DE:
  4309. instance->instancet = &megasas_instance_template_ppc;
  4310. break;
  4311. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  4312. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  4313. instance->instancet = &megasas_instance_template_gen2;
  4314. break;
  4315. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  4316. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  4317. instance->instancet = &megasas_instance_template_skinny;
  4318. break;
  4319. case PCI_DEVICE_ID_LSI_SAS1064R:
  4320. case PCI_DEVICE_ID_DELL_PERC5:
  4321. default:
  4322. instance->instancet = &megasas_instance_template_xscale;
  4323. break;
  4324. }
  4325. if (megasas_transition_to_ready(instance, 0)) {
  4326. atomic_set(&instance->fw_reset_no_pci_access, 1);
  4327. instance->instancet->adp_reset
  4328. (instance, instance->reg_set);
  4329. atomic_set(&instance->fw_reset_no_pci_access, 0);
  4330. dev_info(&instance->pdev->dev,
  4331. "FW restarted successfully from %s!\n",
  4332. __func__);
  4333. /*waitting for about 30 second before retry*/
  4334. ssleep(30);
  4335. if (megasas_transition_to_ready(instance, 0))
  4336. goto fail_ready_state;
  4337. }
  4338. /*
  4339. * MSI-X host index 0 is common for all adapter.
  4340. * It is used for all MPT based Adapters.
  4341. */
  4342. instance->reply_post_host_index_addr[0] =
  4343. (u32 __iomem *)((u8 __iomem *)instance->reg_set +
  4344. MPI2_REPLY_POST_HOST_INDEX_OFFSET);
  4345. /* Check if MSI-X is supported while in ready state */
  4346. msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
  4347. 0x4000000) >> 0x1a;
  4348. if (msix_enable && !msix_disable) {
  4349. scratch_pad_2 = readl
  4350. (&instance->reg_set->outbound_scratch_pad_2);
  4351. /* Check max MSI-X vectors */
  4352. if (fusion) {
  4353. if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
  4354. instance->msix_vectors = (scratch_pad_2
  4355. & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
  4356. fw_msix_count = instance->msix_vectors;
  4357. } else { /* Invader series supports more than 8 MSI-x vectors*/
  4358. instance->msix_vectors = ((scratch_pad_2
  4359. & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
  4360. >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
  4361. if (rdpq_enable)
  4362. instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
  4363. 1 : 0;
  4364. fw_msix_count = instance->msix_vectors;
  4365. /* Save 1-15 reply post index address to local memory
  4366. * Index 0 is already saved from reg offset
  4367. * MPI2_REPLY_POST_HOST_INDEX_OFFSET
  4368. */
  4369. for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
  4370. instance->reply_post_host_index_addr[loop] =
  4371. (u32 __iomem *)
  4372. ((u8 __iomem *)instance->reg_set +
  4373. MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
  4374. + (loop * 0x10));
  4375. }
  4376. }
  4377. if (msix_vectors)
  4378. instance->msix_vectors = min(msix_vectors,
  4379. instance->msix_vectors);
  4380. } else /* MFI adapters */
  4381. instance->msix_vectors = 1;
  4382. /* Don't bother allocating more MSI-X vectors than cpus */
  4383. instance->msix_vectors = min(instance->msix_vectors,
  4384. (unsigned int)num_online_cpus());
  4385. for (i = 0; i < instance->msix_vectors; i++)
  4386. instance->msixentry[i].entry = i;
  4387. i = pci_enable_msix_range(instance->pdev, instance->msixentry,
  4388. 1, instance->msix_vectors);
  4389. if (i > 0)
  4390. instance->msix_vectors = i;
  4391. else
  4392. instance->msix_vectors = 0;
  4393. }
  4394. dev_info(&instance->pdev->dev,
  4395. "firmware supports msix\t: (%d)", fw_msix_count);
  4396. dev_info(&instance->pdev->dev,
  4397. "current msix/online cpus\t: (%d/%d)\n",
  4398. instance->msix_vectors, (unsigned int)num_online_cpus());
  4399. dev_info(&instance->pdev->dev,
  4400. "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
  4401. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  4402. (unsigned long)instance);
  4403. if (instance->msix_vectors ?
  4404. megasas_setup_irqs_msix(instance, 1) :
  4405. megasas_setup_irqs_ioapic(instance))
  4406. goto fail_setup_irqs;
  4407. instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
  4408. GFP_KERNEL);
  4409. if (instance->ctrl_info == NULL)
  4410. goto fail_init_adapter;
  4411. /*
  4412. * Below are default value for legacy Firmware.
  4413. * non-fusion based controllers
  4414. */
  4415. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
  4416. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  4417. /* Get operational params, sge flags, send init cmd to controller */
  4418. if (instance->instancet->init_adapter(instance))
  4419. goto fail_init_adapter;
  4420. instance->instancet->enable_intr(instance);
  4421. dev_info(&instance->pdev->dev, "INIT adapter done\n");
  4422. megasas_setup_jbod_map(instance);
  4423. /** for passthrough
  4424. * the following function will get the PD LIST.
  4425. */
  4426. memset(instance->pd_list, 0,
  4427. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  4428. if (megasas_get_pd_list(instance) < 0) {
  4429. dev_err(&instance->pdev->dev, "failed to get PD list\n");
  4430. goto fail_get_pd_list;
  4431. }
  4432. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  4433. if (megasas_ld_list_query(instance,
  4434. MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
  4435. megasas_get_ld_list(instance);
  4436. /*
  4437. * Compute the max allowed sectors per IO: The controller info has two
  4438. * limits on max sectors. Driver should use the minimum of these two.
  4439. *
  4440. * 1 << stripe_sz_ops.min = max sectors per strip
  4441. *
  4442. * Note that older firmwares ( < FW ver 30) didn't report information
  4443. * to calculate max_sectors_1. So the number ended up as zero always.
  4444. */
  4445. tmp_sectors = 0;
  4446. ctrl_info = instance->ctrl_info;
  4447. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  4448. le16_to_cpu(ctrl_info->max_strips_per_io);
  4449. max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
  4450. tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
  4451. instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
  4452. instance->passive = ctrl_info->cluster.passive;
  4453. memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
  4454. instance->UnevenSpanSupport =
  4455. ctrl_info->adapterOperations2.supportUnevenSpans;
  4456. if (instance->UnevenSpanSupport) {
  4457. struct fusion_context *fusion = instance->ctrl_context;
  4458. if (MR_ValidateMapInfo(instance))
  4459. fusion->fast_path_io = 1;
  4460. else
  4461. fusion->fast_path_io = 0;
  4462. }
  4463. if (ctrl_info->host_interface.SRIOV) {
  4464. instance->requestorId = ctrl_info->iov.requestorId;
  4465. if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
  4466. if (!ctrl_info->adapterOperations2.activePassive)
  4467. instance->PlasmaFW111 = 1;
  4468. dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
  4469. instance->PlasmaFW111 ? "1.11" : "new");
  4470. if (instance->PlasmaFW111) {
  4471. iovPtr = (struct IOV_111 *)
  4472. ((unsigned char *)ctrl_info + IOV_111_OFFSET);
  4473. instance->requestorId = iovPtr->requestorId;
  4474. }
  4475. }
  4476. dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
  4477. instance->requestorId);
  4478. }
  4479. instance->crash_dump_fw_support =
  4480. ctrl_info->adapterOperations3.supportCrashDump;
  4481. instance->crash_dump_drv_support =
  4482. (instance->crash_dump_fw_support &&
  4483. instance->crash_dump_buf);
  4484. if (instance->crash_dump_drv_support)
  4485. megasas_set_crash_dump_params(instance,
  4486. MR_CRASH_BUF_TURN_OFF);
  4487. else {
  4488. if (instance->crash_dump_buf)
  4489. pci_free_consistent(instance->pdev,
  4490. CRASH_DMA_BUF_SIZE,
  4491. instance->crash_dump_buf,
  4492. instance->crash_dump_h);
  4493. instance->crash_dump_buf = NULL;
  4494. }
  4495. dev_info(&instance->pdev->dev,
  4496. "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
  4497. le16_to_cpu(ctrl_info->pci.vendor_id),
  4498. le16_to_cpu(ctrl_info->pci.device_id),
  4499. le16_to_cpu(ctrl_info->pci.sub_vendor_id),
  4500. le16_to_cpu(ctrl_info->pci.sub_device_id));
  4501. dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
  4502. instance->UnevenSpanSupport ? "yes" : "no");
  4503. dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
  4504. instance->crash_dump_drv_support ? "yes" : "no");
  4505. dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
  4506. instance->use_seqnum_jbod_fp ? "yes" : "no");
  4507. instance->max_sectors_per_req = instance->max_num_sge *
  4508. SGE_BUFFER_SIZE / 512;
  4509. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  4510. instance->max_sectors_per_req = tmp_sectors;
  4511. /* Check for valid throttlequeuedepth module parameter */
  4512. if (throttlequeuedepth &&
  4513. throttlequeuedepth <= instance->max_scsi_cmds)
  4514. instance->throttlequeuedepth = throttlequeuedepth;
  4515. else
  4516. instance->throttlequeuedepth =
  4517. MEGASAS_THROTTLE_QUEUE_DEPTH;
  4518. if (resetwaittime > MEGASAS_RESET_WAIT_TIME)
  4519. resetwaittime = MEGASAS_RESET_WAIT_TIME;
  4520. if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
  4521. scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
  4522. /* Launch SR-IOV heartbeat timer */
  4523. if (instance->requestorId) {
  4524. if (!megasas_sriov_start_heartbeat(instance, 1))
  4525. megasas_start_timer(instance,
  4526. &instance->sriov_heartbeat_timer,
  4527. megasas_sriov_heartbeat_handler,
  4528. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  4529. else
  4530. instance->skip_heartbeat_timer_del = 1;
  4531. }
  4532. return 0;
  4533. fail_get_pd_list:
  4534. instance->instancet->disable_intr(instance);
  4535. fail_init_adapter:
  4536. megasas_destroy_irqs(instance);
  4537. fail_setup_irqs:
  4538. if (instance->msix_vectors)
  4539. pci_disable_msix(instance->pdev);
  4540. instance->msix_vectors = 0;
  4541. fail_ready_state:
  4542. kfree(instance->ctrl_info);
  4543. instance->ctrl_info = NULL;
  4544. iounmap(instance->reg_set);
  4545. fail_ioremap:
  4546. pci_release_selected_regions(instance->pdev, 1<<instance->bar);
  4547. return -EINVAL;
  4548. }
  4549. /**
  4550. * megasas_release_mfi - Reverses the FW initialization
  4551. * @instance: Adapter soft state
  4552. */
  4553. static void megasas_release_mfi(struct megasas_instance *instance)
  4554. {
  4555. u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
  4556. if (instance->reply_queue)
  4557. pci_free_consistent(instance->pdev, reply_q_sz,
  4558. instance->reply_queue, instance->reply_queue_h);
  4559. megasas_free_cmds(instance);
  4560. iounmap(instance->reg_set);
  4561. pci_release_selected_regions(instance->pdev, 1<<instance->bar);
  4562. }
  4563. /**
  4564. * megasas_get_seq_num - Gets latest event sequence numbers
  4565. * @instance: Adapter soft state
  4566. * @eli: FW event log sequence numbers information
  4567. *
  4568. * FW maintains a log of all events in a non-volatile area. Upper layers would
  4569. * usually find out the latest sequence number of the events, the seq number at
  4570. * the boot etc. They would "read" all the events below the latest seq number
  4571. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  4572. * number), they would subsribe to AEN (asynchronous event notification) and
  4573. * wait for the events to happen.
  4574. */
  4575. static int
  4576. megasas_get_seq_num(struct megasas_instance *instance,
  4577. struct megasas_evt_log_info *eli)
  4578. {
  4579. struct megasas_cmd *cmd;
  4580. struct megasas_dcmd_frame *dcmd;
  4581. struct megasas_evt_log_info *el_info;
  4582. dma_addr_t el_info_h = 0;
  4583. cmd = megasas_get_cmd(instance);
  4584. if (!cmd) {
  4585. return -ENOMEM;
  4586. }
  4587. dcmd = &cmd->frame->dcmd;
  4588. el_info = pci_alloc_consistent(instance->pdev,
  4589. sizeof(struct megasas_evt_log_info),
  4590. &el_info_h);
  4591. if (!el_info) {
  4592. megasas_return_cmd(instance, cmd);
  4593. return -ENOMEM;
  4594. }
  4595. memset(el_info, 0, sizeof(*el_info));
  4596. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  4597. dcmd->cmd = MFI_CMD_DCMD;
  4598. dcmd->cmd_status = 0x0;
  4599. dcmd->sge_count = 1;
  4600. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  4601. dcmd->timeout = 0;
  4602. dcmd->pad_0 = 0;
  4603. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
  4604. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
  4605. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
  4606. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
  4607. if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
  4608. DCMD_SUCCESS) {
  4609. /*
  4610. * Copy the data back into callers buffer
  4611. */
  4612. eli->newest_seq_num = el_info->newest_seq_num;
  4613. eli->oldest_seq_num = el_info->oldest_seq_num;
  4614. eli->clear_seq_num = el_info->clear_seq_num;
  4615. eli->shutdown_seq_num = el_info->shutdown_seq_num;
  4616. eli->boot_seq_num = el_info->boot_seq_num;
  4617. } else
  4618. dev_err(&instance->pdev->dev, "DCMD failed "
  4619. "from %s\n", __func__);
  4620. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  4621. el_info, el_info_h);
  4622. megasas_return_cmd(instance, cmd);
  4623. return 0;
  4624. }
  4625. /**
  4626. * megasas_register_aen - Registers for asynchronous event notification
  4627. * @instance: Adapter soft state
  4628. * @seq_num: The starting sequence number
  4629. * @class_locale: Class of the event
  4630. *
  4631. * This function subscribes for AEN for events beyond the @seq_num. It requests
  4632. * to be notified if and only if the event is of type @class_locale
  4633. */
  4634. static int
  4635. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  4636. u32 class_locale_word)
  4637. {
  4638. int ret_val;
  4639. struct megasas_cmd *cmd;
  4640. struct megasas_dcmd_frame *dcmd;
  4641. union megasas_evt_class_locale curr_aen;
  4642. union megasas_evt_class_locale prev_aen;
  4643. /*
  4644. * If there an AEN pending already (aen_cmd), check if the
  4645. * class_locale of that pending AEN is inclusive of the new
  4646. * AEN request we currently have. If it is, then we don't have
  4647. * to do anything. In other words, whichever events the current
  4648. * AEN request is subscribing to, have already been subscribed
  4649. * to.
  4650. *
  4651. * If the old_cmd is _not_ inclusive, then we have to abort
  4652. * that command, form a class_locale that is superset of both
  4653. * old and current and re-issue to the FW
  4654. */
  4655. curr_aen.word = class_locale_word;
  4656. if (instance->aen_cmd) {
  4657. prev_aen.word =
  4658. le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
  4659. /*
  4660. * A class whose enum value is smaller is inclusive of all
  4661. * higher values. If a PROGRESS (= -1) was previously
  4662. * registered, then a new registration requests for higher
  4663. * classes need not be sent to FW. They are automatically
  4664. * included.
  4665. *
  4666. * Locale numbers don't have such hierarchy. They are bitmap
  4667. * values
  4668. */
  4669. if ((prev_aen.members.class <= curr_aen.members.class) &&
  4670. !((prev_aen.members.locale & curr_aen.members.locale) ^
  4671. curr_aen.members.locale)) {
  4672. /*
  4673. * Previously issued event registration includes
  4674. * current request. Nothing to do.
  4675. */
  4676. return 0;
  4677. } else {
  4678. curr_aen.members.locale |= prev_aen.members.locale;
  4679. if (prev_aen.members.class < curr_aen.members.class)
  4680. curr_aen.members.class = prev_aen.members.class;
  4681. instance->aen_cmd->abort_aen = 1;
  4682. ret_val = megasas_issue_blocked_abort_cmd(instance,
  4683. instance->
  4684. aen_cmd, 30);
  4685. if (ret_val) {
  4686. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
  4687. "previous AEN command\n");
  4688. return ret_val;
  4689. }
  4690. }
  4691. }
  4692. cmd = megasas_get_cmd(instance);
  4693. if (!cmd)
  4694. return -ENOMEM;
  4695. dcmd = &cmd->frame->dcmd;
  4696. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  4697. /*
  4698. * Prepare DCMD for aen registration
  4699. */
  4700. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  4701. dcmd->cmd = MFI_CMD_DCMD;
  4702. dcmd->cmd_status = 0x0;
  4703. dcmd->sge_count = 1;
  4704. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  4705. dcmd->timeout = 0;
  4706. dcmd->pad_0 = 0;
  4707. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
  4708. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
  4709. dcmd->mbox.w[0] = cpu_to_le32(seq_num);
  4710. instance->last_seq_num = seq_num;
  4711. dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
  4712. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
  4713. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
  4714. if (instance->aen_cmd != NULL) {
  4715. megasas_return_cmd(instance, cmd);
  4716. return 0;
  4717. }
  4718. /*
  4719. * Store reference to the cmd used to register for AEN. When an
  4720. * application wants us to register for AEN, we have to abort this
  4721. * cmd and re-register with a new EVENT LOCALE supplied by that app
  4722. */
  4723. instance->aen_cmd = cmd;
  4724. /*
  4725. * Issue the aen registration frame
  4726. */
  4727. instance->instancet->issue_dcmd(instance, cmd);
  4728. return 0;
  4729. }
  4730. /**
  4731. * megasas_start_aen - Subscribes to AEN during driver load time
  4732. * @instance: Adapter soft state
  4733. */
  4734. static int megasas_start_aen(struct megasas_instance *instance)
  4735. {
  4736. struct megasas_evt_log_info eli;
  4737. union megasas_evt_class_locale class_locale;
  4738. /*
  4739. * Get the latest sequence number from FW
  4740. */
  4741. memset(&eli, 0, sizeof(eli));
  4742. if (megasas_get_seq_num(instance, &eli))
  4743. return -1;
  4744. /*
  4745. * Register AEN with FW for latest sequence number plus 1
  4746. */
  4747. class_locale.members.reserved = 0;
  4748. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  4749. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  4750. return megasas_register_aen(instance,
  4751. le32_to_cpu(eli.newest_seq_num) + 1,
  4752. class_locale.word);
  4753. }
  4754. /**
  4755. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  4756. * @instance: Adapter soft state
  4757. */
  4758. static int megasas_io_attach(struct megasas_instance *instance)
  4759. {
  4760. struct Scsi_Host *host = instance->host;
  4761. /*
  4762. * Export parameters required by SCSI mid-layer
  4763. */
  4764. host->irq = instance->pdev->irq;
  4765. host->unique_id = instance->unique_id;
  4766. host->can_queue = instance->max_scsi_cmds;
  4767. host->this_id = instance->init_id;
  4768. host->sg_tablesize = instance->max_num_sge;
  4769. if (instance->fw_support_ieee)
  4770. instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
  4771. /*
  4772. * Check if the module parameter value for max_sectors can be used
  4773. */
  4774. if (max_sectors && max_sectors < instance->max_sectors_per_req)
  4775. instance->max_sectors_per_req = max_sectors;
  4776. else {
  4777. if (max_sectors) {
  4778. if (((instance->pdev->device ==
  4779. PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  4780. (instance->pdev->device ==
  4781. PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
  4782. (max_sectors <= MEGASAS_MAX_SECTORS)) {
  4783. instance->max_sectors_per_req = max_sectors;
  4784. } else {
  4785. dev_info(&instance->pdev->dev, "max_sectors should be > 0"
  4786. "and <= %d (or < 1MB for GEN2 controller)\n",
  4787. instance->max_sectors_per_req);
  4788. }
  4789. }
  4790. }
  4791. host->max_sectors = instance->max_sectors_per_req;
  4792. host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
  4793. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  4794. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  4795. host->max_lun = MEGASAS_MAX_LUN;
  4796. host->max_cmd_len = 16;
  4797. /*
  4798. * Notify the mid-layer about the new controller
  4799. */
  4800. if (scsi_add_host(host, &instance->pdev->dev)) {
  4801. dev_err(&instance->pdev->dev,
  4802. "Failed to add host from %s %d\n",
  4803. __func__, __LINE__);
  4804. return -ENODEV;
  4805. }
  4806. return 0;
  4807. }
  4808. static int
  4809. megasas_set_dma_mask(struct pci_dev *pdev)
  4810. {
  4811. /*
  4812. * All our controllers are capable of performing 64-bit DMA
  4813. */
  4814. if (IS_DMA64) {
  4815. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  4816. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  4817. goto fail_set_dma_mask;
  4818. }
  4819. } else {
  4820. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  4821. goto fail_set_dma_mask;
  4822. }
  4823. /*
  4824. * Ensure that all data structures are allocated in 32-bit
  4825. * memory.
  4826. */
  4827. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
  4828. /* Try 32bit DMA mask and 32 bit Consistent dma mask */
  4829. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  4830. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  4831. dev_info(&pdev->dev, "set 32bit DMA mask"
  4832. "and 32 bit consistent mask\n");
  4833. else
  4834. goto fail_set_dma_mask;
  4835. }
  4836. return 0;
  4837. fail_set_dma_mask:
  4838. return 1;
  4839. }
  4840. /**
  4841. * megasas_probe_one - PCI hotplug entry point
  4842. * @pdev: PCI device structure
  4843. * @id: PCI ids of supported hotplugged adapter
  4844. */
  4845. static int megasas_probe_one(struct pci_dev *pdev,
  4846. const struct pci_device_id *id)
  4847. {
  4848. int rval, pos;
  4849. struct Scsi_Host *host;
  4850. struct megasas_instance *instance;
  4851. u16 control = 0;
  4852. struct fusion_context *fusion = NULL;
  4853. /* Reset MSI-X in the kdump kernel */
  4854. if (reset_devices) {
  4855. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  4856. if (pos) {
  4857. pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
  4858. &control);
  4859. if (control & PCI_MSIX_FLAGS_ENABLE) {
  4860. dev_info(&pdev->dev, "resetting MSI-X\n");
  4861. pci_write_config_word(pdev,
  4862. pos + PCI_MSIX_FLAGS,
  4863. control &
  4864. ~PCI_MSIX_FLAGS_ENABLE);
  4865. }
  4866. }
  4867. }
  4868. /*
  4869. * PCI prepping: enable device set bus mastering and dma mask
  4870. */
  4871. rval = pci_enable_device_mem(pdev);
  4872. if (rval) {
  4873. return rval;
  4874. }
  4875. pci_set_master(pdev);
  4876. if (megasas_set_dma_mask(pdev))
  4877. goto fail_set_dma_mask;
  4878. host = scsi_host_alloc(&megasas_template,
  4879. sizeof(struct megasas_instance));
  4880. if (!host) {
  4881. dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
  4882. goto fail_alloc_instance;
  4883. }
  4884. instance = (struct megasas_instance *)host->hostdata;
  4885. memset(instance, 0, sizeof(*instance));
  4886. atomic_set(&instance->fw_reset_no_pci_access, 0);
  4887. instance->pdev = pdev;
  4888. switch (instance->pdev->device) {
  4889. case PCI_DEVICE_ID_LSI_FUSION:
  4890. case PCI_DEVICE_ID_LSI_PLASMA:
  4891. case PCI_DEVICE_ID_LSI_INVADER:
  4892. case PCI_DEVICE_ID_LSI_FURY:
  4893. case PCI_DEVICE_ID_LSI_INTRUDER:
  4894. case PCI_DEVICE_ID_LSI_INTRUDER_24:
  4895. case PCI_DEVICE_ID_LSI_CUTLASS_52:
  4896. case PCI_DEVICE_ID_LSI_CUTLASS_53:
  4897. {
  4898. instance->ctrl_context_pages =
  4899. get_order(sizeof(struct fusion_context));
  4900. instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
  4901. instance->ctrl_context_pages);
  4902. if (!instance->ctrl_context) {
  4903. dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
  4904. "memory for Fusion context info\n");
  4905. goto fail_alloc_dma_buf;
  4906. }
  4907. fusion = instance->ctrl_context;
  4908. memset(fusion, 0,
  4909. ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
  4910. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  4911. (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
  4912. fusion->adapter_type = THUNDERBOLT_SERIES;
  4913. else
  4914. fusion->adapter_type = INVADER_SERIES;
  4915. }
  4916. break;
  4917. default: /* For all other supported controllers */
  4918. instance->producer =
  4919. pci_alloc_consistent(pdev, sizeof(u32),
  4920. &instance->producer_h);
  4921. instance->consumer =
  4922. pci_alloc_consistent(pdev, sizeof(u32),
  4923. &instance->consumer_h);
  4924. if (!instance->producer || !instance->consumer) {
  4925. dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
  4926. "memory for producer, consumer\n");
  4927. goto fail_alloc_dma_buf;
  4928. }
  4929. *instance->producer = 0;
  4930. *instance->consumer = 0;
  4931. break;
  4932. }
  4933. /* Crash dump feature related initialisation*/
  4934. instance->drv_buf_index = 0;
  4935. instance->drv_buf_alloc = 0;
  4936. instance->crash_dump_fw_support = 0;
  4937. instance->crash_dump_app_support = 0;
  4938. instance->fw_crash_state = UNAVAILABLE;
  4939. spin_lock_init(&instance->crashdump_lock);
  4940. instance->crash_dump_buf = NULL;
  4941. megasas_poll_wait_aen = 0;
  4942. instance->flag_ieee = 0;
  4943. instance->ev = NULL;
  4944. instance->issuepend_done = 1;
  4945. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  4946. instance->is_imr = 0;
  4947. instance->evt_detail = pci_alloc_consistent(pdev,
  4948. sizeof(struct
  4949. megasas_evt_detail),
  4950. &instance->evt_detail_h);
  4951. if (!instance->evt_detail) {
  4952. dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
  4953. "event detail structure\n");
  4954. goto fail_alloc_dma_buf;
  4955. }
  4956. if (!reset_devices) {
  4957. instance->system_info_buf = pci_zalloc_consistent(pdev,
  4958. sizeof(struct MR_DRV_SYSTEM_INFO),
  4959. &instance->system_info_h);
  4960. if (!instance->system_info_buf)
  4961. dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
  4962. instance->pd_info = pci_alloc_consistent(pdev,
  4963. sizeof(struct MR_PD_INFO), &instance->pd_info_h);
  4964. if (!instance->pd_info)
  4965. dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
  4966. instance->crash_dump_buf = pci_alloc_consistent(pdev,
  4967. CRASH_DMA_BUF_SIZE,
  4968. &instance->crash_dump_h);
  4969. if (!instance->crash_dump_buf)
  4970. dev_err(&pdev->dev, "Can't allocate Firmware "
  4971. "crash dump DMA buffer\n");
  4972. }
  4973. /*
  4974. * Initialize locks and queues
  4975. */
  4976. INIT_LIST_HEAD(&instance->cmd_pool);
  4977. INIT_LIST_HEAD(&instance->internal_reset_pending_q);
  4978. atomic_set(&instance->fw_outstanding,0);
  4979. init_waitqueue_head(&instance->int_cmd_wait_q);
  4980. init_waitqueue_head(&instance->abort_cmd_wait_q);
  4981. spin_lock_init(&instance->mfi_pool_lock);
  4982. spin_lock_init(&instance->hba_lock);
  4983. spin_lock_init(&instance->completion_lock);
  4984. mutex_init(&instance->reset_mutex);
  4985. mutex_init(&instance->hba_mutex);
  4986. /*
  4987. * Initialize PCI related and misc parameters
  4988. */
  4989. instance->host = host;
  4990. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  4991. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  4992. instance->ctrl_info = NULL;
  4993. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  4994. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
  4995. instance->flag_ieee = 1;
  4996. megasas_dbg_lvl = 0;
  4997. instance->flag = 0;
  4998. instance->unload = 1;
  4999. instance->last_time = 0;
  5000. instance->disableOnlineCtrlReset = 1;
  5001. instance->UnevenSpanSupport = 0;
  5002. if (instance->ctrl_context) {
  5003. INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
  5004. INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
  5005. } else
  5006. INIT_WORK(&instance->work_init, process_fw_state_change_wq);
  5007. /*
  5008. * Initialize MFI Firmware
  5009. */
  5010. if (megasas_init_fw(instance))
  5011. goto fail_init_mfi;
  5012. if (instance->requestorId) {
  5013. if (instance->PlasmaFW111) {
  5014. instance->vf_affiliation_111 =
  5015. pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
  5016. &instance->vf_affiliation_111_h);
  5017. if (!instance->vf_affiliation_111)
  5018. dev_warn(&pdev->dev, "Can't allocate "
  5019. "memory for VF affiliation buffer\n");
  5020. } else {
  5021. instance->vf_affiliation =
  5022. pci_alloc_consistent(pdev,
  5023. (MAX_LOGICAL_DRIVES + 1) *
  5024. sizeof(struct MR_LD_VF_AFFILIATION),
  5025. &instance->vf_affiliation_h);
  5026. if (!instance->vf_affiliation)
  5027. dev_warn(&pdev->dev, "Can't allocate "
  5028. "memory for VF affiliation buffer\n");
  5029. }
  5030. }
  5031. /*
  5032. * Store instance in PCI softstate
  5033. */
  5034. pci_set_drvdata(pdev, instance);
  5035. /*
  5036. * Add this controller to megasas_mgmt_info structure so that it
  5037. * can be exported to management applications
  5038. */
  5039. megasas_mgmt_info.count++;
  5040. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  5041. megasas_mgmt_info.max_index++;
  5042. /*
  5043. * Register with SCSI mid-layer
  5044. */
  5045. if (megasas_io_attach(instance))
  5046. goto fail_io_attach;
  5047. instance->unload = 0;
  5048. /*
  5049. * Trigger SCSI to scan our drives
  5050. */
  5051. scsi_scan_host(host);
  5052. /*
  5053. * Initiate AEN (Asynchronous Event Notification)
  5054. */
  5055. if (megasas_start_aen(instance)) {
  5056. dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
  5057. goto fail_start_aen;
  5058. }
  5059. /* Get current SR-IOV LD/VF affiliation */
  5060. if (instance->requestorId)
  5061. megasas_get_ld_vf_affiliation(instance, 1);
  5062. return 0;
  5063. fail_start_aen:
  5064. fail_io_attach:
  5065. megasas_mgmt_info.count--;
  5066. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  5067. megasas_mgmt_info.max_index--;
  5068. instance->instancet->disable_intr(instance);
  5069. megasas_destroy_irqs(instance);
  5070. if (instance->ctrl_context)
  5071. megasas_release_fusion(instance);
  5072. else
  5073. megasas_release_mfi(instance);
  5074. if (instance->msix_vectors)
  5075. pci_disable_msix(instance->pdev);
  5076. fail_init_mfi:
  5077. fail_alloc_dma_buf:
  5078. if (instance->evt_detail)
  5079. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  5080. instance->evt_detail,
  5081. instance->evt_detail_h);
  5082. if (instance->pd_info)
  5083. pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
  5084. instance->pd_info,
  5085. instance->pd_info_h);
  5086. if (instance->producer)
  5087. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  5088. instance->producer_h);
  5089. if (instance->consumer)
  5090. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  5091. instance->consumer_h);
  5092. scsi_host_put(host);
  5093. fail_alloc_instance:
  5094. fail_set_dma_mask:
  5095. pci_disable_device(pdev);
  5096. return -ENODEV;
  5097. }
  5098. /**
  5099. * megasas_flush_cache - Requests FW to flush all its caches
  5100. * @instance: Adapter soft state
  5101. */
  5102. static void megasas_flush_cache(struct megasas_instance *instance)
  5103. {
  5104. struct megasas_cmd *cmd;
  5105. struct megasas_dcmd_frame *dcmd;
  5106. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  5107. return;
  5108. cmd = megasas_get_cmd(instance);
  5109. if (!cmd)
  5110. return;
  5111. dcmd = &cmd->frame->dcmd;
  5112. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  5113. dcmd->cmd = MFI_CMD_DCMD;
  5114. dcmd->cmd_status = 0x0;
  5115. dcmd->sge_count = 0;
  5116. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  5117. dcmd->timeout = 0;
  5118. dcmd->pad_0 = 0;
  5119. dcmd->data_xfer_len = 0;
  5120. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
  5121. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  5122. if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
  5123. != DCMD_SUCCESS) {
  5124. dev_err(&instance->pdev->dev,
  5125. "return from %s %d\n", __func__, __LINE__);
  5126. return;
  5127. }
  5128. megasas_return_cmd(instance, cmd);
  5129. }
  5130. /**
  5131. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  5132. * @instance: Adapter soft state
  5133. * @opcode: Shutdown/Hibernate
  5134. */
  5135. static void megasas_shutdown_controller(struct megasas_instance *instance,
  5136. u32 opcode)
  5137. {
  5138. struct megasas_cmd *cmd;
  5139. struct megasas_dcmd_frame *dcmd;
  5140. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  5141. return;
  5142. cmd = megasas_get_cmd(instance);
  5143. if (!cmd)
  5144. return;
  5145. if (instance->aen_cmd)
  5146. megasas_issue_blocked_abort_cmd(instance,
  5147. instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
  5148. if (instance->map_update_cmd)
  5149. megasas_issue_blocked_abort_cmd(instance,
  5150. instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
  5151. if (instance->jbod_seq_cmd)
  5152. megasas_issue_blocked_abort_cmd(instance,
  5153. instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
  5154. dcmd = &cmd->frame->dcmd;
  5155. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  5156. dcmd->cmd = MFI_CMD_DCMD;
  5157. dcmd->cmd_status = 0x0;
  5158. dcmd->sge_count = 0;
  5159. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  5160. dcmd->timeout = 0;
  5161. dcmd->pad_0 = 0;
  5162. dcmd->data_xfer_len = 0;
  5163. dcmd->opcode = cpu_to_le32(opcode);
  5164. if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
  5165. != DCMD_SUCCESS) {
  5166. dev_err(&instance->pdev->dev,
  5167. "return from %s %d\n", __func__, __LINE__);
  5168. return;
  5169. }
  5170. megasas_return_cmd(instance, cmd);
  5171. }
  5172. #ifdef CONFIG_PM
  5173. /**
  5174. * megasas_suspend - driver suspend entry point
  5175. * @pdev: PCI device structure
  5176. * @state: PCI power state to suspend routine
  5177. */
  5178. static int
  5179. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  5180. {
  5181. struct Scsi_Host *host;
  5182. struct megasas_instance *instance;
  5183. instance = pci_get_drvdata(pdev);
  5184. host = instance->host;
  5185. instance->unload = 1;
  5186. /* Shutdown SR-IOV heartbeat timer */
  5187. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  5188. del_timer_sync(&instance->sriov_heartbeat_timer);
  5189. megasas_flush_cache(instance);
  5190. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  5191. /* cancel the delayed work if this work still in queue */
  5192. if (instance->ev != NULL) {
  5193. struct megasas_aen_event *ev = instance->ev;
  5194. cancel_delayed_work_sync(&ev->hotplug_work);
  5195. instance->ev = NULL;
  5196. }
  5197. tasklet_kill(&instance->isr_tasklet);
  5198. pci_set_drvdata(instance->pdev, instance);
  5199. instance->instancet->disable_intr(instance);
  5200. megasas_destroy_irqs(instance);
  5201. if (instance->msix_vectors)
  5202. pci_disable_msix(instance->pdev);
  5203. pci_save_state(pdev);
  5204. pci_disable_device(pdev);
  5205. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  5206. return 0;
  5207. }
  5208. /**
  5209. * megasas_resume- driver resume entry point
  5210. * @pdev: PCI device structure
  5211. */
  5212. static int
  5213. megasas_resume(struct pci_dev *pdev)
  5214. {
  5215. int rval;
  5216. struct Scsi_Host *host;
  5217. struct megasas_instance *instance;
  5218. instance = pci_get_drvdata(pdev);
  5219. host = instance->host;
  5220. pci_set_power_state(pdev, PCI_D0);
  5221. pci_enable_wake(pdev, PCI_D0, 0);
  5222. pci_restore_state(pdev);
  5223. /*
  5224. * PCI prepping: enable device set bus mastering and dma mask
  5225. */
  5226. rval = pci_enable_device_mem(pdev);
  5227. if (rval) {
  5228. dev_err(&pdev->dev, "Enable device failed\n");
  5229. return rval;
  5230. }
  5231. pci_set_master(pdev);
  5232. if (megasas_set_dma_mask(pdev))
  5233. goto fail_set_dma_mask;
  5234. /*
  5235. * Initialize MFI Firmware
  5236. */
  5237. atomic_set(&instance->fw_outstanding, 0);
  5238. /*
  5239. * We expect the FW state to be READY
  5240. */
  5241. if (megasas_transition_to_ready(instance, 0))
  5242. goto fail_ready_state;
  5243. /* Now re-enable MSI-X */
  5244. if (instance->msix_vectors &&
  5245. pci_enable_msix_exact(instance->pdev, instance->msixentry,
  5246. instance->msix_vectors))
  5247. goto fail_reenable_msix;
  5248. if (instance->ctrl_context) {
  5249. megasas_reset_reply_desc(instance);
  5250. if (megasas_ioc_init_fusion(instance)) {
  5251. megasas_free_cmds(instance);
  5252. megasas_free_cmds_fusion(instance);
  5253. goto fail_init_mfi;
  5254. }
  5255. if (!megasas_get_map_info(instance))
  5256. megasas_sync_map_info(instance);
  5257. } else {
  5258. *instance->producer = 0;
  5259. *instance->consumer = 0;
  5260. if (megasas_issue_init_mfi(instance))
  5261. goto fail_init_mfi;
  5262. }
  5263. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  5264. (unsigned long)instance);
  5265. if (instance->msix_vectors ?
  5266. megasas_setup_irqs_msix(instance, 0) :
  5267. megasas_setup_irqs_ioapic(instance))
  5268. goto fail_init_mfi;
  5269. /* Re-launch SR-IOV heartbeat timer */
  5270. if (instance->requestorId) {
  5271. if (!megasas_sriov_start_heartbeat(instance, 0))
  5272. megasas_start_timer(instance,
  5273. &instance->sriov_heartbeat_timer,
  5274. megasas_sriov_heartbeat_handler,
  5275. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  5276. else {
  5277. instance->skip_heartbeat_timer_del = 1;
  5278. goto fail_init_mfi;
  5279. }
  5280. }
  5281. instance->instancet->enable_intr(instance);
  5282. megasas_setup_jbod_map(instance);
  5283. instance->unload = 0;
  5284. /*
  5285. * Initiate AEN (Asynchronous Event Notification)
  5286. */
  5287. if (megasas_start_aen(instance))
  5288. dev_err(&instance->pdev->dev, "Start AEN failed\n");
  5289. return 0;
  5290. fail_init_mfi:
  5291. if (instance->evt_detail)
  5292. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  5293. instance->evt_detail,
  5294. instance->evt_detail_h);
  5295. if (instance->pd_info)
  5296. pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
  5297. instance->pd_info,
  5298. instance->pd_info_h);
  5299. if (instance->producer)
  5300. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  5301. instance->producer_h);
  5302. if (instance->consumer)
  5303. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  5304. instance->consumer_h);
  5305. scsi_host_put(host);
  5306. fail_set_dma_mask:
  5307. fail_ready_state:
  5308. fail_reenable_msix:
  5309. pci_disable_device(pdev);
  5310. return -ENODEV;
  5311. }
  5312. #else
  5313. #define megasas_suspend NULL
  5314. #define megasas_resume NULL
  5315. #endif
  5316. /**
  5317. * megasas_detach_one - PCI hot"un"plug entry point
  5318. * @pdev: PCI device structure
  5319. */
  5320. static void megasas_detach_one(struct pci_dev *pdev)
  5321. {
  5322. int i;
  5323. struct Scsi_Host *host;
  5324. struct megasas_instance *instance;
  5325. struct fusion_context *fusion;
  5326. u32 pd_seq_map_sz;
  5327. instance = pci_get_drvdata(pdev);
  5328. instance->unload = 1;
  5329. host = instance->host;
  5330. fusion = instance->ctrl_context;
  5331. /* Shutdown SR-IOV heartbeat timer */
  5332. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  5333. del_timer_sync(&instance->sriov_heartbeat_timer);
  5334. if (instance->fw_crash_state != UNAVAILABLE)
  5335. megasas_free_host_crash_buffer(instance);
  5336. scsi_remove_host(instance->host);
  5337. megasas_flush_cache(instance);
  5338. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  5339. /* cancel the delayed work if this work still in queue*/
  5340. if (instance->ev != NULL) {
  5341. struct megasas_aen_event *ev = instance->ev;
  5342. cancel_delayed_work_sync(&ev->hotplug_work);
  5343. instance->ev = NULL;
  5344. }
  5345. /* cancel all wait events */
  5346. wake_up_all(&instance->int_cmd_wait_q);
  5347. tasklet_kill(&instance->isr_tasklet);
  5348. /*
  5349. * Take the instance off the instance array. Note that we will not
  5350. * decrement the max_index. We let this array be sparse array
  5351. */
  5352. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  5353. if (megasas_mgmt_info.instance[i] == instance) {
  5354. megasas_mgmt_info.count--;
  5355. megasas_mgmt_info.instance[i] = NULL;
  5356. break;
  5357. }
  5358. }
  5359. instance->instancet->disable_intr(instance);
  5360. megasas_destroy_irqs(instance);
  5361. if (instance->msix_vectors)
  5362. pci_disable_msix(instance->pdev);
  5363. if (instance->ctrl_context) {
  5364. megasas_release_fusion(instance);
  5365. pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
  5366. (sizeof(struct MR_PD_CFG_SEQ) *
  5367. (MAX_PHYSICAL_DEVICES - 1));
  5368. for (i = 0; i < 2 ; i++) {
  5369. if (fusion->ld_map[i])
  5370. dma_free_coherent(&instance->pdev->dev,
  5371. fusion->max_map_sz,
  5372. fusion->ld_map[i],
  5373. fusion->ld_map_phys[i]);
  5374. if (fusion->ld_drv_map[i])
  5375. free_pages((ulong)fusion->ld_drv_map[i],
  5376. fusion->drv_map_pages);
  5377. if (fusion->pd_seq_sync[i])
  5378. dma_free_coherent(&instance->pdev->dev,
  5379. pd_seq_map_sz,
  5380. fusion->pd_seq_sync[i],
  5381. fusion->pd_seq_phys[i]);
  5382. }
  5383. free_pages((ulong)instance->ctrl_context,
  5384. instance->ctrl_context_pages);
  5385. } else {
  5386. megasas_release_mfi(instance);
  5387. pci_free_consistent(pdev, sizeof(u32),
  5388. instance->producer,
  5389. instance->producer_h);
  5390. pci_free_consistent(pdev, sizeof(u32),
  5391. instance->consumer,
  5392. instance->consumer_h);
  5393. }
  5394. kfree(instance->ctrl_info);
  5395. if (instance->evt_detail)
  5396. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  5397. instance->evt_detail, instance->evt_detail_h);
  5398. if (instance->pd_info)
  5399. pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
  5400. instance->pd_info,
  5401. instance->pd_info_h);
  5402. if (instance->vf_affiliation)
  5403. pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
  5404. sizeof(struct MR_LD_VF_AFFILIATION),
  5405. instance->vf_affiliation,
  5406. instance->vf_affiliation_h);
  5407. if (instance->vf_affiliation_111)
  5408. pci_free_consistent(pdev,
  5409. sizeof(struct MR_LD_VF_AFFILIATION_111),
  5410. instance->vf_affiliation_111,
  5411. instance->vf_affiliation_111_h);
  5412. if (instance->hb_host_mem)
  5413. pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
  5414. instance->hb_host_mem,
  5415. instance->hb_host_mem_h);
  5416. if (instance->crash_dump_buf)
  5417. pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
  5418. instance->crash_dump_buf, instance->crash_dump_h);
  5419. if (instance->system_info_buf)
  5420. pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
  5421. instance->system_info_buf, instance->system_info_h);
  5422. scsi_host_put(host);
  5423. pci_disable_device(pdev);
  5424. }
  5425. /**
  5426. * megasas_shutdown - Shutdown entry point
  5427. * @device: Generic device structure
  5428. */
  5429. static void megasas_shutdown(struct pci_dev *pdev)
  5430. {
  5431. struct megasas_instance *instance = pci_get_drvdata(pdev);
  5432. instance->unload = 1;
  5433. megasas_flush_cache(instance);
  5434. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  5435. instance->instancet->disable_intr(instance);
  5436. megasas_destroy_irqs(instance);
  5437. if (instance->msix_vectors)
  5438. pci_disable_msix(instance->pdev);
  5439. }
  5440. /**
  5441. * megasas_mgmt_open - char node "open" entry point
  5442. */
  5443. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  5444. {
  5445. /*
  5446. * Allow only those users with admin rights
  5447. */
  5448. if (!capable(CAP_SYS_ADMIN))
  5449. return -EACCES;
  5450. return 0;
  5451. }
  5452. /**
  5453. * megasas_mgmt_fasync - Async notifier registration from applications
  5454. *
  5455. * This function adds the calling process to a driver global queue. When an
  5456. * event occurs, SIGIO will be sent to all processes in this queue.
  5457. */
  5458. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  5459. {
  5460. int rc;
  5461. mutex_lock(&megasas_async_queue_mutex);
  5462. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  5463. mutex_unlock(&megasas_async_queue_mutex);
  5464. if (rc >= 0) {
  5465. /* For sanity check when we get ioctl */
  5466. filep->private_data = filep;
  5467. return 0;
  5468. }
  5469. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  5470. return rc;
  5471. }
  5472. /**
  5473. * megasas_mgmt_poll - char node "poll" entry point
  5474. * */
  5475. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  5476. {
  5477. unsigned int mask;
  5478. unsigned long flags;
  5479. poll_wait(file, &megasas_poll_wait, wait);
  5480. spin_lock_irqsave(&poll_aen_lock, flags);
  5481. if (megasas_poll_wait_aen)
  5482. mask = (POLLIN | POLLRDNORM);
  5483. else
  5484. mask = 0;
  5485. megasas_poll_wait_aen = 0;
  5486. spin_unlock_irqrestore(&poll_aen_lock, flags);
  5487. return mask;
  5488. }
  5489. /*
  5490. * megasas_set_crash_dump_params_ioctl:
  5491. * Send CRASH_DUMP_MODE DCMD to all controllers
  5492. * @cmd: MFI command frame
  5493. */
  5494. static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
  5495. {
  5496. struct megasas_instance *local_instance;
  5497. int i, error = 0;
  5498. int crash_support;
  5499. crash_support = cmd->frame->dcmd.mbox.w[0];
  5500. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  5501. local_instance = megasas_mgmt_info.instance[i];
  5502. if (local_instance && local_instance->crash_dump_drv_support) {
  5503. if ((atomic_read(&local_instance->adprecovery) ==
  5504. MEGASAS_HBA_OPERATIONAL) &&
  5505. !megasas_set_crash_dump_params(local_instance,
  5506. crash_support)) {
  5507. local_instance->crash_dump_app_support =
  5508. crash_support;
  5509. dev_info(&local_instance->pdev->dev,
  5510. "Application firmware crash "
  5511. "dump mode set success\n");
  5512. error = 0;
  5513. } else {
  5514. dev_info(&local_instance->pdev->dev,
  5515. "Application firmware crash "
  5516. "dump mode set failed\n");
  5517. error = -1;
  5518. }
  5519. }
  5520. }
  5521. return error;
  5522. }
  5523. /**
  5524. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  5525. * @instance: Adapter soft state
  5526. * @argp: User's ioctl packet
  5527. */
  5528. static int
  5529. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  5530. struct megasas_iocpacket __user * user_ioc,
  5531. struct megasas_iocpacket *ioc)
  5532. {
  5533. struct megasas_sge32 *kern_sge32;
  5534. struct megasas_cmd *cmd;
  5535. void *kbuff_arr[MAX_IOCTL_SGE];
  5536. dma_addr_t buf_handle = 0;
  5537. int error = 0, i;
  5538. void *sense = NULL;
  5539. dma_addr_t sense_handle;
  5540. unsigned long *sense_ptr;
  5541. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  5542. if (ioc->sge_count > MAX_IOCTL_SGE) {
  5543. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
  5544. ioc->sge_count, MAX_IOCTL_SGE);
  5545. return -EINVAL;
  5546. }
  5547. cmd = megasas_get_cmd(instance);
  5548. if (!cmd) {
  5549. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
  5550. return -ENOMEM;
  5551. }
  5552. /*
  5553. * User's IOCTL packet has 2 frames (maximum). Copy those two
  5554. * frames into our cmd's frames. cmd->frame's context will get
  5555. * overwritten when we copy from user's frames. So set that value
  5556. * alone separately
  5557. */
  5558. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  5559. cmd->frame->hdr.context = cpu_to_le32(cmd->index);
  5560. cmd->frame->hdr.pad_0 = 0;
  5561. cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
  5562. MFI_FRAME_SGL64 |
  5563. MFI_FRAME_SENSE64));
  5564. if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
  5565. error = megasas_set_crash_dump_params_ioctl(cmd);
  5566. megasas_return_cmd(instance, cmd);
  5567. return error;
  5568. }
  5569. /*
  5570. * The management interface between applications and the fw uses
  5571. * MFI frames. E.g, RAID configuration changes, LD property changes
  5572. * etc are accomplishes through different kinds of MFI frames. The
  5573. * driver needs to care only about substituting user buffers with
  5574. * kernel buffers in SGLs. The location of SGL is embedded in the
  5575. * struct iocpacket itself.
  5576. */
  5577. kern_sge32 = (struct megasas_sge32 *)
  5578. ((unsigned long)cmd->frame + ioc->sgl_off);
  5579. /*
  5580. * For each user buffer, create a mirror buffer and copy in
  5581. */
  5582. for (i = 0; i < ioc->sge_count; i++) {
  5583. if (!ioc->sgl[i].iov_len)
  5584. continue;
  5585. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  5586. ioc->sgl[i].iov_len,
  5587. &buf_handle, GFP_KERNEL);
  5588. if (!kbuff_arr[i]) {
  5589. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
  5590. "kernel SGL buffer for IOCTL\n");
  5591. error = -ENOMEM;
  5592. goto out;
  5593. }
  5594. /*
  5595. * We don't change the dma_coherent_mask, so
  5596. * pci_alloc_consistent only returns 32bit addresses
  5597. */
  5598. kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
  5599. kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
  5600. /*
  5601. * We created a kernel buffer corresponding to the
  5602. * user buffer. Now copy in from the user buffer
  5603. */
  5604. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  5605. (u32) (ioc->sgl[i].iov_len))) {
  5606. error = -EFAULT;
  5607. goto out;
  5608. }
  5609. }
  5610. if (ioc->sense_len) {
  5611. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  5612. &sense_handle, GFP_KERNEL);
  5613. if (!sense) {
  5614. error = -ENOMEM;
  5615. goto out;
  5616. }
  5617. sense_ptr =
  5618. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  5619. *sense_ptr = cpu_to_le32(sense_handle);
  5620. }
  5621. /*
  5622. * Set the sync_cmd flag so that the ISR knows not to complete this
  5623. * cmd to the SCSI mid-layer
  5624. */
  5625. cmd->sync_cmd = 1;
  5626. if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
  5627. cmd->sync_cmd = 0;
  5628. dev_err(&instance->pdev->dev,
  5629. "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
  5630. __func__, __LINE__, cmd->frame->dcmd.opcode,
  5631. cmd->cmd_status_drv);
  5632. return -EBUSY;
  5633. }
  5634. cmd->sync_cmd = 0;
  5635. if (instance->unload == 1) {
  5636. dev_info(&instance->pdev->dev, "Driver unload is in progress "
  5637. "don't submit data to application\n");
  5638. goto out;
  5639. }
  5640. /*
  5641. * copy out the kernel buffers to user buffers
  5642. */
  5643. for (i = 0; i < ioc->sge_count; i++) {
  5644. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  5645. ioc->sgl[i].iov_len)) {
  5646. error = -EFAULT;
  5647. goto out;
  5648. }
  5649. }
  5650. /*
  5651. * copy out the sense
  5652. */
  5653. if (ioc->sense_len) {
  5654. /*
  5655. * sense_ptr points to the location that has the user
  5656. * sense buffer address
  5657. */
  5658. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  5659. ioc->sense_off);
  5660. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  5661. sense, ioc->sense_len)) {
  5662. dev_err(&instance->pdev->dev, "Failed to copy out to user "
  5663. "sense data\n");
  5664. error = -EFAULT;
  5665. goto out;
  5666. }
  5667. }
  5668. /*
  5669. * copy the status codes returned by the fw
  5670. */
  5671. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  5672. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  5673. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
  5674. error = -EFAULT;
  5675. }
  5676. out:
  5677. if (sense) {
  5678. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  5679. sense, sense_handle);
  5680. }
  5681. for (i = 0; i < ioc->sge_count; i++) {
  5682. if (kbuff_arr[i]) {
  5683. dma_free_coherent(&instance->pdev->dev,
  5684. le32_to_cpu(kern_sge32[i].length),
  5685. kbuff_arr[i],
  5686. le32_to_cpu(kern_sge32[i].phys_addr));
  5687. kbuff_arr[i] = NULL;
  5688. }
  5689. }
  5690. megasas_return_cmd(instance, cmd);
  5691. return error;
  5692. }
  5693. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  5694. {
  5695. struct megasas_iocpacket __user *user_ioc =
  5696. (struct megasas_iocpacket __user *)arg;
  5697. struct megasas_iocpacket *ioc;
  5698. struct megasas_instance *instance;
  5699. int error;
  5700. int i;
  5701. unsigned long flags;
  5702. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  5703. ioc = memdup_user(user_ioc, sizeof(*ioc));
  5704. if (IS_ERR(ioc))
  5705. return PTR_ERR(ioc);
  5706. instance = megasas_lookup_instance(ioc->host_no);
  5707. if (!instance) {
  5708. error = -ENODEV;
  5709. goto out_kfree_ioc;
  5710. }
  5711. /* Adjust ioctl wait time for VF mode */
  5712. if (instance->requestorId)
  5713. wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
  5714. /* Block ioctls in VF mode */
  5715. if (instance->requestorId && !allow_vf_ioctls) {
  5716. error = -ENODEV;
  5717. goto out_kfree_ioc;
  5718. }
  5719. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  5720. dev_err(&instance->pdev->dev, "Controller in crit error\n");
  5721. error = -ENODEV;
  5722. goto out_kfree_ioc;
  5723. }
  5724. if (instance->unload == 1) {
  5725. error = -ENODEV;
  5726. goto out_kfree_ioc;
  5727. }
  5728. if (down_interruptible(&instance->ioctl_sem)) {
  5729. error = -ERESTARTSYS;
  5730. goto out_kfree_ioc;
  5731. }
  5732. for (i = 0; i < wait_time; i++) {
  5733. spin_lock_irqsave(&instance->hba_lock, flags);
  5734. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
  5735. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5736. break;
  5737. }
  5738. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5739. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  5740. dev_notice(&instance->pdev->dev, "waiting"
  5741. "for controller reset to finish\n");
  5742. }
  5743. msleep(1000);
  5744. }
  5745. spin_lock_irqsave(&instance->hba_lock, flags);
  5746. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  5747. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5748. dev_err(&instance->pdev->dev, "timed out while"
  5749. "waiting for HBA to recover\n");
  5750. error = -ENODEV;
  5751. goto out_up;
  5752. }
  5753. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5754. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  5755. out_up:
  5756. up(&instance->ioctl_sem);
  5757. out_kfree_ioc:
  5758. kfree(ioc);
  5759. return error;
  5760. }
  5761. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  5762. {
  5763. struct megasas_instance *instance;
  5764. struct megasas_aen aen;
  5765. int error;
  5766. int i;
  5767. unsigned long flags;
  5768. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  5769. if (file->private_data != file) {
  5770. printk(KERN_DEBUG "megasas: fasync_helper was not "
  5771. "called first\n");
  5772. return -EINVAL;
  5773. }
  5774. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  5775. return -EFAULT;
  5776. instance = megasas_lookup_instance(aen.host_no);
  5777. if (!instance)
  5778. return -ENODEV;
  5779. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  5780. return -ENODEV;
  5781. }
  5782. if (instance->unload == 1) {
  5783. return -ENODEV;
  5784. }
  5785. for (i = 0; i < wait_time; i++) {
  5786. spin_lock_irqsave(&instance->hba_lock, flags);
  5787. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
  5788. spin_unlock_irqrestore(&instance->hba_lock,
  5789. flags);
  5790. break;
  5791. }
  5792. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5793. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  5794. dev_notice(&instance->pdev->dev, "waiting for"
  5795. "controller reset to finish\n");
  5796. }
  5797. msleep(1000);
  5798. }
  5799. spin_lock_irqsave(&instance->hba_lock, flags);
  5800. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  5801. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5802. dev_err(&instance->pdev->dev, "timed out while waiting"
  5803. "for HBA to recover\n");
  5804. return -ENODEV;
  5805. }
  5806. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5807. mutex_lock(&instance->reset_mutex);
  5808. error = megasas_register_aen(instance, aen.seq_num,
  5809. aen.class_locale_word);
  5810. mutex_unlock(&instance->reset_mutex);
  5811. return error;
  5812. }
  5813. /**
  5814. * megasas_mgmt_ioctl - char node ioctl entry point
  5815. */
  5816. static long
  5817. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  5818. {
  5819. switch (cmd) {
  5820. case MEGASAS_IOC_FIRMWARE:
  5821. return megasas_mgmt_ioctl_fw(file, arg);
  5822. case MEGASAS_IOC_GET_AEN:
  5823. return megasas_mgmt_ioctl_aen(file, arg);
  5824. }
  5825. return -ENOTTY;
  5826. }
  5827. #ifdef CONFIG_COMPAT
  5828. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  5829. {
  5830. struct compat_megasas_iocpacket __user *cioc =
  5831. (struct compat_megasas_iocpacket __user *)arg;
  5832. struct megasas_iocpacket __user *ioc =
  5833. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  5834. int i;
  5835. int error = 0;
  5836. compat_uptr_t ptr;
  5837. u32 local_sense_off;
  5838. u32 local_sense_len;
  5839. u32 user_sense_off;
  5840. if (clear_user(ioc, sizeof(*ioc)))
  5841. return -EFAULT;
  5842. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  5843. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  5844. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  5845. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  5846. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  5847. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  5848. return -EFAULT;
  5849. /*
  5850. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  5851. * sense_len is not null, so prepare the 64bit value under
  5852. * the same condition.
  5853. */
  5854. if (get_user(local_sense_off, &ioc->sense_off) ||
  5855. get_user(local_sense_len, &ioc->sense_len) ||
  5856. get_user(user_sense_off, &cioc->sense_off))
  5857. return -EFAULT;
  5858. if (local_sense_len) {
  5859. void __user **sense_ioc_ptr =
  5860. (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
  5861. compat_uptr_t *sense_cioc_ptr =
  5862. (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
  5863. if (get_user(ptr, sense_cioc_ptr) ||
  5864. put_user(compat_ptr(ptr), sense_ioc_ptr))
  5865. return -EFAULT;
  5866. }
  5867. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  5868. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  5869. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  5870. copy_in_user(&ioc->sgl[i].iov_len,
  5871. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  5872. return -EFAULT;
  5873. }
  5874. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  5875. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  5876. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  5877. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  5878. return -EFAULT;
  5879. }
  5880. return error;
  5881. }
  5882. static long
  5883. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  5884. unsigned long arg)
  5885. {
  5886. switch (cmd) {
  5887. case MEGASAS_IOC_FIRMWARE32:
  5888. return megasas_mgmt_compat_ioctl_fw(file, arg);
  5889. case MEGASAS_IOC_GET_AEN:
  5890. return megasas_mgmt_ioctl_aen(file, arg);
  5891. }
  5892. return -ENOTTY;
  5893. }
  5894. #endif
  5895. /*
  5896. * File operations structure for management interface
  5897. */
  5898. static const struct file_operations megasas_mgmt_fops = {
  5899. .owner = THIS_MODULE,
  5900. .open = megasas_mgmt_open,
  5901. .fasync = megasas_mgmt_fasync,
  5902. .unlocked_ioctl = megasas_mgmt_ioctl,
  5903. .poll = megasas_mgmt_poll,
  5904. #ifdef CONFIG_COMPAT
  5905. .compat_ioctl = megasas_mgmt_compat_ioctl,
  5906. #endif
  5907. .llseek = noop_llseek,
  5908. };
  5909. /*
  5910. * PCI hotplug support registration structure
  5911. */
  5912. static struct pci_driver megasas_pci_driver = {
  5913. .name = "megaraid_sas",
  5914. .id_table = megasas_pci_table,
  5915. .probe = megasas_probe_one,
  5916. .remove = megasas_detach_one,
  5917. .suspend = megasas_suspend,
  5918. .resume = megasas_resume,
  5919. .shutdown = megasas_shutdown,
  5920. };
  5921. /*
  5922. * Sysfs driver attributes
  5923. */
  5924. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  5925. {
  5926. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  5927. MEGASAS_VERSION);
  5928. }
  5929. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  5930. static ssize_t
  5931. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  5932. {
  5933. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  5934. MEGASAS_RELDATE);
  5935. }
  5936. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
  5937. static ssize_t
  5938. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  5939. {
  5940. return sprintf(buf, "%u\n", support_poll_for_event);
  5941. }
  5942. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  5943. megasas_sysfs_show_support_poll_for_event, NULL);
  5944. static ssize_t
  5945. megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
  5946. {
  5947. return sprintf(buf, "%u\n", support_device_change);
  5948. }
  5949. static DRIVER_ATTR(support_device_change, S_IRUGO,
  5950. megasas_sysfs_show_support_device_change, NULL);
  5951. static ssize_t
  5952. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  5953. {
  5954. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  5955. }
  5956. static ssize_t
  5957. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  5958. {
  5959. int retval = count;
  5960. if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
  5961. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  5962. retval = -EINVAL;
  5963. }
  5964. return retval;
  5965. }
  5966. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  5967. megasas_sysfs_set_dbg_lvl);
  5968. static void
  5969. megasas_aen_polling(struct work_struct *work)
  5970. {
  5971. struct megasas_aen_event *ev =
  5972. container_of(work, struct megasas_aen_event, hotplug_work.work);
  5973. struct megasas_instance *instance = ev->instance;
  5974. union megasas_evt_class_locale class_locale;
  5975. struct Scsi_Host *host;
  5976. struct scsi_device *sdev1;
  5977. u16 pd_index = 0;
  5978. u16 ld_index = 0;
  5979. int i, j, doscan = 0;
  5980. u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
  5981. int error;
  5982. u8 dcmd_ret = DCMD_SUCCESS;
  5983. if (!instance) {
  5984. printk(KERN_ERR "invalid instance!\n");
  5985. kfree(ev);
  5986. return;
  5987. }
  5988. /* Adjust event workqueue thread wait time for VF mode */
  5989. if (instance->requestorId)
  5990. wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
  5991. /* Don't run the event workqueue thread if OCR is running */
  5992. mutex_lock(&instance->reset_mutex);
  5993. instance->ev = NULL;
  5994. host = instance->host;
  5995. if (instance->evt_detail) {
  5996. megasas_decode_evt(instance);
  5997. switch (le32_to_cpu(instance->evt_detail->code)) {
  5998. case MR_EVT_PD_INSERTED:
  5999. case MR_EVT_PD_REMOVED:
  6000. dcmd_ret = megasas_get_pd_list(instance);
  6001. if (dcmd_ret == DCMD_SUCCESS)
  6002. doscan = SCAN_PD_CHANNEL;
  6003. break;
  6004. case MR_EVT_LD_OFFLINE:
  6005. case MR_EVT_CFG_CLEARED:
  6006. case MR_EVT_LD_DELETED:
  6007. case MR_EVT_LD_CREATED:
  6008. if (!instance->requestorId ||
  6009. (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
  6010. dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
  6011. if (dcmd_ret == DCMD_SUCCESS)
  6012. doscan = SCAN_VD_CHANNEL;
  6013. break;
  6014. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  6015. case MR_EVT_FOREIGN_CFG_IMPORTED:
  6016. case MR_EVT_LD_STATE_CHANGE:
  6017. dcmd_ret = megasas_get_pd_list(instance);
  6018. if (dcmd_ret != DCMD_SUCCESS)
  6019. break;
  6020. if (!instance->requestorId ||
  6021. (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
  6022. dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
  6023. if (dcmd_ret != DCMD_SUCCESS)
  6024. break;
  6025. doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
  6026. dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
  6027. instance->host->host_no);
  6028. break;
  6029. case MR_EVT_CTRL_PROP_CHANGED:
  6030. dcmd_ret = megasas_get_ctrl_info(instance);
  6031. break;
  6032. default:
  6033. doscan = 0;
  6034. break;
  6035. }
  6036. } else {
  6037. dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
  6038. mutex_unlock(&instance->reset_mutex);
  6039. kfree(ev);
  6040. return;
  6041. }
  6042. mutex_unlock(&instance->reset_mutex);
  6043. if (doscan & SCAN_PD_CHANNEL) {
  6044. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  6045. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  6046. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  6047. sdev1 = scsi_device_lookup(host, i, j, 0);
  6048. if (instance->pd_list[pd_index].driveState ==
  6049. MR_PD_STATE_SYSTEM) {
  6050. if (!sdev1)
  6051. scsi_add_device(host, i, j, 0);
  6052. else
  6053. scsi_device_put(sdev1);
  6054. } else {
  6055. if (sdev1) {
  6056. scsi_remove_device(sdev1);
  6057. scsi_device_put(sdev1);
  6058. }
  6059. }
  6060. }
  6061. }
  6062. }
  6063. if (doscan & SCAN_VD_CHANNEL) {
  6064. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  6065. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  6066. ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  6067. sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
  6068. if (instance->ld_ids[ld_index] != 0xff) {
  6069. if (!sdev1)
  6070. scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
  6071. else
  6072. scsi_device_put(sdev1);
  6073. } else {
  6074. if (sdev1) {
  6075. scsi_remove_device(sdev1);
  6076. scsi_device_put(sdev1);
  6077. }
  6078. }
  6079. }
  6080. }
  6081. }
  6082. if (dcmd_ret == DCMD_SUCCESS)
  6083. seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
  6084. else
  6085. seq_num = instance->last_seq_num;
  6086. /* Register AEN with FW for latest sequence number plus 1 */
  6087. class_locale.members.reserved = 0;
  6088. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  6089. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  6090. if (instance->aen_cmd != NULL) {
  6091. kfree(ev);
  6092. return;
  6093. }
  6094. mutex_lock(&instance->reset_mutex);
  6095. error = megasas_register_aen(instance, seq_num,
  6096. class_locale.word);
  6097. if (error)
  6098. dev_err(&instance->pdev->dev,
  6099. "register aen failed error %x\n", error);
  6100. mutex_unlock(&instance->reset_mutex);
  6101. kfree(ev);
  6102. }
  6103. /**
  6104. * megasas_init - Driver load entry point
  6105. */
  6106. static int __init megasas_init(void)
  6107. {
  6108. int rval;
  6109. /*
  6110. * Booted in kdump kernel, minimize memory footprints by
  6111. * disabling few features
  6112. */
  6113. if (reset_devices) {
  6114. msix_vectors = 1;
  6115. rdpq_enable = 0;
  6116. dual_qdepth_disable = 1;
  6117. }
  6118. /*
  6119. * Announce driver version and other information
  6120. */
  6121. pr_info("megasas: %s\n", MEGASAS_VERSION);
  6122. spin_lock_init(&poll_aen_lock);
  6123. support_poll_for_event = 2;
  6124. support_device_change = 1;
  6125. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  6126. /*
  6127. * Register character device node
  6128. */
  6129. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  6130. if (rval < 0) {
  6131. printk(KERN_DEBUG "megasas: failed to open device node\n");
  6132. return rval;
  6133. }
  6134. megasas_mgmt_majorno = rval;
  6135. /*
  6136. * Register ourselves as PCI hotplug module
  6137. */
  6138. rval = pci_register_driver(&megasas_pci_driver);
  6139. if (rval) {
  6140. printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
  6141. goto err_pcidrv;
  6142. }
  6143. rval = driver_create_file(&megasas_pci_driver.driver,
  6144. &driver_attr_version);
  6145. if (rval)
  6146. goto err_dcf_attr_ver;
  6147. rval = driver_create_file(&megasas_pci_driver.driver,
  6148. &driver_attr_release_date);
  6149. if (rval)
  6150. goto err_dcf_rel_date;
  6151. rval = driver_create_file(&megasas_pci_driver.driver,
  6152. &driver_attr_support_poll_for_event);
  6153. if (rval)
  6154. goto err_dcf_support_poll_for_event;
  6155. rval = driver_create_file(&megasas_pci_driver.driver,
  6156. &driver_attr_dbg_lvl);
  6157. if (rval)
  6158. goto err_dcf_dbg_lvl;
  6159. rval = driver_create_file(&megasas_pci_driver.driver,
  6160. &driver_attr_support_device_change);
  6161. if (rval)
  6162. goto err_dcf_support_device_change;
  6163. return rval;
  6164. err_dcf_support_device_change:
  6165. driver_remove_file(&megasas_pci_driver.driver,
  6166. &driver_attr_dbg_lvl);
  6167. err_dcf_dbg_lvl:
  6168. driver_remove_file(&megasas_pci_driver.driver,
  6169. &driver_attr_support_poll_for_event);
  6170. err_dcf_support_poll_for_event:
  6171. driver_remove_file(&megasas_pci_driver.driver,
  6172. &driver_attr_release_date);
  6173. err_dcf_rel_date:
  6174. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  6175. err_dcf_attr_ver:
  6176. pci_unregister_driver(&megasas_pci_driver);
  6177. err_pcidrv:
  6178. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  6179. return rval;
  6180. }
  6181. /**
  6182. * megasas_exit - Driver unload entry point
  6183. */
  6184. static void __exit megasas_exit(void)
  6185. {
  6186. driver_remove_file(&megasas_pci_driver.driver,
  6187. &driver_attr_dbg_lvl);
  6188. driver_remove_file(&megasas_pci_driver.driver,
  6189. &driver_attr_support_poll_for_event);
  6190. driver_remove_file(&megasas_pci_driver.driver,
  6191. &driver_attr_support_device_change);
  6192. driver_remove_file(&megasas_pci_driver.driver,
  6193. &driver_attr_release_date);
  6194. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  6195. pci_unregister_driver(&megasas_pci_driver);
  6196. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  6197. }
  6198. module_init(megasas_init);
  6199. module_exit(megasas_exit);