megaraid_sas_base.c 144 KB

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