megaraid_sas_base.c 143 KB

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