megaraid_sas.c 129 KB

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