megaraid_sas_base.c 138 KB

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