megaraid_sas_base.c 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522
  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_GEN2_ENABLE_INTERRUPT_MASK) {
  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_HBA_OPERATIONAL) {
  1279. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1280. return SCSI_MLQUEUE_HOST_BUSY;
  1281. }
  1282. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1283. scmd->scsi_done = done;
  1284. scmd->result = 0;
  1285. if (MEGASAS_IS_LOGICAL(scmd) &&
  1286. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  1287. scmd->result = DID_BAD_TARGET << 16;
  1288. goto out_done;
  1289. }
  1290. switch (scmd->cmnd[0]) {
  1291. case SYNCHRONIZE_CACHE:
  1292. /*
  1293. * FW takes care of flush cache on its own
  1294. * No need to send it down
  1295. */
  1296. scmd->result = DID_OK << 16;
  1297. goto out_done;
  1298. default:
  1299. break;
  1300. }
  1301. if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
  1302. printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
  1303. return SCSI_MLQUEUE_HOST_BUSY;
  1304. }
  1305. return 0;
  1306. out_done:
  1307. done(scmd);
  1308. return 0;
  1309. }
  1310. static DEF_SCSI_QCMD(megasas_queue_command)
  1311. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1312. {
  1313. int i;
  1314. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1315. if ((megasas_mgmt_info.instance[i]) &&
  1316. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1317. return megasas_mgmt_info.instance[i];
  1318. }
  1319. return NULL;
  1320. }
  1321. static int megasas_slave_configure(struct scsi_device *sdev)
  1322. {
  1323. u16 pd_index = 0;
  1324. struct megasas_instance *instance ;
  1325. instance = megasas_lookup_instance(sdev->host->host_no);
  1326. /*
  1327. * Don't export physical disk devices to the disk driver.
  1328. *
  1329. * FIXME: Currently we don't export them to the midlayer at all.
  1330. * That will be fixed once LSI engineers have audited the
  1331. * firmware for possible issues.
  1332. */
  1333. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1334. sdev->type == TYPE_DISK) {
  1335. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1336. sdev->id;
  1337. if (instance->pd_list[pd_index].driveState ==
  1338. MR_PD_STATE_SYSTEM) {
  1339. blk_queue_rq_timeout(sdev->request_queue,
  1340. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1341. return 0;
  1342. }
  1343. return -ENXIO;
  1344. }
  1345. /*
  1346. * The RAID firmware may require extended timeouts.
  1347. */
  1348. blk_queue_rq_timeout(sdev->request_queue,
  1349. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1350. return 0;
  1351. }
  1352. static int megasas_slave_alloc(struct scsi_device *sdev)
  1353. {
  1354. u16 pd_index = 0;
  1355. struct megasas_instance *instance ;
  1356. instance = megasas_lookup_instance(sdev->host->host_no);
  1357. if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
  1358. (sdev->type == TYPE_DISK)) {
  1359. /*
  1360. * Open the OS scan to the SYSTEM PD
  1361. */
  1362. pd_index =
  1363. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1364. sdev->id;
  1365. if ((instance->pd_list[pd_index].driveState ==
  1366. MR_PD_STATE_SYSTEM) &&
  1367. (instance->pd_list[pd_index].driveType ==
  1368. TYPE_DISK)) {
  1369. return 0;
  1370. }
  1371. return -ENXIO;
  1372. }
  1373. return 0;
  1374. }
  1375. void megaraid_sas_kill_hba(struct megasas_instance *instance)
  1376. {
  1377. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1378. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  1379. (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  1380. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
  1381. writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
  1382. } else {
  1383. writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
  1384. }
  1385. }
  1386. /**
  1387. * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
  1388. * restored to max value
  1389. * @instance: Adapter soft state
  1390. *
  1391. */
  1392. void
  1393. megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
  1394. {
  1395. unsigned long flags;
  1396. if (instance->flag & MEGASAS_FW_BUSY
  1397. && time_after(jiffies, instance->last_time + 5 * HZ)
  1398. && atomic_read(&instance->fw_outstanding) <
  1399. instance->throttlequeuedepth + 1) {
  1400. spin_lock_irqsave(instance->host->host_lock, flags);
  1401. instance->flag &= ~MEGASAS_FW_BUSY;
  1402. if ((instance->pdev->device ==
  1403. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1404. (instance->pdev->device ==
  1405. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1406. instance->host->can_queue =
  1407. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  1408. } else
  1409. instance->host->can_queue =
  1410. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1411. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1412. }
  1413. }
  1414. /**
  1415. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1416. * @instance_addr: Address of adapter soft state
  1417. *
  1418. * Tasklet to complete cmds
  1419. */
  1420. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1421. {
  1422. u32 producer;
  1423. u32 consumer;
  1424. u32 context;
  1425. struct megasas_cmd *cmd;
  1426. struct megasas_instance *instance =
  1427. (struct megasas_instance *)instance_addr;
  1428. unsigned long flags;
  1429. /* If we have already declared adapter dead, donot complete cmds */
  1430. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
  1431. return;
  1432. spin_lock_irqsave(&instance->completion_lock, flags);
  1433. producer = *instance->producer;
  1434. consumer = *instance->consumer;
  1435. while (consumer != producer) {
  1436. context = instance->reply_queue[consumer];
  1437. if (context >= instance->max_fw_cmds) {
  1438. printk(KERN_ERR "Unexpected context value %x\n",
  1439. context);
  1440. BUG();
  1441. }
  1442. cmd = instance->cmd_list[context];
  1443. megasas_complete_cmd(instance, cmd, DID_OK);
  1444. consumer++;
  1445. if (consumer == (instance->max_fw_cmds + 1)) {
  1446. consumer = 0;
  1447. }
  1448. }
  1449. *instance->consumer = producer;
  1450. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1451. /*
  1452. * Check if we can restore can_queue
  1453. */
  1454. megasas_check_and_restore_queue_depth(instance);
  1455. }
  1456. static void
  1457. megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
  1458. static void
  1459. process_fw_state_change_wq(struct work_struct *work);
  1460. void megasas_do_ocr(struct megasas_instance *instance)
  1461. {
  1462. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  1463. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  1464. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  1465. *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
  1466. }
  1467. instance->instancet->disable_intr(instance->reg_set);
  1468. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  1469. instance->issuepend_done = 0;
  1470. atomic_set(&instance->fw_outstanding, 0);
  1471. megasas_internal_reset_defer_cmds(instance);
  1472. process_fw_state_change_wq(&instance->work_init);
  1473. }
  1474. /**
  1475. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  1476. * @instance: Adapter soft state
  1477. *
  1478. * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
  1479. * complete all its outstanding commands. Returns error if one or more IOs
  1480. * are pending after this time period. It also marks the controller dead.
  1481. */
  1482. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  1483. {
  1484. int i;
  1485. u32 reset_index;
  1486. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  1487. u8 adprecovery;
  1488. unsigned long flags;
  1489. struct list_head clist_local;
  1490. struct megasas_cmd *reset_cmd;
  1491. u32 fw_state;
  1492. u8 kill_adapter_flag;
  1493. spin_lock_irqsave(&instance->hba_lock, flags);
  1494. adprecovery = instance->adprecovery;
  1495. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1496. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1497. INIT_LIST_HEAD(&clist_local);
  1498. spin_lock_irqsave(&instance->hba_lock, flags);
  1499. list_splice_init(&instance->internal_reset_pending_q,
  1500. &clist_local);
  1501. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1502. printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
  1503. for (i = 0; i < wait_time; i++) {
  1504. msleep(1000);
  1505. spin_lock_irqsave(&instance->hba_lock, flags);
  1506. adprecovery = instance->adprecovery;
  1507. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1508. if (adprecovery == MEGASAS_HBA_OPERATIONAL)
  1509. break;
  1510. }
  1511. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1512. printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
  1513. spin_lock_irqsave(&instance->hba_lock, flags);
  1514. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1515. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1516. return FAILED;
  1517. }
  1518. reset_index = 0;
  1519. while (!list_empty(&clist_local)) {
  1520. reset_cmd = list_entry((&clist_local)->next,
  1521. struct megasas_cmd, list);
  1522. list_del_init(&reset_cmd->list);
  1523. if (reset_cmd->scmd) {
  1524. reset_cmd->scmd->result = DID_RESET << 16;
  1525. printk(KERN_NOTICE "%d:%p reset [%02x]\n",
  1526. reset_index, reset_cmd,
  1527. reset_cmd->scmd->cmnd[0]);
  1528. reset_cmd->scmd->scsi_done(reset_cmd->scmd);
  1529. megasas_return_cmd(instance, reset_cmd);
  1530. } else if (reset_cmd->sync_cmd) {
  1531. printk(KERN_NOTICE "megasas:%p synch cmds"
  1532. "reset queue\n",
  1533. reset_cmd);
  1534. reset_cmd->cmd_status = ENODATA;
  1535. instance->instancet->fire_cmd(instance,
  1536. reset_cmd->frame_phys_addr,
  1537. 0, instance->reg_set);
  1538. } else {
  1539. printk(KERN_NOTICE "megasas: %p unexpected"
  1540. "cmds lst\n",
  1541. reset_cmd);
  1542. }
  1543. reset_index++;
  1544. }
  1545. return SUCCESS;
  1546. }
  1547. for (i = 0; i < resetwaittime; i++) {
  1548. int outstanding = atomic_read(&instance->fw_outstanding);
  1549. if (!outstanding)
  1550. break;
  1551. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  1552. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  1553. "commands to complete\n",i,outstanding);
  1554. /*
  1555. * Call cmd completion routine. Cmd to be
  1556. * be completed directly without depending on isr.
  1557. */
  1558. megasas_complete_cmd_dpc((unsigned long)instance);
  1559. }
  1560. msleep(1000);
  1561. }
  1562. i = 0;
  1563. kill_adapter_flag = 0;
  1564. do {
  1565. fw_state = instance->instancet->read_fw_status_reg(
  1566. instance->reg_set) & MFI_STATE_MASK;
  1567. if ((fw_state == MFI_STATE_FAULT) &&
  1568. (instance->disableOnlineCtrlReset == 0)) {
  1569. if (i == 3) {
  1570. kill_adapter_flag = 2;
  1571. break;
  1572. }
  1573. megasas_do_ocr(instance);
  1574. kill_adapter_flag = 1;
  1575. /* wait for 1 secs to let FW finish the pending cmds */
  1576. msleep(1000);
  1577. }
  1578. i++;
  1579. } while (i <= 3);
  1580. if (atomic_read(&instance->fw_outstanding) &&
  1581. !kill_adapter_flag) {
  1582. if (instance->disableOnlineCtrlReset == 0) {
  1583. megasas_do_ocr(instance);
  1584. /* wait for 5 secs to let FW finish the pending cmds */
  1585. for (i = 0; i < wait_time; i++) {
  1586. int outstanding =
  1587. atomic_read(&instance->fw_outstanding);
  1588. if (!outstanding)
  1589. return SUCCESS;
  1590. msleep(1000);
  1591. }
  1592. }
  1593. }
  1594. if (atomic_read(&instance->fw_outstanding) ||
  1595. (kill_adapter_flag == 2)) {
  1596. printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
  1597. /*
  1598. * Send signal to FW to stop processing any pending cmds.
  1599. * The controller will be taken offline by the OS now.
  1600. */
  1601. if ((instance->pdev->device ==
  1602. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1603. (instance->pdev->device ==
  1604. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1605. writel(MFI_STOP_ADP,
  1606. &instance->reg_set->doorbell);
  1607. } else {
  1608. writel(MFI_STOP_ADP,
  1609. &instance->reg_set->inbound_doorbell);
  1610. }
  1611. megasas_dump_pending_frames(instance);
  1612. spin_lock_irqsave(&instance->hba_lock, flags);
  1613. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1614. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1615. return FAILED;
  1616. }
  1617. printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
  1618. return SUCCESS;
  1619. }
  1620. /**
  1621. * megasas_generic_reset - Generic reset routine
  1622. * @scmd: Mid-layer SCSI command
  1623. *
  1624. * This routine implements a generic reset handler for device, bus and host
  1625. * reset requests. Device, bus and host specific reset handlers can use this
  1626. * function after they do their specific tasks.
  1627. */
  1628. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  1629. {
  1630. int ret_val;
  1631. struct megasas_instance *instance;
  1632. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1633. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
  1634. scmd->cmnd[0], scmd->retries);
  1635. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  1636. printk(KERN_ERR "megasas: cannot recover from previous reset "
  1637. "failures\n");
  1638. return FAILED;
  1639. }
  1640. ret_val = megasas_wait_for_outstanding(instance);
  1641. if (ret_val == SUCCESS)
  1642. printk(KERN_NOTICE "megasas: reset successful \n");
  1643. else
  1644. printk(KERN_ERR "megasas: failed to do reset\n");
  1645. return ret_val;
  1646. }
  1647. /**
  1648. * megasas_reset_timer - quiesce the adapter if required
  1649. * @scmd: scsi cmnd
  1650. *
  1651. * Sets the FW busy flag and reduces the host->can_queue if the
  1652. * cmd has not been completed within the timeout period.
  1653. */
  1654. static enum
  1655. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  1656. {
  1657. struct megasas_instance *instance;
  1658. unsigned long flags;
  1659. if (time_after(jiffies, scmd->jiffies_at_alloc +
  1660. (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
  1661. return BLK_EH_NOT_HANDLED;
  1662. }
  1663. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1664. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  1665. /* FW is busy, throttle IO */
  1666. spin_lock_irqsave(instance->host->host_lock, flags);
  1667. instance->host->can_queue = instance->throttlequeuedepth;
  1668. instance->last_time = jiffies;
  1669. instance->flag |= MEGASAS_FW_BUSY;
  1670. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1671. }
  1672. return BLK_EH_RESET_TIMER;
  1673. }
  1674. /**
  1675. * megasas_reset_device - Device reset handler entry point
  1676. */
  1677. static int megasas_reset_device(struct scsi_cmnd *scmd)
  1678. {
  1679. int ret;
  1680. /*
  1681. * First wait for all commands to complete
  1682. */
  1683. ret = megasas_generic_reset(scmd);
  1684. return ret;
  1685. }
  1686. /**
  1687. * megasas_reset_bus_host - Bus & host reset handler entry point
  1688. */
  1689. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  1690. {
  1691. int ret;
  1692. struct megasas_instance *instance;
  1693. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1694. /*
  1695. * First wait for all commands to complete
  1696. */
  1697. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  1698. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
  1699. ret = megasas_reset_fusion(scmd->device->host);
  1700. else
  1701. ret = megasas_generic_reset(scmd);
  1702. return ret;
  1703. }
  1704. /**
  1705. * megasas_bios_param - Returns disk geometry for a disk
  1706. * @sdev: device handle
  1707. * @bdev: block device
  1708. * @capacity: drive capacity
  1709. * @geom: geometry parameters
  1710. */
  1711. static int
  1712. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1713. sector_t capacity, int geom[])
  1714. {
  1715. int heads;
  1716. int sectors;
  1717. sector_t cylinders;
  1718. unsigned long tmp;
  1719. /* Default heads (64) & sectors (32) */
  1720. heads = 64;
  1721. sectors = 32;
  1722. tmp = heads * sectors;
  1723. cylinders = capacity;
  1724. sector_div(cylinders, tmp);
  1725. /*
  1726. * Handle extended translation size for logical drives > 1Gb
  1727. */
  1728. if (capacity >= 0x200000) {
  1729. heads = 255;
  1730. sectors = 63;
  1731. tmp = heads*sectors;
  1732. cylinders = capacity;
  1733. sector_div(cylinders, tmp);
  1734. }
  1735. geom[0] = heads;
  1736. geom[1] = sectors;
  1737. geom[2] = cylinders;
  1738. return 0;
  1739. }
  1740. static void megasas_aen_polling(struct work_struct *work);
  1741. /**
  1742. * megasas_service_aen - Processes an event notification
  1743. * @instance: Adapter soft state
  1744. * @cmd: AEN command completed by the ISR
  1745. *
  1746. * For AEN, driver sends a command down to FW that is held by the FW till an
  1747. * event occurs. When an event of interest occurs, FW completes the command
  1748. * that it was previously holding.
  1749. *
  1750. * This routines sends SIGIO signal to processes that have registered with the
  1751. * driver for AEN.
  1752. */
  1753. static void
  1754. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1755. {
  1756. unsigned long flags;
  1757. /*
  1758. * Don't signal app if it is just an aborted previously registered aen
  1759. */
  1760. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  1761. spin_lock_irqsave(&poll_aen_lock, flags);
  1762. megasas_poll_wait_aen = 1;
  1763. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1764. wake_up(&megasas_poll_wait);
  1765. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  1766. }
  1767. else
  1768. cmd->abort_aen = 0;
  1769. instance->aen_cmd = NULL;
  1770. megasas_return_cmd(instance, cmd);
  1771. if ((instance->unload == 0) &&
  1772. ((instance->issuepend_done == 1))) {
  1773. struct megasas_aen_event *ev;
  1774. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  1775. if (!ev) {
  1776. printk(KERN_ERR "megasas_service_aen: out of memory\n");
  1777. } else {
  1778. ev->instance = instance;
  1779. instance->ev = ev;
  1780. INIT_DELAYED_WORK(&ev->hotplug_work,
  1781. megasas_aen_polling);
  1782. schedule_delayed_work(&ev->hotplug_work, 0);
  1783. }
  1784. }
  1785. }
  1786. static int megasas_change_queue_depth(struct scsi_device *sdev,
  1787. int queue_depth, int reason)
  1788. {
  1789. if (reason != SCSI_QDEPTH_DEFAULT)
  1790. return -EOPNOTSUPP;
  1791. if (queue_depth > sdev->host->can_queue)
  1792. queue_depth = sdev->host->can_queue;
  1793. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
  1794. queue_depth);
  1795. return queue_depth;
  1796. }
  1797. /*
  1798. * Scsi host template for megaraid_sas driver
  1799. */
  1800. static struct scsi_host_template megasas_template = {
  1801. .module = THIS_MODULE,
  1802. .name = "LSI SAS based MegaRAID driver",
  1803. .proc_name = "megaraid_sas",
  1804. .slave_configure = megasas_slave_configure,
  1805. .slave_alloc = megasas_slave_alloc,
  1806. .queuecommand = megasas_queue_command,
  1807. .eh_device_reset_handler = megasas_reset_device,
  1808. .eh_bus_reset_handler = megasas_reset_bus_host,
  1809. .eh_host_reset_handler = megasas_reset_bus_host,
  1810. .eh_timed_out = megasas_reset_timer,
  1811. .bios_param = megasas_bios_param,
  1812. .use_clustering = ENABLE_CLUSTERING,
  1813. .change_queue_depth = megasas_change_queue_depth,
  1814. };
  1815. /**
  1816. * megasas_complete_int_cmd - Completes an internal command
  1817. * @instance: Adapter soft state
  1818. * @cmd: Command to be completed
  1819. *
  1820. * The megasas_issue_blocked_cmd() function waits for a command to complete
  1821. * after it issues a command. This function wakes up that waiting routine by
  1822. * calling wake_up() on the wait queue.
  1823. */
  1824. static void
  1825. megasas_complete_int_cmd(struct megasas_instance *instance,
  1826. struct megasas_cmd *cmd)
  1827. {
  1828. cmd->cmd_status = cmd->frame->io.cmd_status;
  1829. if (cmd->cmd_status == ENODATA) {
  1830. cmd->cmd_status = 0;
  1831. }
  1832. wake_up(&instance->int_cmd_wait_q);
  1833. }
  1834. /**
  1835. * megasas_complete_abort - Completes aborting a command
  1836. * @instance: Adapter soft state
  1837. * @cmd: Cmd that was issued to abort another cmd
  1838. *
  1839. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  1840. * after it issues an abort on a previously issued command. This function
  1841. * wakes up all functions waiting on the same wait queue.
  1842. */
  1843. static void
  1844. megasas_complete_abort(struct megasas_instance *instance,
  1845. struct megasas_cmd *cmd)
  1846. {
  1847. if (cmd->sync_cmd) {
  1848. cmd->sync_cmd = 0;
  1849. cmd->cmd_status = 0;
  1850. wake_up(&instance->abort_cmd_wait_q);
  1851. }
  1852. return;
  1853. }
  1854. /**
  1855. * megasas_complete_cmd - Completes a command
  1856. * @instance: Adapter soft state
  1857. * @cmd: Command to be completed
  1858. * @alt_status: If non-zero, use this value as status to
  1859. * SCSI mid-layer instead of the value returned
  1860. * by the FW. This should be used if caller wants
  1861. * an alternate status (as in the case of aborted
  1862. * commands)
  1863. */
  1864. void
  1865. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1866. u8 alt_status)
  1867. {
  1868. int exception = 0;
  1869. struct megasas_header *hdr = &cmd->frame->hdr;
  1870. unsigned long flags;
  1871. struct fusion_context *fusion = instance->ctrl_context;
  1872. /* flag for the retry reset */
  1873. cmd->retry_for_fw_reset = 0;
  1874. if (cmd->scmd)
  1875. cmd->scmd->SCp.ptr = NULL;
  1876. switch (hdr->cmd) {
  1877. case MFI_CMD_INVALID:
  1878. /* Some older 1068 controller FW may keep a pended
  1879. MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
  1880. when booting the kdump kernel. Ignore this command to
  1881. prevent a kernel panic on shutdown of the kdump kernel. */
  1882. printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
  1883. "completed.\n");
  1884. printk(KERN_WARNING "megaraid_sas: If you have a controller "
  1885. "other than PERC5, please upgrade your firmware.\n");
  1886. break;
  1887. case MFI_CMD_PD_SCSI_IO:
  1888. case MFI_CMD_LD_SCSI_IO:
  1889. /*
  1890. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1891. * issued either through an IO path or an IOCTL path. If it
  1892. * was via IOCTL, we will send it to internal completion.
  1893. */
  1894. if (cmd->sync_cmd) {
  1895. cmd->sync_cmd = 0;
  1896. megasas_complete_int_cmd(instance, cmd);
  1897. break;
  1898. }
  1899. case MFI_CMD_LD_READ:
  1900. case MFI_CMD_LD_WRITE:
  1901. if (alt_status) {
  1902. cmd->scmd->result = alt_status << 16;
  1903. exception = 1;
  1904. }
  1905. if (exception) {
  1906. atomic_dec(&instance->fw_outstanding);
  1907. scsi_dma_unmap(cmd->scmd);
  1908. cmd->scmd->scsi_done(cmd->scmd);
  1909. megasas_return_cmd(instance, cmd);
  1910. break;
  1911. }
  1912. switch (hdr->cmd_status) {
  1913. case MFI_STAT_OK:
  1914. cmd->scmd->result = DID_OK << 16;
  1915. break;
  1916. case MFI_STAT_SCSI_IO_FAILED:
  1917. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1918. cmd->scmd->result =
  1919. (DID_ERROR << 16) | hdr->scsi_status;
  1920. break;
  1921. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1922. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1923. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1924. memset(cmd->scmd->sense_buffer, 0,
  1925. SCSI_SENSE_BUFFERSIZE);
  1926. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1927. hdr->sense_len);
  1928. cmd->scmd->result |= DRIVER_SENSE << 24;
  1929. }
  1930. break;
  1931. case MFI_STAT_LD_OFFLINE:
  1932. case MFI_STAT_DEVICE_NOT_FOUND:
  1933. cmd->scmd->result = DID_BAD_TARGET << 16;
  1934. break;
  1935. default:
  1936. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1937. hdr->cmd_status);
  1938. cmd->scmd->result = DID_ERROR << 16;
  1939. break;
  1940. }
  1941. atomic_dec(&instance->fw_outstanding);
  1942. scsi_dma_unmap(cmd->scmd);
  1943. cmd->scmd->scsi_done(cmd->scmd);
  1944. megasas_return_cmd(instance, cmd);
  1945. break;
  1946. case MFI_CMD_SMP:
  1947. case MFI_CMD_STP:
  1948. case MFI_CMD_DCMD:
  1949. /* Check for LD map update */
  1950. if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
  1951. (cmd->frame->dcmd.mbox.b[1] == 1)) {
  1952. spin_lock_irqsave(instance->host->host_lock, flags);
  1953. if (cmd->frame->hdr.cmd_status != 0) {
  1954. if (cmd->frame->hdr.cmd_status !=
  1955. MFI_STAT_NOT_FOUND)
  1956. printk(KERN_WARNING "megasas: map sync"
  1957. "failed, status = 0x%x.\n",
  1958. cmd->frame->hdr.cmd_status);
  1959. else {
  1960. megasas_return_cmd(instance, cmd);
  1961. spin_unlock_irqrestore(
  1962. instance->host->host_lock,
  1963. flags);
  1964. break;
  1965. }
  1966. } else
  1967. instance->map_id++;
  1968. megasas_return_cmd(instance, cmd);
  1969. if (MR_ValidateMapInfo(
  1970. fusion->ld_map[(instance->map_id & 1)],
  1971. fusion->load_balance_info))
  1972. fusion->fast_path_io = 1;
  1973. else
  1974. fusion->fast_path_io = 0;
  1975. megasas_sync_map_info(instance);
  1976. spin_unlock_irqrestore(instance->host->host_lock,
  1977. flags);
  1978. break;
  1979. }
  1980. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  1981. cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
  1982. spin_lock_irqsave(&poll_aen_lock, flags);
  1983. megasas_poll_wait_aen = 0;
  1984. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1985. }
  1986. /*
  1987. * See if got an event notification
  1988. */
  1989. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1990. megasas_service_aen(instance, cmd);
  1991. else
  1992. megasas_complete_int_cmd(instance, cmd);
  1993. break;
  1994. case MFI_CMD_ABORT:
  1995. /*
  1996. * Cmd issued to abort another cmd returned
  1997. */
  1998. megasas_complete_abort(instance, cmd);
  1999. break;
  2000. default:
  2001. printk("megasas: Unknown command completed! [0x%X]\n",
  2002. hdr->cmd);
  2003. break;
  2004. }
  2005. }
  2006. /**
  2007. * megasas_issue_pending_cmds_again - issue all pending cmds
  2008. * in FW again because of the fw reset
  2009. * @instance: Adapter soft state
  2010. */
  2011. static inline void
  2012. megasas_issue_pending_cmds_again(struct megasas_instance *instance)
  2013. {
  2014. struct megasas_cmd *cmd;
  2015. struct list_head clist_local;
  2016. union megasas_evt_class_locale class_locale;
  2017. unsigned long flags;
  2018. u32 seq_num;
  2019. INIT_LIST_HEAD(&clist_local);
  2020. spin_lock_irqsave(&instance->hba_lock, flags);
  2021. list_splice_init(&instance->internal_reset_pending_q, &clist_local);
  2022. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2023. while (!list_empty(&clist_local)) {
  2024. cmd = list_entry((&clist_local)->next,
  2025. struct megasas_cmd, list);
  2026. list_del_init(&cmd->list);
  2027. if (cmd->sync_cmd || cmd->scmd) {
  2028. printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
  2029. "detected to be pending while HBA reset.\n",
  2030. cmd, cmd->scmd, cmd->sync_cmd);
  2031. cmd->retry_for_fw_reset++;
  2032. if (cmd->retry_for_fw_reset == 3) {
  2033. printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
  2034. "was tried multiple times during reset."
  2035. "Shutting down the HBA\n",
  2036. cmd, cmd->scmd, cmd->sync_cmd);
  2037. megaraid_sas_kill_hba(instance);
  2038. instance->adprecovery =
  2039. MEGASAS_HW_CRITICAL_ERROR;
  2040. return;
  2041. }
  2042. }
  2043. if (cmd->sync_cmd == 1) {
  2044. if (cmd->scmd) {
  2045. printk(KERN_NOTICE "megaraid_sas: unexpected"
  2046. "cmd attached to internal command!\n");
  2047. }
  2048. printk(KERN_NOTICE "megasas: %p synchronous cmd"
  2049. "on the internal reset queue,"
  2050. "issue it again.\n", cmd);
  2051. cmd->cmd_status = ENODATA;
  2052. instance->instancet->fire_cmd(instance,
  2053. cmd->frame_phys_addr ,
  2054. 0, instance->reg_set);
  2055. } else if (cmd->scmd) {
  2056. printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
  2057. "detected on the internal queue, issue again.\n",
  2058. cmd, cmd->scmd->cmnd[0]);
  2059. atomic_inc(&instance->fw_outstanding);
  2060. instance->instancet->fire_cmd(instance,
  2061. cmd->frame_phys_addr,
  2062. cmd->frame_count-1, instance->reg_set);
  2063. } else {
  2064. printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
  2065. "internal reset defer list while re-issue!!\n",
  2066. cmd);
  2067. }
  2068. }
  2069. if (instance->aen_cmd) {
  2070. printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
  2071. megasas_return_cmd(instance, instance->aen_cmd);
  2072. instance->aen_cmd = NULL;
  2073. }
  2074. /*
  2075. * Initiate AEN (Asynchronous Event Notification)
  2076. */
  2077. seq_num = instance->last_seq_num;
  2078. class_locale.members.reserved = 0;
  2079. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2080. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2081. megasas_register_aen(instance, seq_num, class_locale.word);
  2082. }
  2083. /**
  2084. * Move the internal reset pending commands to a deferred queue.
  2085. *
  2086. * We move the commands pending at internal reset time to a
  2087. * pending queue. This queue would be flushed after successful
  2088. * completion of the internal reset sequence. if the internal reset
  2089. * did not complete in time, the kernel reset handler would flush
  2090. * these commands.
  2091. **/
  2092. static void
  2093. megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
  2094. {
  2095. struct megasas_cmd *cmd;
  2096. int i;
  2097. u32 max_cmd = instance->max_fw_cmds;
  2098. u32 defer_index;
  2099. unsigned long flags;
  2100. defer_index = 0;
  2101. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  2102. for (i = 0; i < max_cmd; i++) {
  2103. cmd = instance->cmd_list[i];
  2104. if (cmd->sync_cmd == 1 || cmd->scmd) {
  2105. printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
  2106. "on the defer queue as internal\n",
  2107. defer_index, cmd, cmd->sync_cmd, cmd->scmd);
  2108. if (!list_empty(&cmd->list)) {
  2109. printk(KERN_NOTICE "megaraid_sas: ERROR while"
  2110. " moving this cmd:%p, %d %p, it was"
  2111. "discovered on some list?\n",
  2112. cmd, cmd->sync_cmd, cmd->scmd);
  2113. list_del_init(&cmd->list);
  2114. }
  2115. defer_index++;
  2116. list_add_tail(&cmd->list,
  2117. &instance->internal_reset_pending_q);
  2118. }
  2119. }
  2120. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  2121. }
  2122. static void
  2123. process_fw_state_change_wq(struct work_struct *work)
  2124. {
  2125. struct megasas_instance *instance =
  2126. container_of(work, struct megasas_instance, work_init);
  2127. u32 wait;
  2128. unsigned long flags;
  2129. if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
  2130. printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
  2131. instance->adprecovery);
  2132. return ;
  2133. }
  2134. if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
  2135. printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
  2136. "state, restarting it...\n");
  2137. instance->instancet->disable_intr(instance->reg_set);
  2138. atomic_set(&instance->fw_outstanding, 0);
  2139. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2140. instance->instancet->adp_reset(instance, instance->reg_set);
  2141. atomic_set(&instance->fw_reset_no_pci_access, 0 );
  2142. printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
  2143. "initiating next stage...\n");
  2144. printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
  2145. "state 2 starting...\n");
  2146. /*waitting for about 20 second before start the second init*/
  2147. for (wait = 0; wait < 30; wait++) {
  2148. msleep(1000);
  2149. }
  2150. if (megasas_transition_to_ready(instance, 1)) {
  2151. printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
  2152. megaraid_sas_kill_hba(instance);
  2153. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  2154. return ;
  2155. }
  2156. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  2157. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  2158. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
  2159. ) {
  2160. *instance->consumer = *instance->producer;
  2161. } else {
  2162. *instance->consumer = 0;
  2163. *instance->producer = 0;
  2164. }
  2165. megasas_issue_init_mfi(instance);
  2166. spin_lock_irqsave(&instance->hba_lock, flags);
  2167. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  2168. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2169. instance->instancet->enable_intr(instance->reg_set);
  2170. megasas_issue_pending_cmds_again(instance);
  2171. instance->issuepend_done = 1;
  2172. }
  2173. return ;
  2174. }
  2175. /**
  2176. * megasas_deplete_reply_queue - Processes all completed commands
  2177. * @instance: Adapter soft state
  2178. * @alt_status: Alternate status to be returned to
  2179. * SCSI mid-layer instead of the status
  2180. * returned by the FW
  2181. * Note: this must be called with hba lock held
  2182. */
  2183. static int
  2184. megasas_deplete_reply_queue(struct megasas_instance *instance,
  2185. u8 alt_status)
  2186. {
  2187. u32 mfiStatus;
  2188. u32 fw_state;
  2189. if ((mfiStatus = instance->instancet->check_reset(instance,
  2190. instance->reg_set)) == 1) {
  2191. return IRQ_HANDLED;
  2192. }
  2193. if ((mfiStatus = instance->instancet->clear_intr(
  2194. instance->reg_set)
  2195. ) == 0) {
  2196. /* Hardware may not set outbound_intr_status in MSI-X mode */
  2197. if (!instance->msix_vectors)
  2198. return IRQ_NONE;
  2199. }
  2200. instance->mfiStatus = mfiStatus;
  2201. if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
  2202. fw_state = instance->instancet->read_fw_status_reg(
  2203. instance->reg_set) & MFI_STATE_MASK;
  2204. if (fw_state != MFI_STATE_FAULT) {
  2205. printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
  2206. fw_state);
  2207. }
  2208. if ((fw_state == MFI_STATE_FAULT) &&
  2209. (instance->disableOnlineCtrlReset == 0)) {
  2210. printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
  2211. if ((instance->pdev->device ==
  2212. PCI_DEVICE_ID_LSI_SAS1064R) ||
  2213. (instance->pdev->device ==
  2214. PCI_DEVICE_ID_DELL_PERC5) ||
  2215. (instance->pdev->device ==
  2216. PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  2217. *instance->consumer =
  2218. MEGASAS_ADPRESET_INPROG_SIGN;
  2219. }
  2220. instance->instancet->disable_intr(instance->reg_set);
  2221. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  2222. instance->issuepend_done = 0;
  2223. atomic_set(&instance->fw_outstanding, 0);
  2224. megasas_internal_reset_defer_cmds(instance);
  2225. printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
  2226. fw_state, instance->adprecovery);
  2227. schedule_work(&instance->work_init);
  2228. return IRQ_HANDLED;
  2229. } else {
  2230. printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
  2231. fw_state, instance->disableOnlineCtrlReset);
  2232. }
  2233. }
  2234. tasklet_schedule(&instance->isr_tasklet);
  2235. return IRQ_HANDLED;
  2236. }
  2237. /**
  2238. * megasas_isr - isr entry point
  2239. */
  2240. static irqreturn_t megasas_isr(int irq, void *devp)
  2241. {
  2242. struct megasas_irq_context *irq_context = devp;
  2243. struct megasas_instance *instance = irq_context->instance;
  2244. unsigned long flags;
  2245. irqreturn_t rc;
  2246. if (atomic_read(&instance->fw_reset_no_pci_access))
  2247. return IRQ_HANDLED;
  2248. spin_lock_irqsave(&instance->hba_lock, flags);
  2249. rc = megasas_deplete_reply_queue(instance, DID_OK);
  2250. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2251. return rc;
  2252. }
  2253. /**
  2254. * megasas_transition_to_ready - Move the FW to READY state
  2255. * @instance: Adapter soft state
  2256. *
  2257. * During the initialization, FW passes can potentially be in any one of
  2258. * several possible states. If the FW in operational, waiting-for-handshake
  2259. * states, driver must take steps to bring it to ready state. Otherwise, it
  2260. * has to wait for the ready state.
  2261. */
  2262. int
  2263. megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
  2264. {
  2265. int i;
  2266. u8 max_wait;
  2267. u32 fw_state;
  2268. u32 cur_state;
  2269. u32 abs_state, curr_abs_state;
  2270. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2271. if (fw_state != MFI_STATE_READY)
  2272. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  2273. " state\n");
  2274. while (fw_state != MFI_STATE_READY) {
  2275. abs_state =
  2276. instance->instancet->read_fw_status_reg(instance->reg_set);
  2277. switch (fw_state) {
  2278. case MFI_STATE_FAULT:
  2279. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  2280. if (ocr) {
  2281. max_wait = MEGASAS_RESET_WAIT_TIME;
  2282. cur_state = MFI_STATE_FAULT;
  2283. break;
  2284. } else
  2285. return -ENODEV;
  2286. case MFI_STATE_WAIT_HANDSHAKE:
  2287. /*
  2288. * Set the CLR bit in inbound doorbell
  2289. */
  2290. if ((instance->pdev->device ==
  2291. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2292. (instance->pdev->device ==
  2293. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2294. (instance->pdev->device ==
  2295. PCI_DEVICE_ID_LSI_FUSION) ||
  2296. (instance->pdev->device ==
  2297. PCI_DEVICE_ID_LSI_INVADER)) {
  2298. writel(
  2299. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2300. &instance->reg_set->doorbell);
  2301. } else {
  2302. writel(
  2303. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2304. &instance->reg_set->inbound_doorbell);
  2305. }
  2306. max_wait = MEGASAS_RESET_WAIT_TIME;
  2307. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  2308. break;
  2309. case MFI_STATE_BOOT_MESSAGE_PENDING:
  2310. if ((instance->pdev->device ==
  2311. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2312. (instance->pdev->device ==
  2313. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2314. (instance->pdev->device ==
  2315. PCI_DEVICE_ID_LSI_FUSION) ||
  2316. (instance->pdev->device ==
  2317. PCI_DEVICE_ID_LSI_INVADER)) {
  2318. writel(MFI_INIT_HOTPLUG,
  2319. &instance->reg_set->doorbell);
  2320. } else
  2321. writel(MFI_INIT_HOTPLUG,
  2322. &instance->reg_set->inbound_doorbell);
  2323. max_wait = MEGASAS_RESET_WAIT_TIME;
  2324. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  2325. break;
  2326. case MFI_STATE_OPERATIONAL:
  2327. /*
  2328. * Bring it to READY state; assuming max wait 10 secs
  2329. */
  2330. instance->instancet->disable_intr(instance->reg_set);
  2331. if ((instance->pdev->device ==
  2332. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2333. (instance->pdev->device ==
  2334. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2335. (instance->pdev->device
  2336. == PCI_DEVICE_ID_LSI_FUSION) ||
  2337. (instance->pdev->device
  2338. == PCI_DEVICE_ID_LSI_INVADER)) {
  2339. writel(MFI_RESET_FLAGS,
  2340. &instance->reg_set->doorbell);
  2341. if ((instance->pdev->device ==
  2342. PCI_DEVICE_ID_LSI_FUSION) ||
  2343. (instance->pdev->device ==
  2344. PCI_DEVICE_ID_LSI_INVADER)) {
  2345. for (i = 0; i < (10 * 1000); i += 20) {
  2346. if (readl(
  2347. &instance->
  2348. reg_set->
  2349. doorbell) & 1)
  2350. msleep(20);
  2351. else
  2352. break;
  2353. }
  2354. }
  2355. } else
  2356. writel(MFI_RESET_FLAGS,
  2357. &instance->reg_set->inbound_doorbell);
  2358. max_wait = MEGASAS_RESET_WAIT_TIME;
  2359. cur_state = MFI_STATE_OPERATIONAL;
  2360. break;
  2361. case MFI_STATE_UNDEFINED:
  2362. /*
  2363. * This state should not last for more than 2 seconds
  2364. */
  2365. max_wait = MEGASAS_RESET_WAIT_TIME;
  2366. cur_state = MFI_STATE_UNDEFINED;
  2367. break;
  2368. case MFI_STATE_BB_INIT:
  2369. max_wait = MEGASAS_RESET_WAIT_TIME;
  2370. cur_state = MFI_STATE_BB_INIT;
  2371. break;
  2372. case MFI_STATE_FW_INIT:
  2373. max_wait = MEGASAS_RESET_WAIT_TIME;
  2374. cur_state = MFI_STATE_FW_INIT;
  2375. break;
  2376. case MFI_STATE_FW_INIT_2:
  2377. max_wait = MEGASAS_RESET_WAIT_TIME;
  2378. cur_state = MFI_STATE_FW_INIT_2;
  2379. break;
  2380. case MFI_STATE_DEVICE_SCAN:
  2381. max_wait = MEGASAS_RESET_WAIT_TIME;
  2382. cur_state = MFI_STATE_DEVICE_SCAN;
  2383. break;
  2384. case MFI_STATE_FLUSH_CACHE:
  2385. max_wait = MEGASAS_RESET_WAIT_TIME;
  2386. cur_state = MFI_STATE_FLUSH_CACHE;
  2387. break;
  2388. default:
  2389. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  2390. fw_state);
  2391. return -ENODEV;
  2392. }
  2393. /*
  2394. * The cur_state should not last for more than max_wait secs
  2395. */
  2396. for (i = 0; i < (max_wait * 1000); i++) {
  2397. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  2398. MFI_STATE_MASK ;
  2399. curr_abs_state =
  2400. instance->instancet->read_fw_status_reg(instance->reg_set);
  2401. if (abs_state == curr_abs_state) {
  2402. msleep(1);
  2403. } else
  2404. break;
  2405. }
  2406. /*
  2407. * Return error if fw_state hasn't changed after max_wait
  2408. */
  2409. if (curr_abs_state == abs_state) {
  2410. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  2411. "in %d secs\n", fw_state, max_wait);
  2412. return -ENODEV;
  2413. }
  2414. }
  2415. printk(KERN_INFO "megasas: FW now in Ready state\n");
  2416. return 0;
  2417. }
  2418. /**
  2419. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  2420. * @instance: Adapter soft state
  2421. */
  2422. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  2423. {
  2424. int i;
  2425. u32 max_cmd = instance->max_mfi_cmds;
  2426. struct megasas_cmd *cmd;
  2427. if (!instance->frame_dma_pool)
  2428. return;
  2429. /*
  2430. * Return all frames to pool
  2431. */
  2432. for (i = 0; i < max_cmd; i++) {
  2433. cmd = instance->cmd_list[i];
  2434. if (cmd->frame)
  2435. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  2436. cmd->frame_phys_addr);
  2437. if (cmd->sense)
  2438. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  2439. cmd->sense_phys_addr);
  2440. }
  2441. /*
  2442. * Now destroy the pool itself
  2443. */
  2444. pci_pool_destroy(instance->frame_dma_pool);
  2445. pci_pool_destroy(instance->sense_dma_pool);
  2446. instance->frame_dma_pool = NULL;
  2447. instance->sense_dma_pool = NULL;
  2448. }
  2449. /**
  2450. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  2451. * @instance: Adapter soft state
  2452. *
  2453. * Each command packet has an embedded DMA memory buffer that is used for
  2454. * filling MFI frame and the SG list that immediately follows the frame. This
  2455. * function creates those DMA memory buffers for each command packet by using
  2456. * PCI pool facility.
  2457. */
  2458. static int megasas_create_frame_pool(struct megasas_instance *instance)
  2459. {
  2460. int i;
  2461. u32 max_cmd;
  2462. u32 sge_sz;
  2463. u32 sgl_sz;
  2464. u32 total_sz;
  2465. u32 frame_count;
  2466. struct megasas_cmd *cmd;
  2467. max_cmd = instance->max_mfi_cmds;
  2468. /*
  2469. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  2470. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  2471. */
  2472. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  2473. sizeof(struct megasas_sge32);
  2474. if (instance->flag_ieee) {
  2475. sge_sz = sizeof(struct megasas_sge_skinny);
  2476. }
  2477. /*
  2478. * Calculated the number of 64byte frames required for SGL
  2479. */
  2480. sgl_sz = sge_sz * instance->max_num_sge;
  2481. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  2482. frame_count = 15;
  2483. /*
  2484. * We need one extra frame for the MFI command
  2485. */
  2486. frame_count++;
  2487. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  2488. /*
  2489. * Use DMA pool facility provided by PCI layer
  2490. */
  2491. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  2492. instance->pdev, total_sz, 64,
  2493. 0);
  2494. if (!instance->frame_dma_pool) {
  2495. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  2496. return -ENOMEM;
  2497. }
  2498. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  2499. instance->pdev, 128, 4, 0);
  2500. if (!instance->sense_dma_pool) {
  2501. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  2502. pci_pool_destroy(instance->frame_dma_pool);
  2503. instance->frame_dma_pool = NULL;
  2504. return -ENOMEM;
  2505. }
  2506. /*
  2507. * Allocate and attach a frame to each of the commands in cmd_list.
  2508. * By making cmd->index as the context instead of the &cmd, we can
  2509. * always use 32bit context regardless of the architecture
  2510. */
  2511. for (i = 0; i < max_cmd; i++) {
  2512. cmd = instance->cmd_list[i];
  2513. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  2514. GFP_KERNEL, &cmd->frame_phys_addr);
  2515. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  2516. GFP_KERNEL, &cmd->sense_phys_addr);
  2517. /*
  2518. * megasas_teardown_frame_pool() takes care of freeing
  2519. * whatever has been allocated
  2520. */
  2521. if (!cmd->frame || !cmd->sense) {
  2522. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  2523. megasas_teardown_frame_pool(instance);
  2524. return -ENOMEM;
  2525. }
  2526. memset(cmd->frame, 0, total_sz);
  2527. cmd->frame->io.context = cmd->index;
  2528. cmd->frame->io.pad_0 = 0;
  2529. if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
  2530. (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
  2531. (reset_devices))
  2532. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  2533. }
  2534. return 0;
  2535. }
  2536. /**
  2537. * megasas_free_cmds - Free all the cmds in the free cmd pool
  2538. * @instance: Adapter soft state
  2539. */
  2540. void megasas_free_cmds(struct megasas_instance *instance)
  2541. {
  2542. int i;
  2543. /* First free the MFI frame pool */
  2544. megasas_teardown_frame_pool(instance);
  2545. /* Free all the commands in the cmd_list */
  2546. for (i = 0; i < instance->max_mfi_cmds; i++)
  2547. kfree(instance->cmd_list[i]);
  2548. /* Free the cmd_list buffer itself */
  2549. kfree(instance->cmd_list);
  2550. instance->cmd_list = NULL;
  2551. INIT_LIST_HEAD(&instance->cmd_pool);
  2552. }
  2553. /**
  2554. * megasas_alloc_cmds - Allocates the command packets
  2555. * @instance: Adapter soft state
  2556. *
  2557. * Each command that is issued to the FW, whether IO commands from the OS or
  2558. * internal commands like IOCTLs, are wrapped in local data structure called
  2559. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  2560. * the FW.
  2561. *
  2562. * Each frame has a 32-bit field called context (tag). This context is used
  2563. * to get back the megasas_cmd from the frame when a frame gets completed in
  2564. * the ISR. Typically the address of the megasas_cmd itself would be used as
  2565. * the context. But we wanted to keep the differences between 32 and 64 bit
  2566. * systems to the mininum. We always use 32 bit integers for the context. In
  2567. * this driver, the 32 bit values are the indices into an array cmd_list.
  2568. * This array is used only to look up the megasas_cmd given the context. The
  2569. * free commands themselves are maintained in a linked list called cmd_pool.
  2570. */
  2571. int megasas_alloc_cmds(struct megasas_instance *instance)
  2572. {
  2573. int i;
  2574. int j;
  2575. u32 max_cmd;
  2576. struct megasas_cmd *cmd;
  2577. max_cmd = instance->max_mfi_cmds;
  2578. /*
  2579. * instance->cmd_list is an array of struct megasas_cmd pointers.
  2580. * Allocate the dynamic array first and then allocate individual
  2581. * commands.
  2582. */
  2583. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  2584. if (!instance->cmd_list) {
  2585. printk(KERN_DEBUG "megasas: out of memory\n");
  2586. return -ENOMEM;
  2587. }
  2588. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
  2589. for (i = 0; i < max_cmd; i++) {
  2590. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  2591. GFP_KERNEL);
  2592. if (!instance->cmd_list[i]) {
  2593. for (j = 0; j < i; j++)
  2594. kfree(instance->cmd_list[j]);
  2595. kfree(instance->cmd_list);
  2596. instance->cmd_list = NULL;
  2597. return -ENOMEM;
  2598. }
  2599. }
  2600. /*
  2601. * Add all the commands to command pool (instance->cmd_pool)
  2602. */
  2603. for (i = 0; i < max_cmd; i++) {
  2604. cmd = instance->cmd_list[i];
  2605. memset(cmd, 0, sizeof(struct megasas_cmd));
  2606. cmd->index = i;
  2607. cmd->scmd = NULL;
  2608. cmd->instance = instance;
  2609. list_add_tail(&cmd->list, &instance->cmd_pool);
  2610. }
  2611. /*
  2612. * Create a frame pool and assign one frame to each cmd
  2613. */
  2614. if (megasas_create_frame_pool(instance)) {
  2615. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  2616. megasas_free_cmds(instance);
  2617. }
  2618. return 0;
  2619. }
  2620. /*
  2621. * megasas_get_pd_list_info - Returns FW's pd_list structure
  2622. * @instance: Adapter soft state
  2623. * @pd_list: pd_list structure
  2624. *
  2625. * Issues an internal command (DCMD) to get the FW's controller PD
  2626. * list structure. This information is mainly used to find out SYSTEM
  2627. * supported by the FW.
  2628. */
  2629. static int
  2630. megasas_get_pd_list(struct megasas_instance *instance)
  2631. {
  2632. int ret = 0, pd_index = 0;
  2633. struct megasas_cmd *cmd;
  2634. struct megasas_dcmd_frame *dcmd;
  2635. struct MR_PD_LIST *ci;
  2636. struct MR_PD_ADDRESS *pd_addr;
  2637. dma_addr_t ci_h = 0;
  2638. cmd = megasas_get_cmd(instance);
  2639. if (!cmd) {
  2640. printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
  2641. return -ENOMEM;
  2642. }
  2643. dcmd = &cmd->frame->dcmd;
  2644. ci = pci_alloc_consistent(instance->pdev,
  2645. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  2646. if (!ci) {
  2647. printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
  2648. megasas_return_cmd(instance, cmd);
  2649. return -ENOMEM;
  2650. }
  2651. memset(ci, 0, sizeof(*ci));
  2652. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2653. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  2654. dcmd->mbox.b[1] = 0;
  2655. dcmd->cmd = MFI_CMD_DCMD;
  2656. dcmd->cmd_status = 0xFF;
  2657. dcmd->sge_count = 1;
  2658. dcmd->flags = MFI_FRAME_DIR_READ;
  2659. dcmd->timeout = 0;
  2660. dcmd->pad_0 = 0;
  2661. dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2662. dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
  2663. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2664. dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2665. if (!megasas_issue_polled(instance, cmd)) {
  2666. ret = 0;
  2667. } else {
  2668. ret = -1;
  2669. }
  2670. /*
  2671. * the following function will get the instance PD LIST.
  2672. */
  2673. pd_addr = ci->addr;
  2674. if ( ret == 0 &&
  2675. (ci->count <
  2676. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
  2677. memset(instance->pd_list, 0,
  2678. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  2679. for (pd_index = 0; pd_index < ci->count; pd_index++) {
  2680. instance->pd_list[pd_addr->deviceId].tid =
  2681. pd_addr->deviceId;
  2682. instance->pd_list[pd_addr->deviceId].driveType =
  2683. pd_addr->scsiDevType;
  2684. instance->pd_list[pd_addr->deviceId].driveState =
  2685. MR_PD_STATE_SYSTEM;
  2686. pd_addr++;
  2687. }
  2688. }
  2689. pci_free_consistent(instance->pdev,
  2690. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  2691. ci, ci_h);
  2692. megasas_return_cmd(instance, cmd);
  2693. return ret;
  2694. }
  2695. /*
  2696. * megasas_get_ld_list_info - Returns FW's ld_list structure
  2697. * @instance: Adapter soft state
  2698. * @ld_list: ld_list structure
  2699. *
  2700. * Issues an internal command (DCMD) to get the FW's controller PD
  2701. * list structure. This information is mainly used to find out SYSTEM
  2702. * supported by the FW.
  2703. */
  2704. static int
  2705. megasas_get_ld_list(struct megasas_instance *instance)
  2706. {
  2707. int ret = 0, ld_index = 0, ids = 0;
  2708. struct megasas_cmd *cmd;
  2709. struct megasas_dcmd_frame *dcmd;
  2710. struct MR_LD_LIST *ci;
  2711. dma_addr_t ci_h = 0;
  2712. cmd = megasas_get_cmd(instance);
  2713. if (!cmd) {
  2714. printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
  2715. return -ENOMEM;
  2716. }
  2717. dcmd = &cmd->frame->dcmd;
  2718. ci = pci_alloc_consistent(instance->pdev,
  2719. sizeof(struct MR_LD_LIST),
  2720. &ci_h);
  2721. if (!ci) {
  2722. printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
  2723. megasas_return_cmd(instance, cmd);
  2724. return -ENOMEM;
  2725. }
  2726. memset(ci, 0, sizeof(*ci));
  2727. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2728. dcmd->cmd = MFI_CMD_DCMD;
  2729. dcmd->cmd_status = 0xFF;
  2730. dcmd->sge_count = 1;
  2731. dcmd->flags = MFI_FRAME_DIR_READ;
  2732. dcmd->timeout = 0;
  2733. dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
  2734. dcmd->opcode = MR_DCMD_LD_GET_LIST;
  2735. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2736. dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
  2737. dcmd->pad_0 = 0;
  2738. if (!megasas_issue_polled(instance, cmd)) {
  2739. ret = 0;
  2740. } else {
  2741. ret = -1;
  2742. }
  2743. /* the following function will get the instance PD LIST */
  2744. if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
  2745. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2746. for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
  2747. if (ci->ldList[ld_index].state != 0) {
  2748. ids = ci->ldList[ld_index].ref.targetId;
  2749. instance->ld_ids[ids] =
  2750. ci->ldList[ld_index].ref.targetId;
  2751. }
  2752. }
  2753. }
  2754. pci_free_consistent(instance->pdev,
  2755. sizeof(struct MR_LD_LIST),
  2756. ci,
  2757. ci_h);
  2758. megasas_return_cmd(instance, cmd);
  2759. return ret;
  2760. }
  2761. /**
  2762. * megasas_get_controller_info - Returns FW's controller structure
  2763. * @instance: Adapter soft state
  2764. * @ctrl_info: Controller information structure
  2765. *
  2766. * Issues an internal command (DCMD) to get the FW's controller structure.
  2767. * This information is mainly used to find out the maximum IO transfer per
  2768. * command supported by the FW.
  2769. */
  2770. static int
  2771. megasas_get_ctrl_info(struct megasas_instance *instance,
  2772. struct megasas_ctrl_info *ctrl_info)
  2773. {
  2774. int ret = 0;
  2775. struct megasas_cmd *cmd;
  2776. struct megasas_dcmd_frame *dcmd;
  2777. struct megasas_ctrl_info *ci;
  2778. dma_addr_t ci_h = 0;
  2779. cmd = megasas_get_cmd(instance);
  2780. if (!cmd) {
  2781. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  2782. return -ENOMEM;
  2783. }
  2784. dcmd = &cmd->frame->dcmd;
  2785. ci = pci_alloc_consistent(instance->pdev,
  2786. sizeof(struct megasas_ctrl_info), &ci_h);
  2787. if (!ci) {
  2788. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  2789. megasas_return_cmd(instance, cmd);
  2790. return -ENOMEM;
  2791. }
  2792. memset(ci, 0, sizeof(*ci));
  2793. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2794. dcmd->cmd = MFI_CMD_DCMD;
  2795. dcmd->cmd_status = 0xFF;
  2796. dcmd->sge_count = 1;
  2797. dcmd->flags = MFI_FRAME_DIR_READ;
  2798. dcmd->timeout = 0;
  2799. dcmd->pad_0 = 0;
  2800. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  2801. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  2802. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2803. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  2804. if (!megasas_issue_polled(instance, cmd)) {
  2805. ret = 0;
  2806. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  2807. } else {
  2808. ret = -1;
  2809. }
  2810. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  2811. ci, ci_h);
  2812. megasas_return_cmd(instance, cmd);
  2813. return ret;
  2814. }
  2815. /**
  2816. * megasas_issue_init_mfi - Initializes the FW
  2817. * @instance: Adapter soft state
  2818. *
  2819. * Issues the INIT MFI cmd
  2820. */
  2821. static int
  2822. megasas_issue_init_mfi(struct megasas_instance *instance)
  2823. {
  2824. u32 context;
  2825. struct megasas_cmd *cmd;
  2826. struct megasas_init_frame *init_frame;
  2827. struct megasas_init_queue_info *initq_info;
  2828. dma_addr_t init_frame_h;
  2829. dma_addr_t initq_info_h;
  2830. /*
  2831. * Prepare a init frame. Note the init frame points to queue info
  2832. * structure. Each frame has SGL allocated after first 64 bytes. For
  2833. * this frame - since we don't need any SGL - we use SGL's space as
  2834. * queue info structure
  2835. *
  2836. * We will not get a NULL command below. We just created the pool.
  2837. */
  2838. cmd = megasas_get_cmd(instance);
  2839. init_frame = (struct megasas_init_frame *)cmd->frame;
  2840. initq_info = (struct megasas_init_queue_info *)
  2841. ((unsigned long)init_frame + 64);
  2842. init_frame_h = cmd->frame_phys_addr;
  2843. initq_info_h = init_frame_h + 64;
  2844. context = init_frame->context;
  2845. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  2846. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  2847. init_frame->context = context;
  2848. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  2849. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  2850. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  2851. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  2852. init_frame->cmd = MFI_CMD_INIT;
  2853. init_frame->cmd_status = 0xFF;
  2854. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  2855. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  2856. /*
  2857. * disable the intr before firing the init frame to FW
  2858. */
  2859. instance->instancet->disable_intr(instance->reg_set);
  2860. /*
  2861. * Issue the init frame in polled mode
  2862. */
  2863. if (megasas_issue_polled(instance, cmd)) {
  2864. printk(KERN_ERR "megasas: Failed to init firmware\n");
  2865. megasas_return_cmd(instance, cmd);
  2866. goto fail_fw_init;
  2867. }
  2868. megasas_return_cmd(instance, cmd);
  2869. return 0;
  2870. fail_fw_init:
  2871. return -EINVAL;
  2872. }
  2873. static u32
  2874. megasas_init_adapter_mfi(struct megasas_instance *instance)
  2875. {
  2876. struct megasas_register_set __iomem *reg_set;
  2877. u32 context_sz;
  2878. u32 reply_q_sz;
  2879. reg_set = instance->reg_set;
  2880. /*
  2881. * Get various operational parameters from status register
  2882. */
  2883. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  2884. /*
  2885. * Reduce the max supported cmds by 1. This is to ensure that the
  2886. * reply_q_sz (1 more than the max cmd that driver may send)
  2887. * does not exceed max cmds that the FW can support
  2888. */
  2889. instance->max_fw_cmds = instance->max_fw_cmds-1;
  2890. instance->max_mfi_cmds = instance->max_fw_cmds;
  2891. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  2892. 0x10;
  2893. /*
  2894. * Create a pool of commands
  2895. */
  2896. if (megasas_alloc_cmds(instance))
  2897. goto fail_alloc_cmds;
  2898. /*
  2899. * Allocate memory for reply queue. Length of reply queue should
  2900. * be _one_ more than the maximum commands handled by the firmware.
  2901. *
  2902. * Note: When FW completes commands, it places corresponding contex
  2903. * values in this circular reply queue. This circular queue is a fairly
  2904. * typical producer-consumer queue. FW is the producer (of completed
  2905. * commands) and the driver is the consumer.
  2906. */
  2907. context_sz = sizeof(u32);
  2908. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  2909. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  2910. reply_q_sz,
  2911. &instance->reply_queue_h);
  2912. if (!instance->reply_queue) {
  2913. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  2914. goto fail_reply_queue;
  2915. }
  2916. if (megasas_issue_init_mfi(instance))
  2917. goto fail_fw_init;
  2918. instance->fw_support_ieee = 0;
  2919. instance->fw_support_ieee =
  2920. (instance->instancet->read_fw_status_reg(reg_set) &
  2921. 0x04000000);
  2922. printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
  2923. instance->fw_support_ieee);
  2924. if (instance->fw_support_ieee)
  2925. instance->flag_ieee = 1;
  2926. return 0;
  2927. fail_fw_init:
  2928. pci_free_consistent(instance->pdev, reply_q_sz,
  2929. instance->reply_queue, instance->reply_queue_h);
  2930. fail_reply_queue:
  2931. megasas_free_cmds(instance);
  2932. fail_alloc_cmds:
  2933. return 1;
  2934. }
  2935. /**
  2936. * megasas_init_fw - Initializes the FW
  2937. * @instance: Adapter soft state
  2938. *
  2939. * This is the main function for initializing firmware
  2940. */
  2941. static int megasas_init_fw(struct megasas_instance *instance)
  2942. {
  2943. u32 max_sectors_1;
  2944. u32 max_sectors_2;
  2945. u32 tmp_sectors, msix_enable;
  2946. struct megasas_register_set __iomem *reg_set;
  2947. struct megasas_ctrl_info *ctrl_info;
  2948. unsigned long bar_list;
  2949. int i;
  2950. /* Find first memory bar */
  2951. bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
  2952. instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
  2953. instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
  2954. if (pci_request_selected_regions(instance->pdev, instance->bar,
  2955. "megasas: LSI")) {
  2956. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  2957. return -EBUSY;
  2958. }
  2959. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  2960. if (!instance->reg_set) {
  2961. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  2962. goto fail_ioremap;
  2963. }
  2964. reg_set = instance->reg_set;
  2965. switch (instance->pdev->device) {
  2966. case PCI_DEVICE_ID_LSI_FUSION:
  2967. case PCI_DEVICE_ID_LSI_INVADER:
  2968. instance->instancet = &megasas_instance_template_fusion;
  2969. break;
  2970. case PCI_DEVICE_ID_LSI_SAS1078R:
  2971. case PCI_DEVICE_ID_LSI_SAS1078DE:
  2972. instance->instancet = &megasas_instance_template_ppc;
  2973. break;
  2974. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  2975. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  2976. instance->instancet = &megasas_instance_template_gen2;
  2977. break;
  2978. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  2979. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  2980. instance->instancet = &megasas_instance_template_skinny;
  2981. break;
  2982. case PCI_DEVICE_ID_LSI_SAS1064R:
  2983. case PCI_DEVICE_ID_DELL_PERC5:
  2984. default:
  2985. instance->instancet = &megasas_instance_template_xscale;
  2986. break;
  2987. }
  2988. /*
  2989. * We expect the FW state to be READY
  2990. */
  2991. if (megasas_transition_to_ready(instance, 0))
  2992. goto fail_ready_state;
  2993. /* Check if MSI-X is supported while in ready state */
  2994. msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
  2995. 0x4000000) >> 0x1a;
  2996. if (msix_enable && !msix_disable) {
  2997. /* Check max MSI-X vectors */
  2998. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  2999. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
  3000. instance->msix_vectors = (readl(&instance->reg_set->
  3001. outbound_scratch_pad_2
  3002. ) & 0x1F) + 1;
  3003. if (msix_vectors)
  3004. instance->msix_vectors =
  3005. min(msix_vectors,
  3006. instance->msix_vectors);
  3007. } else
  3008. instance->msix_vectors = 1;
  3009. /* Don't bother allocating more MSI-X vectors than cpus */
  3010. instance->msix_vectors = min(instance->msix_vectors,
  3011. (unsigned int)num_online_cpus());
  3012. for (i = 0; i < instance->msix_vectors; i++)
  3013. instance->msixentry[i].entry = i;
  3014. i = pci_enable_msix(instance->pdev, instance->msixentry,
  3015. instance->msix_vectors);
  3016. if (i >= 0) {
  3017. if (i) {
  3018. if (!pci_enable_msix(instance->pdev,
  3019. instance->msixentry, i))
  3020. instance->msix_vectors = i;
  3021. else
  3022. instance->msix_vectors = 0;
  3023. }
  3024. } else
  3025. instance->msix_vectors = 0;
  3026. }
  3027. /* Get operational params, sge flags, send init cmd to controller */
  3028. if (instance->instancet->init_adapter(instance))
  3029. goto fail_init_adapter;
  3030. printk(KERN_ERR "megasas: INIT adapter done\n");
  3031. /** for passthrough
  3032. * the following function will get the PD LIST.
  3033. */
  3034. memset(instance->pd_list, 0 ,
  3035. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  3036. megasas_get_pd_list(instance);
  3037. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  3038. megasas_get_ld_list(instance);
  3039. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  3040. /*
  3041. * Compute the max allowed sectors per IO: The controller info has two
  3042. * limits on max sectors. Driver should use the minimum of these two.
  3043. *
  3044. * 1 << stripe_sz_ops.min = max sectors per strip
  3045. *
  3046. * Note that older firmwares ( < FW ver 30) didn't report information
  3047. * to calculate max_sectors_1. So the number ended up as zero always.
  3048. */
  3049. tmp_sectors = 0;
  3050. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  3051. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  3052. ctrl_info->max_strips_per_io;
  3053. max_sectors_2 = ctrl_info->max_request_size;
  3054. tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
  3055. instance->disableOnlineCtrlReset =
  3056. ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
  3057. }
  3058. instance->max_sectors_per_req = instance->max_num_sge *
  3059. PAGE_SIZE / 512;
  3060. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  3061. instance->max_sectors_per_req = tmp_sectors;
  3062. kfree(ctrl_info);
  3063. /* Check for valid throttlequeuedepth module parameter */
  3064. if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
  3065. instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
  3066. if (throttlequeuedepth > (instance->max_fw_cmds -
  3067. MEGASAS_SKINNY_INT_CMDS))
  3068. instance->throttlequeuedepth =
  3069. MEGASAS_THROTTLE_QUEUE_DEPTH;
  3070. else
  3071. instance->throttlequeuedepth = throttlequeuedepth;
  3072. } else {
  3073. if (throttlequeuedepth > (instance->max_fw_cmds -
  3074. MEGASAS_INT_CMDS))
  3075. instance->throttlequeuedepth =
  3076. MEGASAS_THROTTLE_QUEUE_DEPTH;
  3077. else
  3078. instance->throttlequeuedepth = throttlequeuedepth;
  3079. }
  3080. /*
  3081. * Setup tasklet for cmd completion
  3082. */
  3083. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3084. (unsigned long)instance);
  3085. return 0;
  3086. fail_init_adapter:
  3087. fail_ready_state:
  3088. iounmap(instance->reg_set);
  3089. fail_ioremap:
  3090. pci_release_selected_regions(instance->pdev, instance->bar);
  3091. return -EINVAL;
  3092. }
  3093. /**
  3094. * megasas_release_mfi - Reverses the FW initialization
  3095. * @intance: Adapter soft state
  3096. */
  3097. static void megasas_release_mfi(struct megasas_instance *instance)
  3098. {
  3099. u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
  3100. if (instance->reply_queue)
  3101. pci_free_consistent(instance->pdev, reply_q_sz,
  3102. instance->reply_queue, instance->reply_queue_h);
  3103. megasas_free_cmds(instance);
  3104. iounmap(instance->reg_set);
  3105. pci_release_selected_regions(instance->pdev, instance->bar);
  3106. }
  3107. /**
  3108. * megasas_get_seq_num - Gets latest event sequence numbers
  3109. * @instance: Adapter soft state
  3110. * @eli: FW event log sequence numbers information
  3111. *
  3112. * FW maintains a log of all events in a non-volatile area. Upper layers would
  3113. * usually find out the latest sequence number of the events, the seq number at
  3114. * the boot etc. They would "read" all the events below the latest seq number
  3115. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  3116. * number), they would subsribe to AEN (asynchronous event notification) and
  3117. * wait for the events to happen.
  3118. */
  3119. static int
  3120. megasas_get_seq_num(struct megasas_instance *instance,
  3121. struct megasas_evt_log_info *eli)
  3122. {
  3123. struct megasas_cmd *cmd;
  3124. struct megasas_dcmd_frame *dcmd;
  3125. struct megasas_evt_log_info *el_info;
  3126. dma_addr_t el_info_h = 0;
  3127. cmd = megasas_get_cmd(instance);
  3128. if (!cmd) {
  3129. return -ENOMEM;
  3130. }
  3131. dcmd = &cmd->frame->dcmd;
  3132. el_info = pci_alloc_consistent(instance->pdev,
  3133. sizeof(struct megasas_evt_log_info),
  3134. &el_info_h);
  3135. if (!el_info) {
  3136. megasas_return_cmd(instance, cmd);
  3137. return -ENOMEM;
  3138. }
  3139. memset(el_info, 0, sizeof(*el_info));
  3140. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3141. dcmd->cmd = MFI_CMD_DCMD;
  3142. dcmd->cmd_status = 0x0;
  3143. dcmd->sge_count = 1;
  3144. dcmd->flags = MFI_FRAME_DIR_READ;
  3145. dcmd->timeout = 0;
  3146. dcmd->pad_0 = 0;
  3147. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  3148. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  3149. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  3150. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  3151. megasas_issue_blocked_cmd(instance, cmd);
  3152. /*
  3153. * Copy the data back into callers buffer
  3154. */
  3155. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  3156. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  3157. el_info, el_info_h);
  3158. megasas_return_cmd(instance, cmd);
  3159. return 0;
  3160. }
  3161. /**
  3162. * megasas_register_aen - Registers for asynchronous event notification
  3163. * @instance: Adapter soft state
  3164. * @seq_num: The starting sequence number
  3165. * @class_locale: Class of the event
  3166. *
  3167. * This function subscribes for AEN for events beyond the @seq_num. It requests
  3168. * to be notified if and only if the event is of type @class_locale
  3169. */
  3170. static int
  3171. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  3172. u32 class_locale_word)
  3173. {
  3174. int ret_val;
  3175. struct megasas_cmd *cmd;
  3176. struct megasas_dcmd_frame *dcmd;
  3177. union megasas_evt_class_locale curr_aen;
  3178. union megasas_evt_class_locale prev_aen;
  3179. /*
  3180. * If there an AEN pending already (aen_cmd), check if the
  3181. * class_locale of that pending AEN is inclusive of the new
  3182. * AEN request we currently have. If it is, then we don't have
  3183. * to do anything. In other words, whichever events the current
  3184. * AEN request is subscribing to, have already been subscribed
  3185. * to.
  3186. *
  3187. * If the old_cmd is _not_ inclusive, then we have to abort
  3188. * that command, form a class_locale that is superset of both
  3189. * old and current and re-issue to the FW
  3190. */
  3191. curr_aen.word = class_locale_word;
  3192. if (instance->aen_cmd) {
  3193. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  3194. /*
  3195. * A class whose enum value is smaller is inclusive of all
  3196. * higher values. If a PROGRESS (= -1) was previously
  3197. * registered, then a new registration requests for higher
  3198. * classes need not be sent to FW. They are automatically
  3199. * included.
  3200. *
  3201. * Locale numbers don't have such hierarchy. They are bitmap
  3202. * values
  3203. */
  3204. if ((prev_aen.members.class <= curr_aen.members.class) &&
  3205. !((prev_aen.members.locale & curr_aen.members.locale) ^
  3206. curr_aen.members.locale)) {
  3207. /*
  3208. * Previously issued event registration includes
  3209. * current request. Nothing to do.
  3210. */
  3211. return 0;
  3212. } else {
  3213. curr_aen.members.locale |= prev_aen.members.locale;
  3214. if (prev_aen.members.class < curr_aen.members.class)
  3215. curr_aen.members.class = prev_aen.members.class;
  3216. instance->aen_cmd->abort_aen = 1;
  3217. ret_val = megasas_issue_blocked_abort_cmd(instance,
  3218. instance->
  3219. aen_cmd);
  3220. if (ret_val) {
  3221. printk(KERN_DEBUG "megasas: Failed to abort "
  3222. "previous AEN command\n");
  3223. return ret_val;
  3224. }
  3225. }
  3226. }
  3227. cmd = megasas_get_cmd(instance);
  3228. if (!cmd)
  3229. return -ENOMEM;
  3230. dcmd = &cmd->frame->dcmd;
  3231. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  3232. /*
  3233. * Prepare DCMD for aen registration
  3234. */
  3235. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3236. dcmd->cmd = MFI_CMD_DCMD;
  3237. dcmd->cmd_status = 0x0;
  3238. dcmd->sge_count = 1;
  3239. dcmd->flags = MFI_FRAME_DIR_READ;
  3240. dcmd->timeout = 0;
  3241. dcmd->pad_0 = 0;
  3242. instance->last_seq_num = seq_num;
  3243. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  3244. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  3245. dcmd->mbox.w[0] = seq_num;
  3246. dcmd->mbox.w[1] = curr_aen.word;
  3247. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  3248. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  3249. if (instance->aen_cmd != NULL) {
  3250. megasas_return_cmd(instance, cmd);
  3251. return 0;
  3252. }
  3253. /*
  3254. * Store reference to the cmd used to register for AEN. When an
  3255. * application wants us to register for AEN, we have to abort this
  3256. * cmd and re-register with a new EVENT LOCALE supplied by that app
  3257. */
  3258. instance->aen_cmd = cmd;
  3259. /*
  3260. * Issue the aen registration frame
  3261. */
  3262. instance->instancet->issue_dcmd(instance, cmd);
  3263. return 0;
  3264. }
  3265. /**
  3266. * megasas_start_aen - Subscribes to AEN during driver load time
  3267. * @instance: Adapter soft state
  3268. */
  3269. static int megasas_start_aen(struct megasas_instance *instance)
  3270. {
  3271. struct megasas_evt_log_info eli;
  3272. union megasas_evt_class_locale class_locale;
  3273. /*
  3274. * Get the latest sequence number from FW
  3275. */
  3276. memset(&eli, 0, sizeof(eli));
  3277. if (megasas_get_seq_num(instance, &eli))
  3278. return -1;
  3279. /*
  3280. * Register AEN with FW for latest sequence number plus 1
  3281. */
  3282. class_locale.members.reserved = 0;
  3283. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3284. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3285. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  3286. class_locale.word);
  3287. }
  3288. /**
  3289. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  3290. * @instance: Adapter soft state
  3291. */
  3292. static int megasas_io_attach(struct megasas_instance *instance)
  3293. {
  3294. struct Scsi_Host *host = instance->host;
  3295. /*
  3296. * Export parameters required by SCSI mid-layer
  3297. */
  3298. host->irq = instance->pdev->irq;
  3299. host->unique_id = instance->unique_id;
  3300. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3301. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3302. host->can_queue =
  3303. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  3304. } else
  3305. host->can_queue =
  3306. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  3307. host->this_id = instance->init_id;
  3308. host->sg_tablesize = instance->max_num_sge;
  3309. if (instance->fw_support_ieee)
  3310. instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
  3311. /*
  3312. * Check if the module parameter value for max_sectors can be used
  3313. */
  3314. if (max_sectors && max_sectors < instance->max_sectors_per_req)
  3315. instance->max_sectors_per_req = max_sectors;
  3316. else {
  3317. if (max_sectors) {
  3318. if (((instance->pdev->device ==
  3319. PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  3320. (instance->pdev->device ==
  3321. PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
  3322. (max_sectors <= MEGASAS_MAX_SECTORS)) {
  3323. instance->max_sectors_per_req = max_sectors;
  3324. } else {
  3325. printk(KERN_INFO "megasas: max_sectors should be > 0"
  3326. "and <= %d (or < 1MB for GEN2 controller)\n",
  3327. instance->max_sectors_per_req);
  3328. }
  3329. }
  3330. }
  3331. host->max_sectors = instance->max_sectors_per_req;
  3332. host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
  3333. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  3334. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  3335. host->max_lun = MEGASAS_MAX_LUN;
  3336. host->max_cmd_len = 16;
  3337. /* Fusion only supports host reset */
  3338. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  3339. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
  3340. host->hostt->eh_device_reset_handler = NULL;
  3341. host->hostt->eh_bus_reset_handler = NULL;
  3342. }
  3343. /*
  3344. * Notify the mid-layer about the new controller
  3345. */
  3346. if (scsi_add_host(host, &instance->pdev->dev)) {
  3347. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  3348. return -ENODEV;
  3349. }
  3350. /*
  3351. * Trigger SCSI to scan our drives
  3352. */
  3353. scsi_scan_host(host);
  3354. return 0;
  3355. }
  3356. static int
  3357. megasas_set_dma_mask(struct pci_dev *pdev)
  3358. {
  3359. /*
  3360. * All our contollers are capable of performing 64-bit DMA
  3361. */
  3362. if (IS_DMA64) {
  3363. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  3364. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3365. goto fail_set_dma_mask;
  3366. }
  3367. } else {
  3368. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3369. goto fail_set_dma_mask;
  3370. }
  3371. return 0;
  3372. fail_set_dma_mask:
  3373. return 1;
  3374. }
  3375. /**
  3376. * megasas_probe_one - PCI hotplug entry point
  3377. * @pdev: PCI device structure
  3378. * @id: PCI ids of supported hotplugged adapter
  3379. */
  3380. static int megasas_probe_one(struct pci_dev *pdev,
  3381. const struct pci_device_id *id)
  3382. {
  3383. int rval, pos, i, j;
  3384. struct Scsi_Host *host;
  3385. struct megasas_instance *instance;
  3386. u16 control = 0;
  3387. /* Reset MSI-X in the kdump kernel */
  3388. if (reset_devices) {
  3389. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3390. if (pos) {
  3391. pci_read_config_word(pdev, msi_control_reg(pos),
  3392. &control);
  3393. if (control & PCI_MSIX_FLAGS_ENABLE) {
  3394. dev_info(&pdev->dev, "resetting MSI-X\n");
  3395. pci_write_config_word(pdev,
  3396. msi_control_reg(pos),
  3397. control &
  3398. ~PCI_MSIX_FLAGS_ENABLE);
  3399. }
  3400. }
  3401. }
  3402. /*
  3403. * Announce PCI information
  3404. */
  3405. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  3406. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  3407. pdev->subsystem_device);
  3408. printk("bus %d:slot %d:func %d\n",
  3409. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  3410. /*
  3411. * PCI prepping: enable device set bus mastering and dma mask
  3412. */
  3413. rval = pci_enable_device_mem(pdev);
  3414. if (rval) {
  3415. return rval;
  3416. }
  3417. pci_set_master(pdev);
  3418. if (megasas_set_dma_mask(pdev))
  3419. goto fail_set_dma_mask;
  3420. host = scsi_host_alloc(&megasas_template,
  3421. sizeof(struct megasas_instance));
  3422. if (!host) {
  3423. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  3424. goto fail_alloc_instance;
  3425. }
  3426. instance = (struct megasas_instance *)host->hostdata;
  3427. memset(instance, 0, sizeof(*instance));
  3428. atomic_set( &instance->fw_reset_no_pci_access, 0 );
  3429. instance->pdev = pdev;
  3430. switch (instance->pdev->device) {
  3431. case PCI_DEVICE_ID_LSI_FUSION:
  3432. case PCI_DEVICE_ID_LSI_INVADER:
  3433. {
  3434. struct fusion_context *fusion;
  3435. instance->ctrl_context =
  3436. kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
  3437. if (!instance->ctrl_context) {
  3438. printk(KERN_DEBUG "megasas: Failed to allocate "
  3439. "memory for Fusion context info\n");
  3440. goto fail_alloc_dma_buf;
  3441. }
  3442. fusion = instance->ctrl_context;
  3443. INIT_LIST_HEAD(&fusion->cmd_pool);
  3444. spin_lock_init(&fusion->cmd_pool_lock);
  3445. }
  3446. break;
  3447. default: /* For all other supported controllers */
  3448. instance->producer =
  3449. pci_alloc_consistent(pdev, sizeof(u32),
  3450. &instance->producer_h);
  3451. instance->consumer =
  3452. pci_alloc_consistent(pdev, sizeof(u32),
  3453. &instance->consumer_h);
  3454. if (!instance->producer || !instance->consumer) {
  3455. printk(KERN_DEBUG "megasas: Failed to allocate"
  3456. "memory for producer, consumer\n");
  3457. goto fail_alloc_dma_buf;
  3458. }
  3459. *instance->producer = 0;
  3460. *instance->consumer = 0;
  3461. break;
  3462. }
  3463. megasas_poll_wait_aen = 0;
  3464. instance->flag_ieee = 0;
  3465. instance->ev = NULL;
  3466. instance->issuepend_done = 1;
  3467. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  3468. megasas_poll_wait_aen = 0;
  3469. instance->evt_detail = pci_alloc_consistent(pdev,
  3470. sizeof(struct
  3471. megasas_evt_detail),
  3472. &instance->evt_detail_h);
  3473. if (!instance->evt_detail) {
  3474. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  3475. "event detail structure\n");
  3476. goto fail_alloc_dma_buf;
  3477. }
  3478. /*
  3479. * Initialize locks and queues
  3480. */
  3481. INIT_LIST_HEAD(&instance->cmd_pool);
  3482. INIT_LIST_HEAD(&instance->internal_reset_pending_q);
  3483. atomic_set(&instance->fw_outstanding,0);
  3484. init_waitqueue_head(&instance->int_cmd_wait_q);
  3485. init_waitqueue_head(&instance->abort_cmd_wait_q);
  3486. spin_lock_init(&instance->cmd_pool_lock);
  3487. spin_lock_init(&instance->hba_lock);
  3488. spin_lock_init(&instance->completion_lock);
  3489. mutex_init(&instance->aen_mutex);
  3490. mutex_init(&instance->reset_mutex);
  3491. /*
  3492. * Initialize PCI related and misc parameters
  3493. */
  3494. instance->host = host;
  3495. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  3496. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  3497. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3498. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3499. instance->flag_ieee = 1;
  3500. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  3501. } else
  3502. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  3503. megasas_dbg_lvl = 0;
  3504. instance->flag = 0;
  3505. instance->unload = 1;
  3506. instance->last_time = 0;
  3507. instance->disableOnlineCtrlReset = 1;
  3508. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  3509. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
  3510. INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
  3511. else
  3512. INIT_WORK(&instance->work_init, process_fw_state_change_wq);
  3513. /*
  3514. * Initialize MFI Firmware
  3515. */
  3516. if (megasas_init_fw(instance))
  3517. goto fail_init_mfi;
  3518. /*
  3519. * Register IRQ
  3520. */
  3521. if (instance->msix_vectors) {
  3522. for (i = 0 ; i < instance->msix_vectors; i++) {
  3523. instance->irq_context[i].instance = instance;
  3524. instance->irq_context[i].MSIxIndex = i;
  3525. if (request_irq(instance->msixentry[i].vector,
  3526. instance->instancet->service_isr, 0,
  3527. "megasas",
  3528. &instance->irq_context[i])) {
  3529. printk(KERN_DEBUG "megasas: Failed to "
  3530. "register IRQ for vector %d.\n", i);
  3531. for (j = 0 ; j < i ; j++)
  3532. free_irq(
  3533. instance->msixentry[j].vector,
  3534. &instance->irq_context[j]);
  3535. goto fail_irq;
  3536. }
  3537. }
  3538. } else {
  3539. instance->irq_context[0].instance = instance;
  3540. instance->irq_context[0].MSIxIndex = 0;
  3541. if (request_irq(pdev->irq, instance->instancet->service_isr,
  3542. IRQF_SHARED, "megasas",
  3543. &instance->irq_context[0])) {
  3544. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  3545. goto fail_irq;
  3546. }
  3547. }
  3548. instance->instancet->enable_intr(instance->reg_set);
  3549. /*
  3550. * Store instance in PCI softstate
  3551. */
  3552. pci_set_drvdata(pdev, instance);
  3553. /*
  3554. * Add this controller to megasas_mgmt_info structure so that it
  3555. * can be exported to management applications
  3556. */
  3557. megasas_mgmt_info.count++;
  3558. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  3559. megasas_mgmt_info.max_index++;
  3560. /*
  3561. * Register with SCSI mid-layer
  3562. */
  3563. if (megasas_io_attach(instance))
  3564. goto fail_io_attach;
  3565. instance->unload = 0;
  3566. /*
  3567. * Initiate AEN (Asynchronous Event Notification)
  3568. */
  3569. if (megasas_start_aen(instance)) {
  3570. printk(KERN_DEBUG "megasas: start aen failed\n");
  3571. goto fail_start_aen;
  3572. }
  3573. return 0;
  3574. fail_start_aen:
  3575. fail_io_attach:
  3576. megasas_mgmt_info.count--;
  3577. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  3578. megasas_mgmt_info.max_index--;
  3579. pci_set_drvdata(pdev, NULL);
  3580. instance->instancet->disable_intr(instance->reg_set);
  3581. if (instance->msix_vectors)
  3582. for (i = 0 ; i < instance->msix_vectors; i++)
  3583. free_irq(instance->msixentry[i].vector,
  3584. &instance->irq_context[i]);
  3585. else
  3586. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3587. fail_irq:
  3588. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  3589. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
  3590. megasas_release_fusion(instance);
  3591. else
  3592. megasas_release_mfi(instance);
  3593. fail_init_mfi:
  3594. if (instance->msix_vectors)
  3595. pci_disable_msix(instance->pdev);
  3596. fail_alloc_dma_buf:
  3597. if (instance->evt_detail)
  3598. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3599. instance->evt_detail,
  3600. instance->evt_detail_h);
  3601. if (instance->producer)
  3602. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3603. instance->producer_h);
  3604. if (instance->consumer)
  3605. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3606. instance->consumer_h);
  3607. scsi_host_put(host);
  3608. fail_alloc_instance:
  3609. fail_set_dma_mask:
  3610. pci_disable_device(pdev);
  3611. return -ENODEV;
  3612. }
  3613. /**
  3614. * megasas_flush_cache - Requests FW to flush all its caches
  3615. * @instance: Adapter soft state
  3616. */
  3617. static void megasas_flush_cache(struct megasas_instance *instance)
  3618. {
  3619. struct megasas_cmd *cmd;
  3620. struct megasas_dcmd_frame *dcmd;
  3621. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3622. return;
  3623. cmd = megasas_get_cmd(instance);
  3624. if (!cmd)
  3625. return;
  3626. dcmd = &cmd->frame->dcmd;
  3627. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3628. dcmd->cmd = MFI_CMD_DCMD;
  3629. dcmd->cmd_status = 0x0;
  3630. dcmd->sge_count = 0;
  3631. dcmd->flags = MFI_FRAME_DIR_NONE;
  3632. dcmd->timeout = 0;
  3633. dcmd->pad_0 = 0;
  3634. dcmd->data_xfer_len = 0;
  3635. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  3636. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  3637. megasas_issue_blocked_cmd(instance, cmd);
  3638. megasas_return_cmd(instance, cmd);
  3639. return;
  3640. }
  3641. /**
  3642. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  3643. * @instance: Adapter soft state
  3644. * @opcode: Shutdown/Hibernate
  3645. */
  3646. static void megasas_shutdown_controller(struct megasas_instance *instance,
  3647. u32 opcode)
  3648. {
  3649. struct megasas_cmd *cmd;
  3650. struct megasas_dcmd_frame *dcmd;
  3651. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3652. return;
  3653. cmd = megasas_get_cmd(instance);
  3654. if (!cmd)
  3655. return;
  3656. if (instance->aen_cmd)
  3657. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  3658. if (instance->map_update_cmd)
  3659. megasas_issue_blocked_abort_cmd(instance,
  3660. instance->map_update_cmd);
  3661. dcmd = &cmd->frame->dcmd;
  3662. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3663. dcmd->cmd = MFI_CMD_DCMD;
  3664. dcmd->cmd_status = 0x0;
  3665. dcmd->sge_count = 0;
  3666. dcmd->flags = MFI_FRAME_DIR_NONE;
  3667. dcmd->timeout = 0;
  3668. dcmd->pad_0 = 0;
  3669. dcmd->data_xfer_len = 0;
  3670. dcmd->opcode = opcode;
  3671. megasas_issue_blocked_cmd(instance, cmd);
  3672. megasas_return_cmd(instance, cmd);
  3673. return;
  3674. }
  3675. #ifdef CONFIG_PM
  3676. /**
  3677. * megasas_suspend - driver suspend entry point
  3678. * @pdev: PCI device structure
  3679. * @state: PCI power state to suspend routine
  3680. */
  3681. static int
  3682. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  3683. {
  3684. struct Scsi_Host *host;
  3685. struct megasas_instance *instance;
  3686. int i;
  3687. instance = pci_get_drvdata(pdev);
  3688. host = instance->host;
  3689. instance->unload = 1;
  3690. megasas_flush_cache(instance);
  3691. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  3692. /* cancel the delayed work if this work still in queue */
  3693. if (instance->ev != NULL) {
  3694. struct megasas_aen_event *ev = instance->ev;
  3695. cancel_delayed_work_sync(&ev->hotplug_work);
  3696. instance->ev = NULL;
  3697. }
  3698. tasklet_kill(&instance->isr_tasklet);
  3699. pci_set_drvdata(instance->pdev, instance);
  3700. instance->instancet->disable_intr(instance->reg_set);
  3701. if (instance->msix_vectors)
  3702. for (i = 0 ; i < instance->msix_vectors; i++)
  3703. free_irq(instance->msixentry[i].vector,
  3704. &instance->irq_context[i]);
  3705. else
  3706. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3707. if (instance->msix_vectors)
  3708. pci_disable_msix(instance->pdev);
  3709. pci_save_state(pdev);
  3710. pci_disable_device(pdev);
  3711. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  3712. return 0;
  3713. }
  3714. /**
  3715. * megasas_resume- driver resume entry point
  3716. * @pdev: PCI device structure
  3717. */
  3718. static int
  3719. megasas_resume(struct pci_dev *pdev)
  3720. {
  3721. int rval, i, j;
  3722. struct Scsi_Host *host;
  3723. struct megasas_instance *instance;
  3724. instance = pci_get_drvdata(pdev);
  3725. host = instance->host;
  3726. pci_set_power_state(pdev, PCI_D0);
  3727. pci_enable_wake(pdev, PCI_D0, 0);
  3728. pci_restore_state(pdev);
  3729. /*
  3730. * PCI prepping: enable device set bus mastering and dma mask
  3731. */
  3732. rval = pci_enable_device_mem(pdev);
  3733. if (rval) {
  3734. printk(KERN_ERR "megasas: Enable device failed\n");
  3735. return rval;
  3736. }
  3737. pci_set_master(pdev);
  3738. if (megasas_set_dma_mask(pdev))
  3739. goto fail_set_dma_mask;
  3740. /*
  3741. * Initialize MFI Firmware
  3742. */
  3743. atomic_set(&instance->fw_outstanding, 0);
  3744. /*
  3745. * We expect the FW state to be READY
  3746. */
  3747. if (megasas_transition_to_ready(instance, 0))
  3748. goto fail_ready_state;
  3749. /* Now re-enable MSI-X */
  3750. if (instance->msix_vectors)
  3751. pci_enable_msix(instance->pdev, instance->msixentry,
  3752. instance->msix_vectors);
  3753. switch (instance->pdev->device) {
  3754. case PCI_DEVICE_ID_LSI_FUSION:
  3755. case PCI_DEVICE_ID_LSI_INVADER:
  3756. {
  3757. megasas_reset_reply_desc(instance);
  3758. if (megasas_ioc_init_fusion(instance)) {
  3759. megasas_free_cmds(instance);
  3760. megasas_free_cmds_fusion(instance);
  3761. goto fail_init_mfi;
  3762. }
  3763. if (!megasas_get_map_info(instance))
  3764. megasas_sync_map_info(instance);
  3765. }
  3766. break;
  3767. default:
  3768. *instance->producer = 0;
  3769. *instance->consumer = 0;
  3770. if (megasas_issue_init_mfi(instance))
  3771. goto fail_init_mfi;
  3772. break;
  3773. }
  3774. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3775. (unsigned long)instance);
  3776. /*
  3777. * Register IRQ
  3778. */
  3779. if (instance->msix_vectors) {
  3780. for (i = 0 ; i < instance->msix_vectors; i++) {
  3781. instance->irq_context[i].instance = instance;
  3782. instance->irq_context[i].MSIxIndex = i;
  3783. if (request_irq(instance->msixentry[i].vector,
  3784. instance->instancet->service_isr, 0,
  3785. "megasas",
  3786. &instance->irq_context[i])) {
  3787. printk(KERN_DEBUG "megasas: Failed to "
  3788. "register IRQ for vector %d.\n", i);
  3789. for (j = 0 ; j < i ; j++)
  3790. free_irq(
  3791. instance->msixentry[j].vector,
  3792. &instance->irq_context[j]);
  3793. goto fail_irq;
  3794. }
  3795. }
  3796. } else {
  3797. instance->irq_context[0].instance = instance;
  3798. instance->irq_context[0].MSIxIndex = 0;
  3799. if (request_irq(pdev->irq, instance->instancet->service_isr,
  3800. IRQF_SHARED, "megasas",
  3801. &instance->irq_context[0])) {
  3802. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  3803. goto fail_irq;
  3804. }
  3805. }
  3806. instance->instancet->enable_intr(instance->reg_set);
  3807. instance->unload = 0;
  3808. /*
  3809. * Initiate AEN (Asynchronous Event Notification)
  3810. */
  3811. if (megasas_start_aen(instance))
  3812. printk(KERN_ERR "megasas: Start AEN failed\n");
  3813. return 0;
  3814. fail_irq:
  3815. fail_init_mfi:
  3816. if (instance->evt_detail)
  3817. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3818. instance->evt_detail,
  3819. instance->evt_detail_h);
  3820. if (instance->producer)
  3821. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3822. instance->producer_h);
  3823. if (instance->consumer)
  3824. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3825. instance->consumer_h);
  3826. scsi_host_put(host);
  3827. fail_set_dma_mask:
  3828. fail_ready_state:
  3829. pci_disable_device(pdev);
  3830. return -ENODEV;
  3831. }
  3832. #else
  3833. #define megasas_suspend NULL
  3834. #define megasas_resume NULL
  3835. #endif
  3836. /**
  3837. * megasas_detach_one - PCI hot"un"plug entry point
  3838. * @pdev: PCI device structure
  3839. */
  3840. static void megasas_detach_one(struct pci_dev *pdev)
  3841. {
  3842. int i;
  3843. struct Scsi_Host *host;
  3844. struct megasas_instance *instance;
  3845. struct fusion_context *fusion;
  3846. instance = pci_get_drvdata(pdev);
  3847. instance->unload = 1;
  3848. host = instance->host;
  3849. fusion = instance->ctrl_context;
  3850. scsi_remove_host(instance->host);
  3851. megasas_flush_cache(instance);
  3852. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3853. /* cancel the delayed work if this work still in queue*/
  3854. if (instance->ev != NULL) {
  3855. struct megasas_aen_event *ev = instance->ev;
  3856. cancel_delayed_work_sync(&ev->hotplug_work);
  3857. instance->ev = NULL;
  3858. }
  3859. tasklet_kill(&instance->isr_tasklet);
  3860. /*
  3861. * Take the instance off the instance array. Note that we will not
  3862. * decrement the max_index. We let this array be sparse array
  3863. */
  3864. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3865. if (megasas_mgmt_info.instance[i] == instance) {
  3866. megasas_mgmt_info.count--;
  3867. megasas_mgmt_info.instance[i] = NULL;
  3868. break;
  3869. }
  3870. }
  3871. pci_set_drvdata(instance->pdev, NULL);
  3872. instance->instancet->disable_intr(instance->reg_set);
  3873. if (instance->msix_vectors)
  3874. for (i = 0 ; i < instance->msix_vectors; i++)
  3875. free_irq(instance->msixentry[i].vector,
  3876. &instance->irq_context[i]);
  3877. else
  3878. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3879. if (instance->msix_vectors)
  3880. pci_disable_msix(instance->pdev);
  3881. switch (instance->pdev->device) {
  3882. case PCI_DEVICE_ID_LSI_FUSION:
  3883. case PCI_DEVICE_ID_LSI_INVADER:
  3884. megasas_release_fusion(instance);
  3885. for (i = 0; i < 2 ; i++)
  3886. if (fusion->ld_map[i])
  3887. dma_free_coherent(&instance->pdev->dev,
  3888. fusion->map_sz,
  3889. fusion->ld_map[i],
  3890. fusion->
  3891. ld_map_phys[i]);
  3892. kfree(instance->ctrl_context);
  3893. break;
  3894. default:
  3895. megasas_release_mfi(instance);
  3896. pci_free_consistent(pdev,
  3897. sizeof(struct megasas_evt_detail),
  3898. instance->evt_detail,
  3899. instance->evt_detail_h);
  3900. pci_free_consistent(pdev, sizeof(u32),
  3901. instance->producer,
  3902. instance->producer_h);
  3903. pci_free_consistent(pdev, sizeof(u32),
  3904. instance->consumer,
  3905. instance->consumer_h);
  3906. break;
  3907. }
  3908. scsi_host_put(host);
  3909. pci_set_drvdata(pdev, NULL);
  3910. pci_disable_device(pdev);
  3911. return;
  3912. }
  3913. /**
  3914. * megasas_shutdown - Shutdown entry point
  3915. * @device: Generic device structure
  3916. */
  3917. static void megasas_shutdown(struct pci_dev *pdev)
  3918. {
  3919. int i;
  3920. struct megasas_instance *instance = pci_get_drvdata(pdev);
  3921. instance->unload = 1;
  3922. megasas_flush_cache(instance);
  3923. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3924. instance->instancet->disable_intr(instance->reg_set);
  3925. if (instance->msix_vectors)
  3926. for (i = 0 ; i < instance->msix_vectors; i++)
  3927. free_irq(instance->msixentry[i].vector,
  3928. &instance->irq_context[i]);
  3929. else
  3930. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3931. if (instance->msix_vectors)
  3932. pci_disable_msix(instance->pdev);
  3933. }
  3934. /**
  3935. * megasas_mgmt_open - char node "open" entry point
  3936. */
  3937. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  3938. {
  3939. /*
  3940. * Allow only those users with admin rights
  3941. */
  3942. if (!capable(CAP_SYS_ADMIN))
  3943. return -EACCES;
  3944. return 0;
  3945. }
  3946. /**
  3947. * megasas_mgmt_fasync - Async notifier registration from applications
  3948. *
  3949. * This function adds the calling process to a driver global queue. When an
  3950. * event occurs, SIGIO will be sent to all processes in this queue.
  3951. */
  3952. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  3953. {
  3954. int rc;
  3955. mutex_lock(&megasas_async_queue_mutex);
  3956. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  3957. mutex_unlock(&megasas_async_queue_mutex);
  3958. if (rc >= 0) {
  3959. /* For sanity check when we get ioctl */
  3960. filep->private_data = filep;
  3961. return 0;
  3962. }
  3963. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  3964. return rc;
  3965. }
  3966. /**
  3967. * megasas_mgmt_poll - char node "poll" entry point
  3968. * */
  3969. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  3970. {
  3971. unsigned int mask;
  3972. unsigned long flags;
  3973. poll_wait(file, &megasas_poll_wait, wait);
  3974. spin_lock_irqsave(&poll_aen_lock, flags);
  3975. if (megasas_poll_wait_aen)
  3976. mask = (POLLIN | POLLRDNORM);
  3977. else
  3978. mask = 0;
  3979. spin_unlock_irqrestore(&poll_aen_lock, flags);
  3980. return mask;
  3981. }
  3982. /**
  3983. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  3984. * @instance: Adapter soft state
  3985. * @argp: User's ioctl packet
  3986. */
  3987. static int
  3988. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  3989. struct megasas_iocpacket __user * user_ioc,
  3990. struct megasas_iocpacket *ioc)
  3991. {
  3992. struct megasas_sge32 *kern_sge32;
  3993. struct megasas_cmd *cmd;
  3994. void *kbuff_arr[MAX_IOCTL_SGE];
  3995. dma_addr_t buf_handle = 0;
  3996. int error = 0, i;
  3997. void *sense = NULL;
  3998. dma_addr_t sense_handle;
  3999. unsigned long *sense_ptr;
  4000. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  4001. if (ioc->sge_count > MAX_IOCTL_SGE) {
  4002. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  4003. ioc->sge_count, MAX_IOCTL_SGE);
  4004. return -EINVAL;
  4005. }
  4006. cmd = megasas_get_cmd(instance);
  4007. if (!cmd) {
  4008. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  4009. return -ENOMEM;
  4010. }
  4011. /*
  4012. * User's IOCTL packet has 2 frames (maximum). Copy those two
  4013. * frames into our cmd's frames. cmd->frame's context will get
  4014. * overwritten when we copy from user's frames. So set that value
  4015. * alone separately
  4016. */
  4017. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  4018. cmd->frame->hdr.context = cmd->index;
  4019. cmd->frame->hdr.pad_0 = 0;
  4020. cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
  4021. MFI_FRAME_SENSE64);
  4022. /*
  4023. * The management interface between applications and the fw uses
  4024. * MFI frames. E.g, RAID configuration changes, LD property changes
  4025. * etc are accomplishes through different kinds of MFI frames. The
  4026. * driver needs to care only about substituting user buffers with
  4027. * kernel buffers in SGLs. The location of SGL is embedded in the
  4028. * struct iocpacket itself.
  4029. */
  4030. kern_sge32 = (struct megasas_sge32 *)
  4031. ((unsigned long)cmd->frame + ioc->sgl_off);
  4032. /*
  4033. * For each user buffer, create a mirror buffer and copy in
  4034. */
  4035. for (i = 0; i < ioc->sge_count; i++) {
  4036. if (!ioc->sgl[i].iov_len)
  4037. continue;
  4038. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  4039. ioc->sgl[i].iov_len,
  4040. &buf_handle, GFP_KERNEL);
  4041. if (!kbuff_arr[i]) {
  4042. printk(KERN_DEBUG "megasas: Failed to alloc "
  4043. "kernel SGL buffer for IOCTL \n");
  4044. error = -ENOMEM;
  4045. goto out;
  4046. }
  4047. /*
  4048. * We don't change the dma_coherent_mask, so
  4049. * pci_alloc_consistent only returns 32bit addresses
  4050. */
  4051. kern_sge32[i].phys_addr = (u32) buf_handle;
  4052. kern_sge32[i].length = ioc->sgl[i].iov_len;
  4053. /*
  4054. * We created a kernel buffer corresponding to the
  4055. * user buffer. Now copy in from the user buffer
  4056. */
  4057. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  4058. (u32) (ioc->sgl[i].iov_len))) {
  4059. error = -EFAULT;
  4060. goto out;
  4061. }
  4062. }
  4063. if (ioc->sense_len) {
  4064. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  4065. &sense_handle, GFP_KERNEL);
  4066. if (!sense) {
  4067. error = -ENOMEM;
  4068. goto out;
  4069. }
  4070. sense_ptr =
  4071. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  4072. *sense_ptr = sense_handle;
  4073. }
  4074. /*
  4075. * Set the sync_cmd flag so that the ISR knows not to complete this
  4076. * cmd to the SCSI mid-layer
  4077. */
  4078. cmd->sync_cmd = 1;
  4079. megasas_issue_blocked_cmd(instance, cmd);
  4080. cmd->sync_cmd = 0;
  4081. /*
  4082. * copy out the kernel buffers to user buffers
  4083. */
  4084. for (i = 0; i < ioc->sge_count; i++) {
  4085. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  4086. ioc->sgl[i].iov_len)) {
  4087. error = -EFAULT;
  4088. goto out;
  4089. }
  4090. }
  4091. /*
  4092. * copy out the sense
  4093. */
  4094. if (ioc->sense_len) {
  4095. /*
  4096. * sense_ptr points to the location that has the user
  4097. * sense buffer address
  4098. */
  4099. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  4100. ioc->sense_off);
  4101. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  4102. sense, ioc->sense_len)) {
  4103. printk(KERN_ERR "megasas: Failed to copy out to user "
  4104. "sense data\n");
  4105. error = -EFAULT;
  4106. goto out;
  4107. }
  4108. }
  4109. /*
  4110. * copy the status codes returned by the fw
  4111. */
  4112. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  4113. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  4114. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  4115. error = -EFAULT;
  4116. }
  4117. out:
  4118. if (sense) {
  4119. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  4120. sense, sense_handle);
  4121. }
  4122. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  4123. dma_free_coherent(&instance->pdev->dev,
  4124. kern_sge32[i].length,
  4125. kbuff_arr[i], kern_sge32[i].phys_addr);
  4126. }
  4127. megasas_return_cmd(instance, cmd);
  4128. return error;
  4129. }
  4130. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  4131. {
  4132. struct megasas_iocpacket __user *user_ioc =
  4133. (struct megasas_iocpacket __user *)arg;
  4134. struct megasas_iocpacket *ioc;
  4135. struct megasas_instance *instance;
  4136. int error;
  4137. int i;
  4138. unsigned long flags;
  4139. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4140. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  4141. if (!ioc)
  4142. return -ENOMEM;
  4143. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  4144. error = -EFAULT;
  4145. goto out_kfree_ioc;
  4146. }
  4147. instance = megasas_lookup_instance(ioc->host_no);
  4148. if (!instance) {
  4149. error = -ENODEV;
  4150. goto out_kfree_ioc;
  4151. }
  4152. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4153. printk(KERN_ERR "Controller in crit error\n");
  4154. error = -ENODEV;
  4155. goto out_kfree_ioc;
  4156. }
  4157. if (instance->unload == 1) {
  4158. error = -ENODEV;
  4159. goto out_kfree_ioc;
  4160. }
  4161. /*
  4162. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  4163. */
  4164. if (down_interruptible(&instance->ioctl_sem)) {
  4165. error = -ERESTARTSYS;
  4166. goto out_kfree_ioc;
  4167. }
  4168. for (i = 0; i < wait_time; i++) {
  4169. spin_lock_irqsave(&instance->hba_lock, flags);
  4170. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4171. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4172. break;
  4173. }
  4174. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4175. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4176. printk(KERN_NOTICE "megasas: waiting"
  4177. "for controller reset to finish\n");
  4178. }
  4179. msleep(1000);
  4180. }
  4181. spin_lock_irqsave(&instance->hba_lock, flags);
  4182. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4183. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4184. printk(KERN_ERR "megaraid_sas: timed out while"
  4185. "waiting for HBA to recover\n");
  4186. error = -ENODEV;
  4187. goto out_kfree_ioc;
  4188. }
  4189. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4190. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  4191. up(&instance->ioctl_sem);
  4192. out_kfree_ioc:
  4193. kfree(ioc);
  4194. return error;
  4195. }
  4196. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  4197. {
  4198. struct megasas_instance *instance;
  4199. struct megasas_aen aen;
  4200. int error;
  4201. int i;
  4202. unsigned long flags;
  4203. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4204. if (file->private_data != file) {
  4205. printk(KERN_DEBUG "megasas: fasync_helper was not "
  4206. "called first\n");
  4207. return -EINVAL;
  4208. }
  4209. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  4210. return -EFAULT;
  4211. instance = megasas_lookup_instance(aen.host_no);
  4212. if (!instance)
  4213. return -ENODEV;
  4214. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4215. return -ENODEV;
  4216. }
  4217. if (instance->unload == 1) {
  4218. return -ENODEV;
  4219. }
  4220. for (i = 0; i < wait_time; i++) {
  4221. spin_lock_irqsave(&instance->hba_lock, flags);
  4222. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4223. spin_unlock_irqrestore(&instance->hba_lock,
  4224. flags);
  4225. break;
  4226. }
  4227. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4228. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4229. printk(KERN_NOTICE "megasas: waiting for"
  4230. "controller reset to finish\n");
  4231. }
  4232. msleep(1000);
  4233. }
  4234. spin_lock_irqsave(&instance->hba_lock, flags);
  4235. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4236. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4237. printk(KERN_ERR "megaraid_sas: timed out while waiting"
  4238. "for HBA to recover.\n");
  4239. return -ENODEV;
  4240. }
  4241. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4242. mutex_lock(&instance->aen_mutex);
  4243. error = megasas_register_aen(instance, aen.seq_num,
  4244. aen.class_locale_word);
  4245. mutex_unlock(&instance->aen_mutex);
  4246. return error;
  4247. }
  4248. /**
  4249. * megasas_mgmt_ioctl - char node ioctl entry point
  4250. */
  4251. static long
  4252. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4253. {
  4254. switch (cmd) {
  4255. case MEGASAS_IOC_FIRMWARE:
  4256. return megasas_mgmt_ioctl_fw(file, arg);
  4257. case MEGASAS_IOC_GET_AEN:
  4258. return megasas_mgmt_ioctl_aen(file, arg);
  4259. }
  4260. return -ENOTTY;
  4261. }
  4262. #ifdef CONFIG_COMPAT
  4263. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  4264. {
  4265. struct compat_megasas_iocpacket __user *cioc =
  4266. (struct compat_megasas_iocpacket __user *)arg;
  4267. struct megasas_iocpacket __user *ioc =
  4268. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  4269. int i;
  4270. int error = 0;
  4271. compat_uptr_t ptr;
  4272. if (clear_user(ioc, sizeof(*ioc)))
  4273. return -EFAULT;
  4274. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  4275. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  4276. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  4277. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  4278. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  4279. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  4280. return -EFAULT;
  4281. /*
  4282. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  4283. * sense_len is not null, so prepare the 64bit value under
  4284. * the same condition.
  4285. */
  4286. if (ioc->sense_len) {
  4287. void __user **sense_ioc_ptr =
  4288. (void __user **)(ioc->frame.raw + ioc->sense_off);
  4289. compat_uptr_t *sense_cioc_ptr =
  4290. (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
  4291. if (get_user(ptr, sense_cioc_ptr) ||
  4292. put_user(compat_ptr(ptr), sense_ioc_ptr))
  4293. return -EFAULT;
  4294. }
  4295. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  4296. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  4297. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  4298. copy_in_user(&ioc->sgl[i].iov_len,
  4299. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  4300. return -EFAULT;
  4301. }
  4302. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  4303. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  4304. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  4305. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  4306. return -EFAULT;
  4307. }
  4308. return error;
  4309. }
  4310. static long
  4311. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  4312. unsigned long arg)
  4313. {
  4314. switch (cmd) {
  4315. case MEGASAS_IOC_FIRMWARE32:
  4316. return megasas_mgmt_compat_ioctl_fw(file, arg);
  4317. case MEGASAS_IOC_GET_AEN:
  4318. return megasas_mgmt_ioctl_aen(file, arg);
  4319. }
  4320. return -ENOTTY;
  4321. }
  4322. #endif
  4323. /*
  4324. * File operations structure for management interface
  4325. */
  4326. static const struct file_operations megasas_mgmt_fops = {
  4327. .owner = THIS_MODULE,
  4328. .open = megasas_mgmt_open,
  4329. .fasync = megasas_mgmt_fasync,
  4330. .unlocked_ioctl = megasas_mgmt_ioctl,
  4331. .poll = megasas_mgmt_poll,
  4332. #ifdef CONFIG_COMPAT
  4333. .compat_ioctl = megasas_mgmt_compat_ioctl,
  4334. #endif
  4335. .llseek = noop_llseek,
  4336. };
  4337. /*
  4338. * PCI hotplug support registration structure
  4339. */
  4340. static struct pci_driver megasas_pci_driver = {
  4341. .name = "megaraid_sas",
  4342. .id_table = megasas_pci_table,
  4343. .probe = megasas_probe_one,
  4344. .remove = megasas_detach_one,
  4345. .suspend = megasas_suspend,
  4346. .resume = megasas_resume,
  4347. .shutdown = megasas_shutdown,
  4348. };
  4349. /*
  4350. * Sysfs driver attributes
  4351. */
  4352. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  4353. {
  4354. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  4355. MEGASAS_VERSION);
  4356. }
  4357. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  4358. static ssize_t
  4359. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  4360. {
  4361. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  4362. MEGASAS_RELDATE);
  4363. }
  4364. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  4365. NULL);
  4366. static ssize_t
  4367. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  4368. {
  4369. return sprintf(buf, "%u\n", support_poll_for_event);
  4370. }
  4371. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  4372. megasas_sysfs_show_support_poll_for_event, NULL);
  4373. static ssize_t
  4374. megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
  4375. {
  4376. return sprintf(buf, "%u\n", support_device_change);
  4377. }
  4378. static DRIVER_ATTR(support_device_change, S_IRUGO,
  4379. megasas_sysfs_show_support_device_change, NULL);
  4380. static ssize_t
  4381. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  4382. {
  4383. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  4384. }
  4385. static ssize_t
  4386. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  4387. {
  4388. int retval = count;
  4389. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  4390. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  4391. retval = -EINVAL;
  4392. }
  4393. return retval;
  4394. }
  4395. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  4396. megasas_sysfs_set_dbg_lvl);
  4397. static void
  4398. megasas_aen_polling(struct work_struct *work)
  4399. {
  4400. struct megasas_aen_event *ev =
  4401. container_of(work, struct megasas_aen_event, hotplug_work.work);
  4402. struct megasas_instance *instance = ev->instance;
  4403. union megasas_evt_class_locale class_locale;
  4404. struct Scsi_Host *host;
  4405. struct scsi_device *sdev1;
  4406. u16 pd_index = 0;
  4407. u16 ld_index = 0;
  4408. int i, j, doscan = 0;
  4409. u32 seq_num;
  4410. int error;
  4411. if (!instance) {
  4412. printk(KERN_ERR "invalid instance!\n");
  4413. kfree(ev);
  4414. return;
  4415. }
  4416. instance->ev = NULL;
  4417. host = instance->host;
  4418. if (instance->evt_detail) {
  4419. switch (instance->evt_detail->code) {
  4420. case MR_EVT_PD_INSERTED:
  4421. if (megasas_get_pd_list(instance) == 0) {
  4422. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4423. for (j = 0;
  4424. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4425. j++) {
  4426. pd_index =
  4427. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4428. sdev1 =
  4429. scsi_device_lookup(host, i, j, 0);
  4430. if (instance->pd_list[pd_index].driveState
  4431. == MR_PD_STATE_SYSTEM) {
  4432. if (!sdev1) {
  4433. scsi_add_device(host, i, j, 0);
  4434. }
  4435. if (sdev1)
  4436. scsi_device_put(sdev1);
  4437. }
  4438. }
  4439. }
  4440. }
  4441. doscan = 0;
  4442. break;
  4443. case MR_EVT_PD_REMOVED:
  4444. if (megasas_get_pd_list(instance) == 0) {
  4445. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4446. for (j = 0;
  4447. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4448. j++) {
  4449. pd_index =
  4450. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4451. sdev1 =
  4452. scsi_device_lookup(host, i, j, 0);
  4453. if (instance->pd_list[pd_index].driveState
  4454. == MR_PD_STATE_SYSTEM) {
  4455. if (sdev1) {
  4456. scsi_device_put(sdev1);
  4457. }
  4458. } else {
  4459. if (sdev1) {
  4460. scsi_remove_device(sdev1);
  4461. scsi_device_put(sdev1);
  4462. }
  4463. }
  4464. }
  4465. }
  4466. }
  4467. doscan = 0;
  4468. break;
  4469. case MR_EVT_LD_OFFLINE:
  4470. case MR_EVT_CFG_CLEARED:
  4471. case MR_EVT_LD_DELETED:
  4472. megasas_get_ld_list(instance);
  4473. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4474. for (j = 0;
  4475. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4476. j++) {
  4477. ld_index =
  4478. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4479. sdev1 = scsi_device_lookup(host,
  4480. i + MEGASAS_MAX_LD_CHANNELS,
  4481. j,
  4482. 0);
  4483. if (instance->ld_ids[ld_index] != 0xff) {
  4484. if (sdev1) {
  4485. scsi_device_put(sdev1);
  4486. }
  4487. } else {
  4488. if (sdev1) {
  4489. scsi_remove_device(sdev1);
  4490. scsi_device_put(sdev1);
  4491. }
  4492. }
  4493. }
  4494. }
  4495. doscan = 0;
  4496. break;
  4497. case MR_EVT_LD_CREATED:
  4498. megasas_get_ld_list(instance);
  4499. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4500. for (j = 0;
  4501. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4502. j++) {
  4503. ld_index =
  4504. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4505. sdev1 = scsi_device_lookup(host,
  4506. i+MEGASAS_MAX_LD_CHANNELS,
  4507. j, 0);
  4508. if (instance->ld_ids[ld_index] !=
  4509. 0xff) {
  4510. if (!sdev1) {
  4511. scsi_add_device(host,
  4512. i + 2,
  4513. j, 0);
  4514. }
  4515. }
  4516. if (sdev1) {
  4517. scsi_device_put(sdev1);
  4518. }
  4519. }
  4520. }
  4521. doscan = 0;
  4522. break;
  4523. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  4524. case MR_EVT_FOREIGN_CFG_IMPORTED:
  4525. case MR_EVT_LD_STATE_CHANGE:
  4526. doscan = 1;
  4527. break;
  4528. default:
  4529. doscan = 0;
  4530. break;
  4531. }
  4532. } else {
  4533. printk(KERN_ERR "invalid evt_detail!\n");
  4534. kfree(ev);
  4535. return;
  4536. }
  4537. if (doscan) {
  4538. printk(KERN_INFO "scanning ...\n");
  4539. megasas_get_pd_list(instance);
  4540. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4541. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4542. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  4543. sdev1 = scsi_device_lookup(host, i, j, 0);
  4544. if (instance->pd_list[pd_index].driveState ==
  4545. MR_PD_STATE_SYSTEM) {
  4546. if (!sdev1) {
  4547. scsi_add_device(host, i, j, 0);
  4548. }
  4549. if (sdev1)
  4550. scsi_device_put(sdev1);
  4551. } else {
  4552. if (sdev1) {
  4553. scsi_remove_device(sdev1);
  4554. scsi_device_put(sdev1);
  4555. }
  4556. }
  4557. }
  4558. }
  4559. megasas_get_ld_list(instance);
  4560. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4561. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4562. ld_index =
  4563. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4564. sdev1 = scsi_device_lookup(host,
  4565. i+MEGASAS_MAX_LD_CHANNELS, j, 0);
  4566. if (instance->ld_ids[ld_index] != 0xff) {
  4567. if (!sdev1) {
  4568. scsi_add_device(host,
  4569. i+2,
  4570. j, 0);
  4571. } else {
  4572. scsi_device_put(sdev1);
  4573. }
  4574. } else {
  4575. if (sdev1) {
  4576. scsi_remove_device(sdev1);
  4577. scsi_device_put(sdev1);
  4578. }
  4579. }
  4580. }
  4581. }
  4582. }
  4583. if ( instance->aen_cmd != NULL ) {
  4584. kfree(ev);
  4585. return ;
  4586. }
  4587. seq_num = instance->evt_detail->seq_num + 1;
  4588. /* Register AEN with FW for latest sequence number plus 1 */
  4589. class_locale.members.reserved = 0;
  4590. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  4591. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  4592. mutex_lock(&instance->aen_mutex);
  4593. error = megasas_register_aen(instance, seq_num,
  4594. class_locale.word);
  4595. mutex_unlock(&instance->aen_mutex);
  4596. if (error)
  4597. printk(KERN_ERR "register aen failed error %x\n", error);
  4598. kfree(ev);
  4599. }
  4600. /**
  4601. * megasas_init - Driver load entry point
  4602. */
  4603. static int __init megasas_init(void)
  4604. {
  4605. int rval;
  4606. /*
  4607. * Announce driver version and other information
  4608. */
  4609. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  4610. MEGASAS_EXT_VERSION);
  4611. spin_lock_init(&poll_aen_lock);
  4612. support_poll_for_event = 2;
  4613. support_device_change = 1;
  4614. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  4615. /*
  4616. * Register character device node
  4617. */
  4618. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  4619. if (rval < 0) {
  4620. printk(KERN_DEBUG "megasas: failed to open device node\n");
  4621. return rval;
  4622. }
  4623. megasas_mgmt_majorno = rval;
  4624. /*
  4625. * Register ourselves as PCI hotplug module
  4626. */
  4627. rval = pci_register_driver(&megasas_pci_driver);
  4628. if (rval) {
  4629. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  4630. goto err_pcidrv;
  4631. }
  4632. rval = driver_create_file(&megasas_pci_driver.driver,
  4633. &driver_attr_version);
  4634. if (rval)
  4635. goto err_dcf_attr_ver;
  4636. rval = driver_create_file(&megasas_pci_driver.driver,
  4637. &driver_attr_release_date);
  4638. if (rval)
  4639. goto err_dcf_rel_date;
  4640. rval = driver_create_file(&megasas_pci_driver.driver,
  4641. &driver_attr_support_poll_for_event);
  4642. if (rval)
  4643. goto err_dcf_support_poll_for_event;
  4644. rval = driver_create_file(&megasas_pci_driver.driver,
  4645. &driver_attr_dbg_lvl);
  4646. if (rval)
  4647. goto err_dcf_dbg_lvl;
  4648. rval = driver_create_file(&megasas_pci_driver.driver,
  4649. &driver_attr_support_device_change);
  4650. if (rval)
  4651. goto err_dcf_support_device_change;
  4652. return rval;
  4653. err_dcf_support_device_change:
  4654. driver_remove_file(&megasas_pci_driver.driver,
  4655. &driver_attr_dbg_lvl);
  4656. err_dcf_dbg_lvl:
  4657. driver_remove_file(&megasas_pci_driver.driver,
  4658. &driver_attr_support_poll_for_event);
  4659. err_dcf_support_poll_for_event:
  4660. driver_remove_file(&megasas_pci_driver.driver,
  4661. &driver_attr_release_date);
  4662. err_dcf_rel_date:
  4663. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4664. err_dcf_attr_ver:
  4665. pci_unregister_driver(&megasas_pci_driver);
  4666. err_pcidrv:
  4667. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4668. return rval;
  4669. }
  4670. /**
  4671. * megasas_exit - Driver unload entry point
  4672. */
  4673. static void __exit megasas_exit(void)
  4674. {
  4675. driver_remove_file(&megasas_pci_driver.driver,
  4676. &driver_attr_dbg_lvl);
  4677. driver_remove_file(&megasas_pci_driver.driver,
  4678. &driver_attr_support_poll_for_event);
  4679. driver_remove_file(&megasas_pci_driver.driver,
  4680. &driver_attr_support_device_change);
  4681. driver_remove_file(&megasas_pci_driver.driver,
  4682. &driver_attr_release_date);
  4683. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4684. pci_unregister_driver(&megasas_pci_driver);
  4685. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4686. }
  4687. module_init(megasas_init);
  4688. module_exit(megasas_exit);