megaraid_sas_base.c 142 KB

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