mpt3sas_base.c 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795
  1. /*
  2. * This is the Fusion MPT base driver providing common API layer interface
  3. * for access to MPT (Message Passing Technology) firmware.
  4. *
  5. * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
  6. * Copyright (C) 2012-2014 LSI Corporation
  7. * Copyright (C) 2013-2014 Avago Technologies
  8. * (mailto: MPT-FusionLinux.pdl@avagotech.com)
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * NO WARRANTY
  21. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  22. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  23. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  24. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  25. * solely responsible for determining the appropriateness of using and
  26. * distributing the Program and assumes all risks associated with its
  27. * exercise of rights under this Agreement, including but not limited to
  28. * the risks and costs of program errors, damage to or loss of data,
  29. * programs or equipment, and unavailability or interruption of operations.
  30. * DISCLAIMER OF LIABILITY
  31. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  34. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  35. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  37. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/errno.h>
  46. #include <linux/init.h>
  47. #include <linux/slab.h>
  48. #include <linux/types.h>
  49. #include <linux/pci.h>
  50. #include <linux/kdev_t.h>
  51. #include <linux/blkdev.h>
  52. #include <linux/delay.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/dma-mapping.h>
  55. #include <linux/io.h>
  56. #include <linux/time.h>
  57. #include <linux/ktime.h>
  58. #include <linux/kthread.h>
  59. #include <linux/aer.h>
  60. #include "mpt3sas_base.h"
  61. static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
  62. #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
  63. /* maximum controller queue depth */
  64. #define MAX_HBA_QUEUE_DEPTH 30000
  65. #define MAX_CHAIN_DEPTH 100000
  66. static int max_queue_depth = -1;
  67. module_param(max_queue_depth, int, 0);
  68. MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
  69. static int max_sgl_entries = -1;
  70. module_param(max_sgl_entries, int, 0);
  71. MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
  72. static int msix_disable = -1;
  73. module_param(msix_disable, int, 0);
  74. MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
  75. static int smp_affinity_enable = 1;
  76. module_param(smp_affinity_enable, int, S_IRUGO);
  77. MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
  78. static int max_msix_vectors = -1;
  79. module_param(max_msix_vectors, int, 0);
  80. MODULE_PARM_DESC(max_msix_vectors,
  81. " max msix vectors");
  82. static int mpt3sas_fwfault_debug;
  83. MODULE_PARM_DESC(mpt3sas_fwfault_debug,
  84. " enable detection of firmware fault and halt firmware - (default=0)");
  85. static int
  86. _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
  87. /**
  88. * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
  89. *
  90. */
  91. static int
  92. _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
  93. {
  94. int ret = param_set_int(val, kp);
  95. struct MPT3SAS_ADAPTER *ioc;
  96. if (ret)
  97. return ret;
  98. /* global ioc spinlock to protect controller list on list operations */
  99. pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
  100. spin_lock(&gioc_lock);
  101. list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
  102. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  103. spin_unlock(&gioc_lock);
  104. return 0;
  105. }
  106. module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
  107. param_get_int, &mpt3sas_fwfault_debug, 0644);
  108. /**
  109. * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
  110. * @arg: input argument, used to derive ioc
  111. *
  112. * Return 0 if controller is removed from pci subsystem.
  113. * Return -1 for other case.
  114. */
  115. static int mpt3sas_remove_dead_ioc_func(void *arg)
  116. {
  117. struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
  118. struct pci_dev *pdev;
  119. if ((ioc == NULL))
  120. return -1;
  121. pdev = ioc->pdev;
  122. if ((pdev == NULL))
  123. return -1;
  124. pci_stop_and_remove_bus_device_locked(pdev);
  125. return 0;
  126. }
  127. /**
  128. * _base_fault_reset_work - workq handling ioc fault conditions
  129. * @work: input argument, used to derive ioc
  130. * Context: sleep.
  131. *
  132. * Return nothing.
  133. */
  134. static void
  135. _base_fault_reset_work(struct work_struct *work)
  136. {
  137. struct MPT3SAS_ADAPTER *ioc =
  138. container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
  139. unsigned long flags;
  140. u32 doorbell;
  141. int rc;
  142. struct task_struct *p;
  143. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  144. if (ioc->shost_recovery || ioc->pci_error_recovery)
  145. goto rearm_timer;
  146. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  147. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  148. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
  149. pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
  150. ioc->name);
  151. /* It may be possible that EEH recovery can resolve some of
  152. * pci bus failure issues rather removing the dead ioc function
  153. * by considering controller is in a non-operational state. So
  154. * here priority is given to the EEH recovery. If it doesn't
  155. * not resolve this issue, mpt3sas driver will consider this
  156. * controller to non-operational state and remove the dead ioc
  157. * function.
  158. */
  159. if (ioc->non_operational_loop++ < 5) {
  160. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
  161. flags);
  162. goto rearm_timer;
  163. }
  164. /*
  165. * Call _scsih_flush_pending_cmds callback so that we flush all
  166. * pending commands back to OS. This call is required to aovid
  167. * deadlock at block layer. Dead IOC will fail to do diag reset,
  168. * and this call is safe since dead ioc will never return any
  169. * command back from HW.
  170. */
  171. ioc->schedule_dead_ioc_flush_running_cmds(ioc);
  172. /*
  173. * Set remove_host flag early since kernel thread will
  174. * take some time to execute.
  175. */
  176. ioc->remove_host = 1;
  177. /*Remove the Dead Host */
  178. p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
  179. "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
  180. if (IS_ERR(p))
  181. pr_err(MPT3SAS_FMT
  182. "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
  183. ioc->name, __func__);
  184. else
  185. pr_err(MPT3SAS_FMT
  186. "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
  187. ioc->name, __func__);
  188. return; /* don't rearm timer */
  189. }
  190. ioc->non_operational_loop = 0;
  191. if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
  192. rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  193. pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
  194. __func__, (rc == 0) ? "success" : "failed");
  195. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  196. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  197. mpt3sas_base_fault_info(ioc, doorbell &
  198. MPI2_DOORBELL_DATA_MASK);
  199. if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
  200. MPI2_IOC_STATE_OPERATIONAL)
  201. return; /* don't rearm timer */
  202. }
  203. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  204. rearm_timer:
  205. if (ioc->fault_reset_work_q)
  206. queue_delayed_work(ioc->fault_reset_work_q,
  207. &ioc->fault_reset_work,
  208. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  209. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  210. }
  211. /**
  212. * mpt3sas_base_start_watchdog - start the fault_reset_work_q
  213. * @ioc: per adapter object
  214. * Context: sleep.
  215. *
  216. * Return nothing.
  217. */
  218. void
  219. mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
  220. {
  221. unsigned long flags;
  222. if (ioc->fault_reset_work_q)
  223. return;
  224. /* initialize fault polling */
  225. INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
  226. snprintf(ioc->fault_reset_work_q_name,
  227. sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
  228. ioc->driver_name, ioc->id);
  229. ioc->fault_reset_work_q =
  230. create_singlethread_workqueue(ioc->fault_reset_work_q_name);
  231. if (!ioc->fault_reset_work_q) {
  232. pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
  233. ioc->name, __func__, __LINE__);
  234. return;
  235. }
  236. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  237. if (ioc->fault_reset_work_q)
  238. queue_delayed_work(ioc->fault_reset_work_q,
  239. &ioc->fault_reset_work,
  240. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  241. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  242. }
  243. /**
  244. * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
  245. * @ioc: per adapter object
  246. * Context: sleep.
  247. *
  248. * Return nothing.
  249. */
  250. void
  251. mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
  252. {
  253. unsigned long flags;
  254. struct workqueue_struct *wq;
  255. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  256. wq = ioc->fault_reset_work_q;
  257. ioc->fault_reset_work_q = NULL;
  258. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  259. if (wq) {
  260. if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
  261. flush_workqueue(wq);
  262. destroy_workqueue(wq);
  263. }
  264. }
  265. /**
  266. * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
  267. * @ioc: per adapter object
  268. * @fault_code: fault code
  269. *
  270. * Return nothing.
  271. */
  272. void
  273. mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
  274. {
  275. pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
  276. ioc->name, fault_code);
  277. }
  278. /**
  279. * mpt3sas_halt_firmware - halt's mpt controller firmware
  280. * @ioc: per adapter object
  281. *
  282. * For debugging timeout related issues. Writing 0xCOFFEE00
  283. * to the doorbell register will halt controller firmware. With
  284. * the purpose to stop both driver and firmware, the enduser can
  285. * obtain a ring buffer from controller UART.
  286. */
  287. void
  288. mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
  289. {
  290. u32 doorbell;
  291. if (!ioc->fwfault_debug)
  292. return;
  293. dump_stack();
  294. doorbell = readl(&ioc->chip->Doorbell);
  295. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  296. mpt3sas_base_fault_info(ioc , doorbell);
  297. else {
  298. writel(0xC0FFEE00, &ioc->chip->Doorbell);
  299. pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
  300. ioc->name);
  301. }
  302. if (ioc->fwfault_debug == 2)
  303. for (;;)
  304. ;
  305. else
  306. panic("panic in %s\n", __func__);
  307. }
  308. /**
  309. * _base_sas_ioc_info - verbose translation of the ioc status
  310. * @ioc: per adapter object
  311. * @mpi_reply: reply mf payload returned from firmware
  312. * @request_hdr: request mf
  313. *
  314. * Return nothing.
  315. */
  316. static void
  317. _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
  318. MPI2RequestHeader_t *request_hdr)
  319. {
  320. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  321. MPI2_IOCSTATUS_MASK;
  322. char *desc = NULL;
  323. u16 frame_sz;
  324. char *func_str = NULL;
  325. /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
  326. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  327. request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  328. request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
  329. return;
  330. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  331. return;
  332. switch (ioc_status) {
  333. /****************************************************************************
  334. * Common IOCStatus values for all replies
  335. ****************************************************************************/
  336. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  337. desc = "invalid function";
  338. break;
  339. case MPI2_IOCSTATUS_BUSY:
  340. desc = "busy";
  341. break;
  342. case MPI2_IOCSTATUS_INVALID_SGL:
  343. desc = "invalid sgl";
  344. break;
  345. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  346. desc = "internal error";
  347. break;
  348. case MPI2_IOCSTATUS_INVALID_VPID:
  349. desc = "invalid vpid";
  350. break;
  351. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  352. desc = "insufficient resources";
  353. break;
  354. case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
  355. desc = "insufficient power";
  356. break;
  357. case MPI2_IOCSTATUS_INVALID_FIELD:
  358. desc = "invalid field";
  359. break;
  360. case MPI2_IOCSTATUS_INVALID_STATE:
  361. desc = "invalid state";
  362. break;
  363. case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
  364. desc = "op state not supported";
  365. break;
  366. /****************************************************************************
  367. * Config IOCStatus values
  368. ****************************************************************************/
  369. case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
  370. desc = "config invalid action";
  371. break;
  372. case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
  373. desc = "config invalid type";
  374. break;
  375. case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
  376. desc = "config invalid page";
  377. break;
  378. case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
  379. desc = "config invalid data";
  380. break;
  381. case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
  382. desc = "config no defaults";
  383. break;
  384. case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
  385. desc = "config cant commit";
  386. break;
  387. /****************************************************************************
  388. * SCSI IO Reply
  389. ****************************************************************************/
  390. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  391. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  392. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  393. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  394. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  395. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  396. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  397. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  398. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  399. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  400. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  401. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  402. break;
  403. /****************************************************************************
  404. * For use by SCSI Initiator and SCSI Target end-to-end data protection
  405. ****************************************************************************/
  406. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  407. desc = "eedp guard error";
  408. break;
  409. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  410. desc = "eedp ref tag error";
  411. break;
  412. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  413. desc = "eedp app tag error";
  414. break;
  415. /****************************************************************************
  416. * SCSI Target values
  417. ****************************************************************************/
  418. case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
  419. desc = "target invalid io index";
  420. break;
  421. case MPI2_IOCSTATUS_TARGET_ABORTED:
  422. desc = "target aborted";
  423. break;
  424. case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
  425. desc = "target no conn retryable";
  426. break;
  427. case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
  428. desc = "target no connection";
  429. break;
  430. case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
  431. desc = "target xfer count mismatch";
  432. break;
  433. case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
  434. desc = "target data offset error";
  435. break;
  436. case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
  437. desc = "target too much write data";
  438. break;
  439. case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
  440. desc = "target iu too short";
  441. break;
  442. case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
  443. desc = "target ack nak timeout";
  444. break;
  445. case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
  446. desc = "target nak received";
  447. break;
  448. /****************************************************************************
  449. * Serial Attached SCSI values
  450. ****************************************************************************/
  451. case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
  452. desc = "smp request failed";
  453. break;
  454. case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
  455. desc = "smp data overrun";
  456. break;
  457. /****************************************************************************
  458. * Diagnostic Buffer Post / Diagnostic Release values
  459. ****************************************************************************/
  460. case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
  461. desc = "diagnostic released";
  462. break;
  463. default:
  464. break;
  465. }
  466. if (!desc)
  467. return;
  468. switch (request_hdr->Function) {
  469. case MPI2_FUNCTION_CONFIG:
  470. frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
  471. func_str = "config_page";
  472. break;
  473. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  474. frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
  475. func_str = "task_mgmt";
  476. break;
  477. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  478. frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
  479. func_str = "sas_iounit_ctl";
  480. break;
  481. case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  482. frame_sz = sizeof(Mpi2SepRequest_t);
  483. func_str = "enclosure";
  484. break;
  485. case MPI2_FUNCTION_IOC_INIT:
  486. frame_sz = sizeof(Mpi2IOCInitRequest_t);
  487. func_str = "ioc_init";
  488. break;
  489. case MPI2_FUNCTION_PORT_ENABLE:
  490. frame_sz = sizeof(Mpi2PortEnableRequest_t);
  491. func_str = "port_enable";
  492. break;
  493. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  494. frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
  495. func_str = "smp_passthru";
  496. break;
  497. default:
  498. frame_sz = 32;
  499. func_str = "unknown";
  500. break;
  501. }
  502. pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
  503. ioc->name, desc, ioc_status, request_hdr, func_str);
  504. _debug_dump_mf(request_hdr, frame_sz/4);
  505. }
  506. /**
  507. * _base_display_event_data - verbose translation of firmware asyn events
  508. * @ioc: per adapter object
  509. * @mpi_reply: reply mf payload returned from firmware
  510. *
  511. * Return nothing.
  512. */
  513. static void
  514. _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
  515. Mpi2EventNotificationReply_t *mpi_reply)
  516. {
  517. char *desc = NULL;
  518. u16 event;
  519. if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
  520. return;
  521. event = le16_to_cpu(mpi_reply->Event);
  522. switch (event) {
  523. case MPI2_EVENT_LOG_DATA:
  524. desc = "Log Data";
  525. break;
  526. case MPI2_EVENT_STATE_CHANGE:
  527. desc = "Status Change";
  528. break;
  529. case MPI2_EVENT_HARD_RESET_RECEIVED:
  530. desc = "Hard Reset Received";
  531. break;
  532. case MPI2_EVENT_EVENT_CHANGE:
  533. desc = "Event Change";
  534. break;
  535. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  536. desc = "Device Status Change";
  537. break;
  538. case MPI2_EVENT_IR_OPERATION_STATUS:
  539. if (!ioc->hide_ir_msg)
  540. desc = "IR Operation Status";
  541. break;
  542. case MPI2_EVENT_SAS_DISCOVERY:
  543. {
  544. Mpi2EventDataSasDiscovery_t *event_data =
  545. (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
  546. pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
  547. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  548. "start" : "stop");
  549. if (event_data->DiscoveryStatus)
  550. pr_info("discovery_status(0x%08x)",
  551. le32_to_cpu(event_data->DiscoveryStatus));
  552. pr_info("\n");
  553. return;
  554. }
  555. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  556. desc = "SAS Broadcast Primitive";
  557. break;
  558. case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  559. desc = "SAS Init Device Status Change";
  560. break;
  561. case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  562. desc = "SAS Init Table Overflow";
  563. break;
  564. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  565. desc = "SAS Topology Change List";
  566. break;
  567. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  568. desc = "SAS Enclosure Device Status Change";
  569. break;
  570. case MPI2_EVENT_IR_VOLUME:
  571. if (!ioc->hide_ir_msg)
  572. desc = "IR Volume";
  573. break;
  574. case MPI2_EVENT_IR_PHYSICAL_DISK:
  575. if (!ioc->hide_ir_msg)
  576. desc = "IR Physical Disk";
  577. break;
  578. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  579. if (!ioc->hide_ir_msg)
  580. desc = "IR Configuration Change List";
  581. break;
  582. case MPI2_EVENT_LOG_ENTRY_ADDED:
  583. if (!ioc->hide_ir_msg)
  584. desc = "Log Entry Added";
  585. break;
  586. case MPI2_EVENT_TEMP_THRESHOLD:
  587. desc = "Temperature Threshold";
  588. break;
  589. case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
  590. desc = "Active cable exception";
  591. break;
  592. }
  593. if (!desc)
  594. return;
  595. pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
  596. }
  597. /**
  598. * _base_sas_log_info - verbose translation of firmware log info
  599. * @ioc: per adapter object
  600. * @log_info: log info
  601. *
  602. * Return nothing.
  603. */
  604. static void
  605. _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
  606. {
  607. union loginfo_type {
  608. u32 loginfo;
  609. struct {
  610. u32 subcode:16;
  611. u32 code:8;
  612. u32 originator:4;
  613. u32 bus_type:4;
  614. } dw;
  615. };
  616. union loginfo_type sas_loginfo;
  617. char *originator_str = NULL;
  618. sas_loginfo.loginfo = log_info;
  619. if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  620. return;
  621. /* each nexus loss loginfo */
  622. if (log_info == 0x31170000)
  623. return;
  624. /* eat the loginfos associated with task aborts */
  625. if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
  626. 0x31140000 || log_info == 0x31130000))
  627. return;
  628. switch (sas_loginfo.dw.originator) {
  629. case 0:
  630. originator_str = "IOP";
  631. break;
  632. case 1:
  633. originator_str = "PL";
  634. break;
  635. case 2:
  636. if (!ioc->hide_ir_msg)
  637. originator_str = "IR";
  638. else
  639. originator_str = "WarpDrive";
  640. break;
  641. }
  642. pr_warn(MPT3SAS_FMT
  643. "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
  644. ioc->name, log_info,
  645. originator_str, sas_loginfo.dw.code,
  646. sas_loginfo.dw.subcode);
  647. }
  648. /**
  649. * _base_display_reply_info -
  650. * @ioc: per adapter object
  651. * @smid: system request message index
  652. * @msix_index: MSIX table index supplied by the OS
  653. * @reply: reply message frame(lower 32bit addr)
  654. *
  655. * Return nothing.
  656. */
  657. static void
  658. _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  659. u32 reply)
  660. {
  661. MPI2DefaultReply_t *mpi_reply;
  662. u16 ioc_status;
  663. u32 loginfo = 0;
  664. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  665. if (unlikely(!mpi_reply)) {
  666. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  667. ioc->name, __FILE__, __LINE__, __func__);
  668. return;
  669. }
  670. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  671. if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
  672. (ioc->logging_level & MPT_DEBUG_REPLY)) {
  673. _base_sas_ioc_info(ioc , mpi_reply,
  674. mpt3sas_base_get_msg_frame(ioc, smid));
  675. }
  676. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
  677. loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
  678. _base_sas_log_info(ioc, loginfo);
  679. }
  680. if (ioc_status || loginfo) {
  681. ioc_status &= MPI2_IOCSTATUS_MASK;
  682. mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
  683. }
  684. }
  685. /**
  686. * mpt3sas_base_done - base internal command completion routine
  687. * @ioc: per adapter object
  688. * @smid: system request message index
  689. * @msix_index: MSIX table index supplied by the OS
  690. * @reply: reply message frame(lower 32bit addr)
  691. *
  692. * Return 1 meaning mf should be freed from _base_interrupt
  693. * 0 means the mf is freed from this function.
  694. */
  695. u8
  696. mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  697. u32 reply)
  698. {
  699. MPI2DefaultReply_t *mpi_reply;
  700. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  701. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  702. return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
  703. if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
  704. return 1;
  705. ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
  706. if (mpi_reply) {
  707. ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
  708. memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  709. }
  710. ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
  711. complete(&ioc->base_cmds.done);
  712. return 1;
  713. }
  714. /**
  715. * _base_async_event - main callback handler for firmware asyn events
  716. * @ioc: per adapter object
  717. * @msix_index: MSIX table index supplied by the OS
  718. * @reply: reply message frame(lower 32bit addr)
  719. *
  720. * Return 1 meaning mf should be freed from _base_interrupt
  721. * 0 means the mf is freed from this function.
  722. */
  723. static u8
  724. _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
  725. {
  726. Mpi2EventNotificationReply_t *mpi_reply;
  727. Mpi2EventAckRequest_t *ack_request;
  728. u16 smid;
  729. struct _event_ack_list *delayed_event_ack;
  730. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  731. if (!mpi_reply)
  732. return 1;
  733. if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
  734. return 1;
  735. _base_display_event_data(ioc, mpi_reply);
  736. if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
  737. goto out;
  738. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  739. if (!smid) {
  740. delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
  741. GFP_ATOMIC);
  742. if (!delayed_event_ack)
  743. goto out;
  744. INIT_LIST_HEAD(&delayed_event_ack->list);
  745. delayed_event_ack->Event = mpi_reply->Event;
  746. delayed_event_ack->EventContext = mpi_reply->EventContext;
  747. list_add_tail(&delayed_event_ack->list,
  748. &ioc->delayed_event_ack_list);
  749. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  750. "DELAYED: EVENT ACK: event (0x%04x)\n",
  751. ioc->name, le16_to_cpu(mpi_reply->Event)));
  752. goto out;
  753. }
  754. ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
  755. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  756. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  757. ack_request->Event = mpi_reply->Event;
  758. ack_request->EventContext = mpi_reply->EventContext;
  759. ack_request->VF_ID = 0; /* TODO */
  760. ack_request->VP_ID = 0;
  761. ioc->put_smid_default(ioc, smid);
  762. out:
  763. /* scsih callback handler */
  764. mpt3sas_scsih_event_callback(ioc, msix_index, reply);
  765. /* ctl callback handler */
  766. mpt3sas_ctl_event_callback(ioc, msix_index, reply);
  767. return 1;
  768. }
  769. /**
  770. * _base_get_cb_idx - obtain the callback index
  771. * @ioc: per adapter object
  772. * @smid: system request message index
  773. *
  774. * Return callback index.
  775. */
  776. static u8
  777. _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  778. {
  779. int i;
  780. u8 cb_idx;
  781. if (smid < ioc->hi_priority_smid) {
  782. i = smid - 1;
  783. cb_idx = ioc->scsi_lookup[i].cb_idx;
  784. } else if (smid < ioc->internal_smid) {
  785. i = smid - ioc->hi_priority_smid;
  786. cb_idx = ioc->hpr_lookup[i].cb_idx;
  787. } else if (smid <= ioc->hba_queue_depth) {
  788. i = smid - ioc->internal_smid;
  789. cb_idx = ioc->internal_lookup[i].cb_idx;
  790. } else
  791. cb_idx = 0xFF;
  792. return cb_idx;
  793. }
  794. /**
  795. * _base_mask_interrupts - disable interrupts
  796. * @ioc: per adapter object
  797. *
  798. * Disabling ResetIRQ, Reply and Doorbell Interrupts
  799. *
  800. * Return nothing.
  801. */
  802. static void
  803. _base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  804. {
  805. u32 him_register;
  806. ioc->mask_interrupts = 1;
  807. him_register = readl(&ioc->chip->HostInterruptMask);
  808. him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
  809. writel(him_register, &ioc->chip->HostInterruptMask);
  810. readl(&ioc->chip->HostInterruptMask);
  811. }
  812. /**
  813. * _base_unmask_interrupts - enable interrupts
  814. * @ioc: per adapter object
  815. *
  816. * Enabling only Reply Interrupts
  817. *
  818. * Return nothing.
  819. */
  820. static void
  821. _base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  822. {
  823. u32 him_register;
  824. him_register = readl(&ioc->chip->HostInterruptMask);
  825. him_register &= ~MPI2_HIM_RIM;
  826. writel(him_register, &ioc->chip->HostInterruptMask);
  827. ioc->mask_interrupts = 0;
  828. }
  829. union reply_descriptor {
  830. u64 word;
  831. struct {
  832. u32 low;
  833. u32 high;
  834. } u;
  835. };
  836. /**
  837. * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
  838. * @irq: irq number (not used)
  839. * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
  840. * @r: pt_regs pointer (not used)
  841. *
  842. * Return IRQ_HANDLE if processed, else IRQ_NONE.
  843. */
  844. static irqreturn_t
  845. _base_interrupt(int irq, void *bus_id)
  846. {
  847. struct adapter_reply_queue *reply_q = bus_id;
  848. union reply_descriptor rd;
  849. u32 completed_cmds;
  850. u8 request_desript_type;
  851. u16 smid;
  852. u8 cb_idx;
  853. u32 reply;
  854. u8 msix_index = reply_q->msix_index;
  855. struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
  856. Mpi2ReplyDescriptorsUnion_t *rpf;
  857. u8 rc;
  858. if (ioc->mask_interrupts)
  859. return IRQ_NONE;
  860. if (!atomic_add_unless(&reply_q->busy, 1, 1))
  861. return IRQ_NONE;
  862. rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
  863. request_desript_type = rpf->Default.ReplyFlags
  864. & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  865. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
  866. atomic_dec(&reply_q->busy);
  867. return IRQ_NONE;
  868. }
  869. completed_cmds = 0;
  870. cb_idx = 0xFF;
  871. do {
  872. rd.word = le64_to_cpu(rpf->Words);
  873. if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
  874. goto out;
  875. reply = 0;
  876. smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
  877. if (request_desript_type ==
  878. MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
  879. request_desript_type ==
  880. MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
  881. cb_idx = _base_get_cb_idx(ioc, smid);
  882. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  883. (likely(mpt_callbacks[cb_idx] != NULL))) {
  884. rc = mpt_callbacks[cb_idx](ioc, smid,
  885. msix_index, 0);
  886. if (rc)
  887. mpt3sas_base_free_smid(ioc, smid);
  888. }
  889. } else if (request_desript_type ==
  890. MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
  891. reply = le32_to_cpu(
  892. rpf->AddressReply.ReplyFrameAddress);
  893. if (reply > ioc->reply_dma_max_address ||
  894. reply < ioc->reply_dma_min_address)
  895. reply = 0;
  896. if (smid) {
  897. cb_idx = _base_get_cb_idx(ioc, smid);
  898. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  899. (likely(mpt_callbacks[cb_idx] != NULL))) {
  900. rc = mpt_callbacks[cb_idx](ioc, smid,
  901. msix_index, reply);
  902. if (reply)
  903. _base_display_reply_info(ioc,
  904. smid, msix_index, reply);
  905. if (rc)
  906. mpt3sas_base_free_smid(ioc,
  907. smid);
  908. }
  909. } else {
  910. _base_async_event(ioc, msix_index, reply);
  911. }
  912. /* reply free queue handling */
  913. if (reply) {
  914. ioc->reply_free_host_index =
  915. (ioc->reply_free_host_index ==
  916. (ioc->reply_free_queue_depth - 1)) ?
  917. 0 : ioc->reply_free_host_index + 1;
  918. ioc->reply_free[ioc->reply_free_host_index] =
  919. cpu_to_le32(reply);
  920. wmb();
  921. writel(ioc->reply_free_host_index,
  922. &ioc->chip->ReplyFreeHostIndex);
  923. }
  924. }
  925. rpf->Words = cpu_to_le64(ULLONG_MAX);
  926. reply_q->reply_post_host_index =
  927. (reply_q->reply_post_host_index ==
  928. (ioc->reply_post_queue_depth - 1)) ? 0 :
  929. reply_q->reply_post_host_index + 1;
  930. request_desript_type =
  931. reply_q->reply_post_free[reply_q->reply_post_host_index].
  932. Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  933. completed_cmds++;
  934. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  935. goto out;
  936. if (!reply_q->reply_post_host_index)
  937. rpf = reply_q->reply_post_free;
  938. else
  939. rpf++;
  940. } while (1);
  941. out:
  942. if (!completed_cmds) {
  943. atomic_dec(&reply_q->busy);
  944. return IRQ_NONE;
  945. }
  946. wmb();
  947. if (ioc->is_warpdrive) {
  948. writel(reply_q->reply_post_host_index,
  949. ioc->reply_post_host_index[msix_index]);
  950. atomic_dec(&reply_q->busy);
  951. return IRQ_HANDLED;
  952. }
  953. /* Update Reply Post Host Index.
  954. * For those HBA's which support combined reply queue feature
  955. * 1. Get the correct Supplemental Reply Post Host Index Register.
  956. * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
  957. * Index Register address bank i.e replyPostRegisterIndex[],
  958. * 2. Then update this register with new reply host index value
  959. * in ReplyPostIndex field and the MSIxIndex field with
  960. * msix_index value reduced to a value between 0 and 7,
  961. * using a modulo 8 operation. Since each Supplemental Reply Post
  962. * Host Index Register supports 8 MSI-X vectors.
  963. *
  964. * For other HBA's just update the Reply Post Host Index register with
  965. * new reply host index value in ReplyPostIndex Field and msix_index
  966. * value in MSIxIndex field.
  967. */
  968. if (ioc->combined_reply_queue)
  969. writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
  970. MPI2_RPHI_MSIX_INDEX_SHIFT),
  971. ioc->replyPostRegisterIndex[msix_index/8]);
  972. else
  973. writel(reply_q->reply_post_host_index | (msix_index <<
  974. MPI2_RPHI_MSIX_INDEX_SHIFT),
  975. &ioc->chip->ReplyPostHostIndex);
  976. atomic_dec(&reply_q->busy);
  977. return IRQ_HANDLED;
  978. }
  979. /**
  980. * _base_is_controller_msix_enabled - is controller support muli-reply queues
  981. * @ioc: per adapter object
  982. *
  983. */
  984. static inline int
  985. _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
  986. {
  987. return (ioc->facts.IOCCapabilities &
  988. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
  989. }
  990. /**
  991. * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
  992. * @ioc: per adapter object
  993. * Context: non ISR conext
  994. *
  995. * Called when a Task Management request has completed.
  996. *
  997. * Return nothing.
  998. */
  999. void
  1000. mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc)
  1001. {
  1002. struct adapter_reply_queue *reply_q;
  1003. /* If MSIX capability is turned off
  1004. * then multi-queues are not enabled
  1005. */
  1006. if (!_base_is_controller_msix_enabled(ioc))
  1007. return;
  1008. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  1009. if (ioc->shost_recovery || ioc->remove_host ||
  1010. ioc->pci_error_recovery)
  1011. return;
  1012. /* TMs are on msix_index == 0 */
  1013. if (reply_q->msix_index == 0)
  1014. continue;
  1015. synchronize_irq(reply_q->vector);
  1016. }
  1017. }
  1018. /**
  1019. * mpt3sas_base_release_callback_handler - clear interrupt callback handler
  1020. * @cb_idx: callback index
  1021. *
  1022. * Return nothing.
  1023. */
  1024. void
  1025. mpt3sas_base_release_callback_handler(u8 cb_idx)
  1026. {
  1027. mpt_callbacks[cb_idx] = NULL;
  1028. }
  1029. /**
  1030. * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
  1031. * @cb_func: callback function
  1032. *
  1033. * Returns cb_func.
  1034. */
  1035. u8
  1036. mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
  1037. {
  1038. u8 cb_idx;
  1039. for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  1040. if (mpt_callbacks[cb_idx] == NULL)
  1041. break;
  1042. mpt_callbacks[cb_idx] = cb_func;
  1043. return cb_idx;
  1044. }
  1045. /**
  1046. * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
  1047. *
  1048. * Return nothing.
  1049. */
  1050. void
  1051. mpt3sas_base_initialize_callback_handler(void)
  1052. {
  1053. u8 cb_idx;
  1054. for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  1055. mpt3sas_base_release_callback_handler(cb_idx);
  1056. }
  1057. /**
  1058. * _base_build_zero_len_sge - build zero length sg entry
  1059. * @ioc: per adapter object
  1060. * @paddr: virtual address for SGE
  1061. *
  1062. * Create a zero length scatter gather entry to insure the IOCs hardware has
  1063. * something to use if the target device goes brain dead and tries
  1064. * to send data even when none is asked for.
  1065. *
  1066. * Return nothing.
  1067. */
  1068. static void
  1069. _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  1070. {
  1071. u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
  1072. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
  1073. MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
  1074. MPI2_SGE_FLAGS_SHIFT);
  1075. ioc->base_add_sg_single(paddr, flags_length, -1);
  1076. }
  1077. /**
  1078. * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
  1079. * @paddr: virtual address for SGE
  1080. * @flags_length: SGE flags and data transfer length
  1081. * @dma_addr: Physical address
  1082. *
  1083. * Return nothing.
  1084. */
  1085. static void
  1086. _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1087. {
  1088. Mpi2SGESimple32_t *sgel = paddr;
  1089. flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
  1090. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1091. sgel->FlagsLength = cpu_to_le32(flags_length);
  1092. sgel->Address = cpu_to_le32(dma_addr);
  1093. }
  1094. /**
  1095. * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
  1096. * @paddr: virtual address for SGE
  1097. * @flags_length: SGE flags and data transfer length
  1098. * @dma_addr: Physical address
  1099. *
  1100. * Return nothing.
  1101. */
  1102. static void
  1103. _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1104. {
  1105. Mpi2SGESimple64_t *sgel = paddr;
  1106. flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
  1107. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1108. sgel->FlagsLength = cpu_to_le32(flags_length);
  1109. sgel->Address = cpu_to_le64(dma_addr);
  1110. }
  1111. /**
  1112. * _base_get_chain_buffer_tracker - obtain chain tracker
  1113. * @ioc: per adapter object
  1114. * @smid: smid associated to an IO request
  1115. *
  1116. * Returns chain tracker(from ioc->free_chain_list)
  1117. */
  1118. static struct chain_tracker *
  1119. _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1120. {
  1121. struct chain_tracker *chain_req;
  1122. unsigned long flags;
  1123. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1124. if (list_empty(&ioc->free_chain_list)) {
  1125. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1126. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1127. "chain buffers not available\n", ioc->name));
  1128. return NULL;
  1129. }
  1130. chain_req = list_entry(ioc->free_chain_list.next,
  1131. struct chain_tracker, tracker_list);
  1132. list_del_init(&chain_req->tracker_list);
  1133. list_add_tail(&chain_req->tracker_list,
  1134. &ioc->scsi_lookup[smid - 1].chain_list);
  1135. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1136. return chain_req;
  1137. }
  1138. /**
  1139. * _base_build_sg - build generic sg
  1140. * @ioc: per adapter object
  1141. * @psge: virtual address for SGE
  1142. * @data_out_dma: physical address for WRITES
  1143. * @data_out_sz: data xfer size for WRITES
  1144. * @data_in_dma: physical address for READS
  1145. * @data_in_sz: data xfer size for READS
  1146. *
  1147. * Return nothing.
  1148. */
  1149. static void
  1150. _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
  1151. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  1152. size_t data_in_sz)
  1153. {
  1154. u32 sgl_flags;
  1155. if (!data_out_sz && !data_in_sz) {
  1156. _base_build_zero_len_sge(ioc, psge);
  1157. return;
  1158. }
  1159. if (data_out_sz && data_in_sz) {
  1160. /* WRITE sgel first */
  1161. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1162. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1163. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1164. ioc->base_add_sg_single(psge, sgl_flags |
  1165. data_out_sz, data_out_dma);
  1166. /* incr sgel */
  1167. psge += ioc->sge_size;
  1168. /* READ sgel last */
  1169. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1170. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1171. MPI2_SGE_FLAGS_END_OF_LIST);
  1172. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1173. ioc->base_add_sg_single(psge, sgl_flags |
  1174. data_in_sz, data_in_dma);
  1175. } else if (data_out_sz) /* WRITE */ {
  1176. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1177. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1178. MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1179. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1180. ioc->base_add_sg_single(psge, sgl_flags |
  1181. data_out_sz, data_out_dma);
  1182. } else if (data_in_sz) /* READ */ {
  1183. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1184. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1185. MPI2_SGE_FLAGS_END_OF_LIST);
  1186. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1187. ioc->base_add_sg_single(psge, sgl_flags |
  1188. data_in_sz, data_in_dma);
  1189. }
  1190. }
  1191. /* IEEE format sgls */
  1192. /**
  1193. * _base_add_sg_single_ieee - add sg element for IEEE format
  1194. * @paddr: virtual address for SGE
  1195. * @flags: SGE flags
  1196. * @chain_offset: number of 128 byte elements from start of segment
  1197. * @length: data transfer length
  1198. * @dma_addr: Physical address
  1199. *
  1200. * Return nothing.
  1201. */
  1202. static void
  1203. _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
  1204. dma_addr_t dma_addr)
  1205. {
  1206. Mpi25IeeeSgeChain64_t *sgel = paddr;
  1207. sgel->Flags = flags;
  1208. sgel->NextChainOffset = chain_offset;
  1209. sgel->Length = cpu_to_le32(length);
  1210. sgel->Address = cpu_to_le64(dma_addr);
  1211. }
  1212. /**
  1213. * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
  1214. * @ioc: per adapter object
  1215. * @paddr: virtual address for SGE
  1216. *
  1217. * Create a zero length scatter gather entry to insure the IOCs hardware has
  1218. * something to use if the target device goes brain dead and tries
  1219. * to send data even when none is asked for.
  1220. *
  1221. * Return nothing.
  1222. */
  1223. static void
  1224. _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  1225. {
  1226. u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1227. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
  1228. MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
  1229. _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
  1230. }
  1231. /**
  1232. * _base_build_sg_scmd - main sg creation routine
  1233. * @ioc: per adapter object
  1234. * @scmd: scsi command
  1235. * @smid: system request message index
  1236. * Context: none.
  1237. *
  1238. * The main routine that builds scatter gather table from a given
  1239. * scsi request sent via the .queuecommand main handler.
  1240. *
  1241. * Returns 0 success, anything else error
  1242. */
  1243. static int
  1244. _base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
  1245. struct scsi_cmnd *scmd, u16 smid)
  1246. {
  1247. Mpi2SCSIIORequest_t *mpi_request;
  1248. dma_addr_t chain_dma;
  1249. struct scatterlist *sg_scmd;
  1250. void *sg_local, *chain;
  1251. u32 chain_offset;
  1252. u32 chain_length;
  1253. u32 chain_flags;
  1254. int sges_left;
  1255. u32 sges_in_segment;
  1256. u32 sgl_flags;
  1257. u32 sgl_flags_last_element;
  1258. u32 sgl_flags_end_buffer;
  1259. struct chain_tracker *chain_req;
  1260. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1261. /* init scatter gather flags */
  1262. sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
  1263. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  1264. sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
  1265. sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
  1266. << MPI2_SGE_FLAGS_SHIFT;
  1267. sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
  1268. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
  1269. << MPI2_SGE_FLAGS_SHIFT;
  1270. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1271. sg_scmd = scsi_sglist(scmd);
  1272. sges_left = scsi_dma_map(scmd);
  1273. if (sges_left < 0) {
  1274. sdev_printk(KERN_ERR, scmd->device,
  1275. "pci_map_sg failed: request for %d bytes!\n",
  1276. scsi_bufflen(scmd));
  1277. return -ENOMEM;
  1278. }
  1279. sg_local = &mpi_request->SGL;
  1280. sges_in_segment = ioc->max_sges_in_main_message;
  1281. if (sges_left <= sges_in_segment)
  1282. goto fill_in_last_segment;
  1283. mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
  1284. (sges_in_segment * ioc->sge_size))/4;
  1285. /* fill in main message segment when there is a chain following */
  1286. while (sges_in_segment) {
  1287. if (sges_in_segment == 1)
  1288. ioc->base_add_sg_single(sg_local,
  1289. sgl_flags_last_element | sg_dma_len(sg_scmd),
  1290. sg_dma_address(sg_scmd));
  1291. else
  1292. ioc->base_add_sg_single(sg_local, sgl_flags |
  1293. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1294. sg_scmd = sg_next(sg_scmd);
  1295. sg_local += ioc->sge_size;
  1296. sges_left--;
  1297. sges_in_segment--;
  1298. }
  1299. /* initializing the chain flags and pointers */
  1300. chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
  1301. chain_req = _base_get_chain_buffer_tracker(ioc, smid);
  1302. if (!chain_req)
  1303. return -1;
  1304. chain = chain_req->chain_buffer;
  1305. chain_dma = chain_req->chain_buffer_dma;
  1306. do {
  1307. sges_in_segment = (sges_left <=
  1308. ioc->max_sges_in_chain_message) ? sges_left :
  1309. ioc->max_sges_in_chain_message;
  1310. chain_offset = (sges_left == sges_in_segment) ?
  1311. 0 : (sges_in_segment * ioc->sge_size)/4;
  1312. chain_length = sges_in_segment * ioc->sge_size;
  1313. if (chain_offset) {
  1314. chain_offset = chain_offset <<
  1315. MPI2_SGE_CHAIN_OFFSET_SHIFT;
  1316. chain_length += ioc->sge_size;
  1317. }
  1318. ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
  1319. chain_length, chain_dma);
  1320. sg_local = chain;
  1321. if (!chain_offset)
  1322. goto fill_in_last_segment;
  1323. /* fill in chain segments */
  1324. while (sges_in_segment) {
  1325. if (sges_in_segment == 1)
  1326. ioc->base_add_sg_single(sg_local,
  1327. sgl_flags_last_element |
  1328. sg_dma_len(sg_scmd),
  1329. sg_dma_address(sg_scmd));
  1330. else
  1331. ioc->base_add_sg_single(sg_local, sgl_flags |
  1332. sg_dma_len(sg_scmd),
  1333. sg_dma_address(sg_scmd));
  1334. sg_scmd = sg_next(sg_scmd);
  1335. sg_local += ioc->sge_size;
  1336. sges_left--;
  1337. sges_in_segment--;
  1338. }
  1339. chain_req = _base_get_chain_buffer_tracker(ioc, smid);
  1340. if (!chain_req)
  1341. return -1;
  1342. chain = chain_req->chain_buffer;
  1343. chain_dma = chain_req->chain_buffer_dma;
  1344. } while (1);
  1345. fill_in_last_segment:
  1346. /* fill the last segment */
  1347. while (sges_left) {
  1348. if (sges_left == 1)
  1349. ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
  1350. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1351. else
  1352. ioc->base_add_sg_single(sg_local, sgl_flags |
  1353. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1354. sg_scmd = sg_next(sg_scmd);
  1355. sg_local += ioc->sge_size;
  1356. sges_left--;
  1357. }
  1358. return 0;
  1359. }
  1360. /**
  1361. * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
  1362. * @ioc: per adapter object
  1363. * @scmd: scsi command
  1364. * @smid: system request message index
  1365. * Context: none.
  1366. *
  1367. * The main routine that builds scatter gather table from a given
  1368. * scsi request sent via the .queuecommand main handler.
  1369. *
  1370. * Returns 0 success, anything else error
  1371. */
  1372. static int
  1373. _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
  1374. struct scsi_cmnd *scmd, u16 smid)
  1375. {
  1376. Mpi2SCSIIORequest_t *mpi_request;
  1377. dma_addr_t chain_dma;
  1378. struct scatterlist *sg_scmd;
  1379. void *sg_local, *chain;
  1380. u32 chain_offset;
  1381. u32 chain_length;
  1382. int sges_left;
  1383. u32 sges_in_segment;
  1384. u8 simple_sgl_flags;
  1385. u8 simple_sgl_flags_last;
  1386. u8 chain_sgl_flags;
  1387. struct chain_tracker *chain_req;
  1388. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1389. /* init scatter gather flags */
  1390. simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1391. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1392. simple_sgl_flags_last = simple_sgl_flags |
  1393. MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  1394. chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1395. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1396. sg_scmd = scsi_sglist(scmd);
  1397. sges_left = scsi_dma_map(scmd);
  1398. if (sges_left < 0) {
  1399. sdev_printk(KERN_ERR, scmd->device,
  1400. "pci_map_sg failed: request for %d bytes!\n",
  1401. scsi_bufflen(scmd));
  1402. return -ENOMEM;
  1403. }
  1404. sg_local = &mpi_request->SGL;
  1405. sges_in_segment = (ioc->request_sz -
  1406. offsetof(Mpi2SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
  1407. if (sges_left <= sges_in_segment)
  1408. goto fill_in_last_segment;
  1409. mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
  1410. (offsetof(Mpi2SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
  1411. /* fill in main message segment when there is a chain following */
  1412. while (sges_in_segment > 1) {
  1413. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  1414. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1415. sg_scmd = sg_next(sg_scmd);
  1416. sg_local += ioc->sge_size_ieee;
  1417. sges_left--;
  1418. sges_in_segment--;
  1419. }
  1420. /* initializing the pointers */
  1421. chain_req = _base_get_chain_buffer_tracker(ioc, smid);
  1422. if (!chain_req)
  1423. return -1;
  1424. chain = chain_req->chain_buffer;
  1425. chain_dma = chain_req->chain_buffer_dma;
  1426. do {
  1427. sges_in_segment = (sges_left <=
  1428. ioc->max_sges_in_chain_message) ? sges_left :
  1429. ioc->max_sges_in_chain_message;
  1430. chain_offset = (sges_left == sges_in_segment) ?
  1431. 0 : sges_in_segment;
  1432. chain_length = sges_in_segment * ioc->sge_size_ieee;
  1433. if (chain_offset)
  1434. chain_length += ioc->sge_size_ieee;
  1435. _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
  1436. chain_offset, chain_length, chain_dma);
  1437. sg_local = chain;
  1438. if (!chain_offset)
  1439. goto fill_in_last_segment;
  1440. /* fill in chain segments */
  1441. while (sges_in_segment) {
  1442. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  1443. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1444. sg_scmd = sg_next(sg_scmd);
  1445. sg_local += ioc->sge_size_ieee;
  1446. sges_left--;
  1447. sges_in_segment--;
  1448. }
  1449. chain_req = _base_get_chain_buffer_tracker(ioc, smid);
  1450. if (!chain_req)
  1451. return -1;
  1452. chain = chain_req->chain_buffer;
  1453. chain_dma = chain_req->chain_buffer_dma;
  1454. } while (1);
  1455. fill_in_last_segment:
  1456. /* fill the last segment */
  1457. while (sges_left > 0) {
  1458. if (sges_left == 1)
  1459. _base_add_sg_single_ieee(sg_local,
  1460. simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
  1461. sg_dma_address(sg_scmd));
  1462. else
  1463. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  1464. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1465. sg_scmd = sg_next(sg_scmd);
  1466. sg_local += ioc->sge_size_ieee;
  1467. sges_left--;
  1468. }
  1469. return 0;
  1470. }
  1471. /**
  1472. * _base_build_sg_ieee - build generic sg for IEEE format
  1473. * @ioc: per adapter object
  1474. * @psge: virtual address for SGE
  1475. * @data_out_dma: physical address for WRITES
  1476. * @data_out_sz: data xfer size for WRITES
  1477. * @data_in_dma: physical address for READS
  1478. * @data_in_sz: data xfer size for READS
  1479. *
  1480. * Return nothing.
  1481. */
  1482. static void
  1483. _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
  1484. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  1485. size_t data_in_sz)
  1486. {
  1487. u8 sgl_flags;
  1488. if (!data_out_sz && !data_in_sz) {
  1489. _base_build_zero_len_sge_ieee(ioc, psge);
  1490. return;
  1491. }
  1492. if (data_out_sz && data_in_sz) {
  1493. /* WRITE sgel first */
  1494. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1495. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1496. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  1497. data_out_dma);
  1498. /* incr sgel */
  1499. psge += ioc->sge_size_ieee;
  1500. /* READ sgel last */
  1501. sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  1502. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  1503. data_in_dma);
  1504. } else if (data_out_sz) /* WRITE */ {
  1505. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1506. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  1507. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1508. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  1509. data_out_dma);
  1510. } else if (data_in_sz) /* READ */ {
  1511. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1512. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  1513. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1514. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  1515. data_in_dma);
  1516. }
  1517. }
  1518. #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
  1519. /**
  1520. * _base_config_dma_addressing - set dma addressing
  1521. * @ioc: per adapter object
  1522. * @pdev: PCI device struct
  1523. *
  1524. * Returns 0 for success, non-zero for failure.
  1525. */
  1526. static int
  1527. _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
  1528. {
  1529. struct sysinfo s;
  1530. u64 consistent_dma_mask;
  1531. if (ioc->dma_mask)
  1532. consistent_dma_mask = DMA_BIT_MASK(64);
  1533. else
  1534. consistent_dma_mask = DMA_BIT_MASK(32);
  1535. if (sizeof(dma_addr_t) > 4) {
  1536. const uint64_t required_mask =
  1537. dma_get_required_mask(&pdev->dev);
  1538. if ((required_mask > DMA_BIT_MASK(32)) &&
  1539. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
  1540. !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
  1541. ioc->base_add_sg_single = &_base_add_sg_single_64;
  1542. ioc->sge_size = sizeof(Mpi2SGESimple64_t);
  1543. ioc->dma_mask = 64;
  1544. goto out;
  1545. }
  1546. }
  1547. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  1548. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1549. ioc->base_add_sg_single = &_base_add_sg_single_32;
  1550. ioc->sge_size = sizeof(Mpi2SGESimple32_t);
  1551. ioc->dma_mask = 32;
  1552. } else
  1553. return -ENODEV;
  1554. out:
  1555. si_meminfo(&s);
  1556. pr_info(MPT3SAS_FMT
  1557. "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
  1558. ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
  1559. return 0;
  1560. }
  1561. static int
  1562. _base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
  1563. struct pci_dev *pdev)
  1564. {
  1565. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
  1566. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  1567. return -ENODEV;
  1568. }
  1569. return 0;
  1570. }
  1571. /**
  1572. * _base_check_enable_msix - checks MSIX capabable.
  1573. * @ioc: per adapter object
  1574. *
  1575. * Check to see if card is capable of MSIX, and set number
  1576. * of available msix vectors
  1577. */
  1578. static int
  1579. _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  1580. {
  1581. int base;
  1582. u16 message_control;
  1583. /* Check whether controller SAS2008 B0 controller,
  1584. * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
  1585. */
  1586. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
  1587. ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
  1588. return -EINVAL;
  1589. }
  1590. base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
  1591. if (!base) {
  1592. dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
  1593. ioc->name));
  1594. return -EINVAL;
  1595. }
  1596. /* get msix vector count */
  1597. /* NUMA_IO not supported for older controllers */
  1598. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
  1599. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
  1600. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
  1601. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
  1602. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
  1603. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
  1604. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
  1605. ioc->msix_vector_count = 1;
  1606. else {
  1607. pci_read_config_word(ioc->pdev, base + 2, &message_control);
  1608. ioc->msix_vector_count = (message_control & 0x3FF) + 1;
  1609. }
  1610. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  1611. "msix is supported, vector_count(%d)\n",
  1612. ioc->name, ioc->msix_vector_count));
  1613. return 0;
  1614. }
  1615. /**
  1616. * _base_free_irq - free irq
  1617. * @ioc: per adapter object
  1618. *
  1619. * Freeing respective reply_queue from the list.
  1620. */
  1621. static void
  1622. _base_free_irq(struct MPT3SAS_ADAPTER *ioc)
  1623. {
  1624. struct adapter_reply_queue *reply_q, *next;
  1625. if (list_empty(&ioc->reply_queue_list))
  1626. return;
  1627. list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
  1628. list_del(&reply_q->list);
  1629. if (smp_affinity_enable) {
  1630. irq_set_affinity_hint(reply_q->vector, NULL);
  1631. free_cpumask_var(reply_q->affinity_hint);
  1632. }
  1633. free_irq(reply_q->vector, reply_q);
  1634. kfree(reply_q);
  1635. }
  1636. }
  1637. /**
  1638. * _base_request_irq - request irq
  1639. * @ioc: per adapter object
  1640. * @index: msix index into vector table
  1641. * @vector: irq vector
  1642. *
  1643. * Inserting respective reply_queue into the list.
  1644. */
  1645. static int
  1646. _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index, u32 vector)
  1647. {
  1648. struct adapter_reply_queue *reply_q;
  1649. int r;
  1650. reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
  1651. if (!reply_q) {
  1652. pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
  1653. ioc->name, (int)sizeof(struct adapter_reply_queue));
  1654. return -ENOMEM;
  1655. }
  1656. reply_q->ioc = ioc;
  1657. reply_q->msix_index = index;
  1658. reply_q->vector = vector;
  1659. if (smp_affinity_enable) {
  1660. if (!zalloc_cpumask_var(&reply_q->affinity_hint, GFP_KERNEL)) {
  1661. kfree(reply_q);
  1662. return -ENOMEM;
  1663. }
  1664. }
  1665. atomic_set(&reply_q->busy, 0);
  1666. if (ioc->msix_enable)
  1667. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
  1668. ioc->driver_name, ioc->id, index);
  1669. else
  1670. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
  1671. ioc->driver_name, ioc->id);
  1672. r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
  1673. reply_q);
  1674. if (r) {
  1675. pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
  1676. reply_q->name, vector);
  1677. free_cpumask_var(reply_q->affinity_hint);
  1678. kfree(reply_q);
  1679. return -EBUSY;
  1680. }
  1681. INIT_LIST_HEAD(&reply_q->list);
  1682. list_add_tail(&reply_q->list, &ioc->reply_queue_list);
  1683. return 0;
  1684. }
  1685. /**
  1686. * _base_assign_reply_queues - assigning msix index for each cpu
  1687. * @ioc: per adapter object
  1688. *
  1689. * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
  1690. *
  1691. * It would nice if we could call irq_set_affinity, however it is not
  1692. * an exported symbol
  1693. */
  1694. static void
  1695. _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
  1696. {
  1697. unsigned int cpu, nr_cpus, nr_msix, index = 0;
  1698. struct adapter_reply_queue *reply_q;
  1699. if (!_base_is_controller_msix_enabled(ioc))
  1700. return;
  1701. memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
  1702. nr_cpus = num_online_cpus();
  1703. nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
  1704. ioc->facts.MaxMSIxVectors);
  1705. if (!nr_msix)
  1706. return;
  1707. cpu = cpumask_first(cpu_online_mask);
  1708. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  1709. unsigned int i, group = nr_cpus / nr_msix;
  1710. if (cpu >= nr_cpus)
  1711. break;
  1712. if (index < nr_cpus % nr_msix)
  1713. group++;
  1714. for (i = 0 ; i < group ; i++) {
  1715. ioc->cpu_msix_table[cpu] = index;
  1716. if (smp_affinity_enable)
  1717. cpumask_or(reply_q->affinity_hint,
  1718. reply_q->affinity_hint, get_cpu_mask(cpu));
  1719. cpu = cpumask_next(cpu, cpu_online_mask);
  1720. }
  1721. if (smp_affinity_enable)
  1722. if (irq_set_affinity_hint(reply_q->vector,
  1723. reply_q->affinity_hint))
  1724. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  1725. "Err setting affinity hint to irq vector %d\n",
  1726. ioc->name, reply_q->vector));
  1727. index++;
  1728. }
  1729. }
  1730. /**
  1731. * _base_disable_msix - disables msix
  1732. * @ioc: per adapter object
  1733. *
  1734. */
  1735. static void
  1736. _base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
  1737. {
  1738. if (!ioc->msix_enable)
  1739. return;
  1740. pci_disable_msix(ioc->pdev);
  1741. ioc->msix_enable = 0;
  1742. }
  1743. /**
  1744. * _base_enable_msix - enables msix, failback to io_apic
  1745. * @ioc: per adapter object
  1746. *
  1747. */
  1748. static int
  1749. _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  1750. {
  1751. struct msix_entry *entries, *a;
  1752. int r;
  1753. int i, local_max_msix_vectors;
  1754. u8 try_msix = 0;
  1755. if (msix_disable == -1 || msix_disable == 0)
  1756. try_msix = 1;
  1757. if (!try_msix)
  1758. goto try_ioapic;
  1759. if (_base_check_enable_msix(ioc) != 0)
  1760. goto try_ioapic;
  1761. ioc->reply_queue_count = min_t(int, ioc->cpu_count,
  1762. ioc->msix_vector_count);
  1763. printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
  1764. ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
  1765. ioc->cpu_count, max_msix_vectors);
  1766. if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
  1767. local_max_msix_vectors = 8;
  1768. else
  1769. local_max_msix_vectors = max_msix_vectors;
  1770. if (local_max_msix_vectors > 0) {
  1771. ioc->reply_queue_count = min_t(int, local_max_msix_vectors,
  1772. ioc->reply_queue_count);
  1773. ioc->msix_vector_count = ioc->reply_queue_count;
  1774. } else if (local_max_msix_vectors == 0)
  1775. goto try_ioapic;
  1776. if (ioc->msix_vector_count < ioc->cpu_count)
  1777. smp_affinity_enable = 0;
  1778. entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
  1779. GFP_KERNEL);
  1780. if (!entries) {
  1781. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  1782. "kcalloc failed @ at %s:%d/%s() !!!\n",
  1783. ioc->name, __FILE__, __LINE__, __func__));
  1784. goto try_ioapic;
  1785. }
  1786. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
  1787. a->entry = i;
  1788. r = pci_enable_msix_exact(ioc->pdev, entries, ioc->reply_queue_count);
  1789. if (r) {
  1790. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  1791. "pci_enable_msix_exact failed (r=%d) !!!\n",
  1792. ioc->name, r));
  1793. kfree(entries);
  1794. goto try_ioapic;
  1795. }
  1796. ioc->msix_enable = 1;
  1797. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
  1798. r = _base_request_irq(ioc, i, a->vector);
  1799. if (r) {
  1800. _base_free_irq(ioc);
  1801. _base_disable_msix(ioc);
  1802. kfree(entries);
  1803. goto try_ioapic;
  1804. }
  1805. }
  1806. kfree(entries);
  1807. return 0;
  1808. /* failback to io_apic interrupt routing */
  1809. try_ioapic:
  1810. ioc->reply_queue_count = 1;
  1811. r = _base_request_irq(ioc, 0, ioc->pdev->irq);
  1812. return r;
  1813. }
  1814. /**
  1815. * mpt3sas_base_unmap_resources - free controller resources
  1816. * @ioc: per adapter object
  1817. */
  1818. static void
  1819. mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
  1820. {
  1821. struct pci_dev *pdev = ioc->pdev;
  1822. dexitprintk(ioc, printk(MPT3SAS_FMT "%s\n",
  1823. ioc->name, __func__));
  1824. _base_free_irq(ioc);
  1825. _base_disable_msix(ioc);
  1826. if (ioc->combined_reply_queue) {
  1827. kfree(ioc->replyPostRegisterIndex);
  1828. ioc->replyPostRegisterIndex = NULL;
  1829. }
  1830. if (ioc->chip_phys) {
  1831. iounmap(ioc->chip);
  1832. ioc->chip_phys = 0;
  1833. }
  1834. if (pci_is_enabled(pdev)) {
  1835. pci_release_selected_regions(ioc->pdev, ioc->bars);
  1836. pci_disable_pcie_error_reporting(pdev);
  1837. pci_disable_device(pdev);
  1838. }
  1839. }
  1840. /**
  1841. * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
  1842. * @ioc: per adapter object
  1843. *
  1844. * Returns 0 for success, non-zero for failure.
  1845. */
  1846. int
  1847. mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
  1848. {
  1849. struct pci_dev *pdev = ioc->pdev;
  1850. u32 memap_sz;
  1851. u32 pio_sz;
  1852. int i, r = 0;
  1853. u64 pio_chip = 0;
  1854. u64 chip_phys = 0;
  1855. struct adapter_reply_queue *reply_q;
  1856. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
  1857. ioc->name, __func__));
  1858. ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1859. if (pci_enable_device_mem(pdev)) {
  1860. pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
  1861. ioc->name);
  1862. ioc->bars = 0;
  1863. return -ENODEV;
  1864. }
  1865. if (pci_request_selected_regions(pdev, ioc->bars,
  1866. ioc->driver_name)) {
  1867. pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
  1868. ioc->name);
  1869. ioc->bars = 0;
  1870. r = -ENODEV;
  1871. goto out_fail;
  1872. }
  1873. /* AER (Advanced Error Reporting) hooks */
  1874. pci_enable_pcie_error_reporting(pdev);
  1875. pci_set_master(pdev);
  1876. if (_base_config_dma_addressing(ioc, pdev) != 0) {
  1877. pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
  1878. ioc->name, pci_name(pdev));
  1879. r = -ENODEV;
  1880. goto out_fail;
  1881. }
  1882. for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
  1883. (!memap_sz || !pio_sz); i++) {
  1884. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  1885. if (pio_sz)
  1886. continue;
  1887. pio_chip = (u64)pci_resource_start(pdev, i);
  1888. pio_sz = pci_resource_len(pdev, i);
  1889. } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  1890. if (memap_sz)
  1891. continue;
  1892. ioc->chip_phys = pci_resource_start(pdev, i);
  1893. chip_phys = (u64)ioc->chip_phys;
  1894. memap_sz = pci_resource_len(pdev, i);
  1895. ioc->chip = ioremap(ioc->chip_phys, memap_sz);
  1896. }
  1897. }
  1898. if (ioc->chip == NULL) {
  1899. pr_err(MPT3SAS_FMT "unable to map adapter memory! "
  1900. " or resource not found\n", ioc->name);
  1901. r = -EINVAL;
  1902. goto out_fail;
  1903. }
  1904. _base_mask_interrupts(ioc);
  1905. r = _base_get_ioc_facts(ioc);
  1906. if (r)
  1907. goto out_fail;
  1908. if (!ioc->rdpq_array_enable_assigned) {
  1909. ioc->rdpq_array_enable = ioc->rdpq_array_capable;
  1910. ioc->rdpq_array_enable_assigned = 1;
  1911. }
  1912. r = _base_enable_msix(ioc);
  1913. if (r)
  1914. goto out_fail;
  1915. /* Use the Combined reply queue feature only for SAS3 C0 & higher
  1916. * revision HBAs and also only when reply queue count is greater than 8
  1917. */
  1918. if (ioc->combined_reply_queue && ioc->reply_queue_count > 8) {
  1919. /* Determine the Supplemental Reply Post Host Index Registers
  1920. * Addresse. Supplemental Reply Post Host Index Registers
  1921. * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
  1922. * each register is at offset bytes of
  1923. * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
  1924. */
  1925. ioc->replyPostRegisterIndex = kcalloc(
  1926. ioc->combined_reply_index_count,
  1927. sizeof(resource_size_t *), GFP_KERNEL);
  1928. if (!ioc->replyPostRegisterIndex) {
  1929. dfailprintk(ioc, printk(MPT3SAS_FMT
  1930. "allocation for reply Post Register Index failed!!!\n",
  1931. ioc->name));
  1932. r = -ENOMEM;
  1933. goto out_fail;
  1934. }
  1935. for (i = 0; i < ioc->combined_reply_index_count; i++) {
  1936. ioc->replyPostRegisterIndex[i] = (resource_size_t *)
  1937. ((u8 *)&ioc->chip->Doorbell +
  1938. MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
  1939. (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
  1940. }
  1941. } else
  1942. ioc->combined_reply_queue = 0;
  1943. if (ioc->is_warpdrive) {
  1944. ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
  1945. &ioc->chip->ReplyPostHostIndex;
  1946. for (i = 1; i < ioc->cpu_msix_table_sz; i++)
  1947. ioc->reply_post_host_index[i] =
  1948. (resource_size_t __iomem *)
  1949. ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
  1950. * 4)));
  1951. }
  1952. list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
  1953. pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
  1954. reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
  1955. "IO-APIC enabled"), reply_q->vector);
  1956. pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
  1957. ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
  1958. pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
  1959. ioc->name, (unsigned long long)pio_chip, pio_sz);
  1960. /* Save PCI configuration state for recovery from PCI AER/EEH errors */
  1961. pci_save_state(pdev);
  1962. return 0;
  1963. out_fail:
  1964. mpt3sas_base_unmap_resources(ioc);
  1965. return r;
  1966. }
  1967. /**
  1968. * mpt3sas_base_get_msg_frame - obtain request mf pointer
  1969. * @ioc: per adapter object
  1970. * @smid: system request message index(smid zero is invalid)
  1971. *
  1972. * Returns virt pointer to message frame.
  1973. */
  1974. void *
  1975. mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1976. {
  1977. return (void *)(ioc->request + (smid * ioc->request_sz));
  1978. }
  1979. /**
  1980. * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
  1981. * @ioc: per adapter object
  1982. * @smid: system request message index
  1983. *
  1984. * Returns virt pointer to sense buffer.
  1985. */
  1986. void *
  1987. mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1988. {
  1989. return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1990. }
  1991. /**
  1992. * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
  1993. * @ioc: per adapter object
  1994. * @smid: system request message index
  1995. *
  1996. * Returns phys pointer to the low 32bit address of the sense buffer.
  1997. */
  1998. __le32
  1999. mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2000. {
  2001. return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
  2002. SCSI_SENSE_BUFFERSIZE));
  2003. }
  2004. /**
  2005. * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
  2006. * @ioc: per adapter object
  2007. * @phys_addr: lower 32 physical addr of the reply
  2008. *
  2009. * Converts 32bit lower physical addr into a virt address.
  2010. */
  2011. void *
  2012. mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
  2013. {
  2014. if (!phys_addr)
  2015. return NULL;
  2016. return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
  2017. }
  2018. static inline u8
  2019. _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
  2020. {
  2021. return ioc->cpu_msix_table[raw_smp_processor_id()];
  2022. }
  2023. /**
  2024. * mpt3sas_base_get_smid - obtain a free smid from internal queue
  2025. * @ioc: per adapter object
  2026. * @cb_idx: callback index
  2027. *
  2028. * Returns smid (zero is invalid)
  2029. */
  2030. u16
  2031. mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  2032. {
  2033. unsigned long flags;
  2034. struct request_tracker *request;
  2035. u16 smid;
  2036. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2037. if (list_empty(&ioc->internal_free_list)) {
  2038. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2039. pr_err(MPT3SAS_FMT "%s: smid not available\n",
  2040. ioc->name, __func__);
  2041. return 0;
  2042. }
  2043. request = list_entry(ioc->internal_free_list.next,
  2044. struct request_tracker, tracker_list);
  2045. request->cb_idx = cb_idx;
  2046. smid = request->smid;
  2047. list_del(&request->tracker_list);
  2048. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2049. return smid;
  2050. }
  2051. /**
  2052. * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
  2053. * @ioc: per adapter object
  2054. * @cb_idx: callback index
  2055. * @scmd: pointer to scsi command object
  2056. *
  2057. * Returns smid (zero is invalid)
  2058. */
  2059. u16
  2060. mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
  2061. struct scsi_cmnd *scmd)
  2062. {
  2063. unsigned long flags;
  2064. struct scsiio_tracker *request;
  2065. u16 smid;
  2066. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2067. if (list_empty(&ioc->free_list)) {
  2068. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2069. pr_err(MPT3SAS_FMT "%s: smid not available\n",
  2070. ioc->name, __func__);
  2071. return 0;
  2072. }
  2073. request = list_entry(ioc->free_list.next,
  2074. struct scsiio_tracker, tracker_list);
  2075. request->scmd = scmd;
  2076. request->cb_idx = cb_idx;
  2077. smid = request->smid;
  2078. request->msix_io = _base_get_msix_index(ioc);
  2079. list_del(&request->tracker_list);
  2080. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2081. return smid;
  2082. }
  2083. /**
  2084. * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
  2085. * @ioc: per adapter object
  2086. * @cb_idx: callback index
  2087. *
  2088. * Returns smid (zero is invalid)
  2089. */
  2090. u16
  2091. mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  2092. {
  2093. unsigned long flags;
  2094. struct request_tracker *request;
  2095. u16 smid;
  2096. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2097. if (list_empty(&ioc->hpr_free_list)) {
  2098. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2099. return 0;
  2100. }
  2101. request = list_entry(ioc->hpr_free_list.next,
  2102. struct request_tracker, tracker_list);
  2103. request->cb_idx = cb_idx;
  2104. smid = request->smid;
  2105. list_del(&request->tracker_list);
  2106. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2107. return smid;
  2108. }
  2109. /**
  2110. * mpt3sas_base_free_smid - put smid back on free_list
  2111. * @ioc: per adapter object
  2112. * @smid: system request message index
  2113. *
  2114. * Return nothing.
  2115. */
  2116. void
  2117. mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2118. {
  2119. unsigned long flags;
  2120. int i;
  2121. struct chain_tracker *chain_req, *next;
  2122. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2123. if (smid < ioc->hi_priority_smid) {
  2124. /* scsiio queue */
  2125. i = smid - 1;
  2126. if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
  2127. list_for_each_entry_safe(chain_req, next,
  2128. &ioc->scsi_lookup[i].chain_list, tracker_list) {
  2129. list_del_init(&chain_req->tracker_list);
  2130. list_add(&chain_req->tracker_list,
  2131. &ioc->free_chain_list);
  2132. }
  2133. }
  2134. ioc->scsi_lookup[i].cb_idx = 0xFF;
  2135. ioc->scsi_lookup[i].scmd = NULL;
  2136. ioc->scsi_lookup[i].direct_io = 0;
  2137. list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
  2138. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2139. /*
  2140. * See _wait_for_commands_to_complete() call with regards
  2141. * to this code.
  2142. */
  2143. if (ioc->shost_recovery && ioc->pending_io_count) {
  2144. if (ioc->pending_io_count == 1)
  2145. wake_up(&ioc->reset_wq);
  2146. ioc->pending_io_count--;
  2147. }
  2148. return;
  2149. } else if (smid < ioc->internal_smid) {
  2150. /* hi-priority */
  2151. i = smid - ioc->hi_priority_smid;
  2152. ioc->hpr_lookup[i].cb_idx = 0xFF;
  2153. list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
  2154. } else if (smid <= ioc->hba_queue_depth) {
  2155. /* internal queue */
  2156. i = smid - ioc->internal_smid;
  2157. ioc->internal_lookup[i].cb_idx = 0xFF;
  2158. list_add(&ioc->internal_lookup[i].tracker_list,
  2159. &ioc->internal_free_list);
  2160. }
  2161. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2162. }
  2163. /**
  2164. * _base_writeq - 64 bit write to MMIO
  2165. * @ioc: per adapter object
  2166. * @b: data payload
  2167. * @addr: address in MMIO space
  2168. * @writeq_lock: spin lock
  2169. *
  2170. * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
  2171. * care of 32 bit environment where its not quarenteed to send the entire word
  2172. * in one transfer.
  2173. */
  2174. #if defined(writeq) && defined(CONFIG_64BIT)
  2175. static inline void
  2176. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  2177. {
  2178. writeq(cpu_to_le64(b), addr);
  2179. }
  2180. #else
  2181. static inline void
  2182. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  2183. {
  2184. unsigned long flags;
  2185. __u64 data_out = cpu_to_le64(b);
  2186. spin_lock_irqsave(writeq_lock, flags);
  2187. writel((u32)(data_out), addr);
  2188. writel((u32)(data_out >> 32), (addr + 4));
  2189. spin_unlock_irqrestore(writeq_lock, flags);
  2190. }
  2191. #endif
  2192. /**
  2193. * _base_put_smid_scsi_io - send SCSI_IO request to firmware
  2194. * @ioc: per adapter object
  2195. * @smid: system request message index
  2196. * @handle: device handle
  2197. *
  2198. * Return nothing.
  2199. */
  2200. static void
  2201. _base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
  2202. {
  2203. Mpi2RequestDescriptorUnion_t descriptor;
  2204. u64 *request = (u64 *)&descriptor;
  2205. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  2206. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  2207. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  2208. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  2209. descriptor.SCSIIO.LMID = 0;
  2210. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  2211. &ioc->scsi_lookup_lock);
  2212. }
  2213. /**
  2214. * _base_put_smid_fast_path - send fast path request to firmware
  2215. * @ioc: per adapter object
  2216. * @smid: system request message index
  2217. * @handle: device handle
  2218. *
  2219. * Return nothing.
  2220. */
  2221. static void
  2222. _base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  2223. u16 handle)
  2224. {
  2225. Mpi2RequestDescriptorUnion_t descriptor;
  2226. u64 *request = (u64 *)&descriptor;
  2227. descriptor.SCSIIO.RequestFlags =
  2228. MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
  2229. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  2230. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  2231. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  2232. descriptor.SCSIIO.LMID = 0;
  2233. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  2234. &ioc->scsi_lookup_lock);
  2235. }
  2236. /**
  2237. * _base_put_smid_hi_priority - send Task Management request to firmware
  2238. * @ioc: per adapter object
  2239. * @smid: system request message index
  2240. * @msix_task: msix_task will be same as msix of IO incase of task abort else 0.
  2241. * Return nothing.
  2242. */
  2243. static void
  2244. _base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  2245. u16 msix_task)
  2246. {
  2247. Mpi2RequestDescriptorUnion_t descriptor;
  2248. u64 *request = (u64 *)&descriptor;
  2249. descriptor.HighPriority.RequestFlags =
  2250. MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  2251. descriptor.HighPriority.MSIxIndex = msix_task;
  2252. descriptor.HighPriority.SMID = cpu_to_le16(smid);
  2253. descriptor.HighPriority.LMID = 0;
  2254. descriptor.HighPriority.Reserved1 = 0;
  2255. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  2256. &ioc->scsi_lookup_lock);
  2257. }
  2258. /**
  2259. * _base_put_smid_default - Default, primarily used for config pages
  2260. * @ioc: per adapter object
  2261. * @smid: system request message index
  2262. *
  2263. * Return nothing.
  2264. */
  2265. static void
  2266. _base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2267. {
  2268. Mpi2RequestDescriptorUnion_t descriptor;
  2269. u64 *request = (u64 *)&descriptor;
  2270. descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  2271. descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
  2272. descriptor.Default.SMID = cpu_to_le16(smid);
  2273. descriptor.Default.LMID = 0;
  2274. descriptor.Default.DescriptorTypeDependent = 0;
  2275. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  2276. &ioc->scsi_lookup_lock);
  2277. }
  2278. /**
  2279. * _base_put_smid_scsi_io_atomic - send SCSI_IO request to firmware using
  2280. * Atomic Request Descriptor
  2281. * @ioc: per adapter object
  2282. * @smid: system request message index
  2283. * @handle: device handle, unused in this function, for function type match
  2284. *
  2285. * Return nothing.
  2286. */
  2287. static void
  2288. _base_put_smid_scsi_io_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  2289. u16 handle)
  2290. {
  2291. Mpi26AtomicRequestDescriptor_t descriptor;
  2292. u32 *request = (u32 *)&descriptor;
  2293. descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  2294. descriptor.MSIxIndex = _base_get_msix_index(ioc);
  2295. descriptor.SMID = cpu_to_le16(smid);
  2296. writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
  2297. }
  2298. /**
  2299. * _base_put_smid_fast_path_atomic - send fast path request to firmware
  2300. * using Atomic Request Descriptor
  2301. * @ioc: per adapter object
  2302. * @smid: system request message index
  2303. * @handle: device handle, unused in this function, for function type match
  2304. * Return nothing
  2305. */
  2306. static void
  2307. _base_put_smid_fast_path_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  2308. u16 handle)
  2309. {
  2310. Mpi26AtomicRequestDescriptor_t descriptor;
  2311. u32 *request = (u32 *)&descriptor;
  2312. descriptor.RequestFlags = MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
  2313. descriptor.MSIxIndex = _base_get_msix_index(ioc);
  2314. descriptor.SMID = cpu_to_le16(smid);
  2315. writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
  2316. }
  2317. /**
  2318. * _base_put_smid_hi_priority_atomic - send Task Management request to
  2319. * firmware using Atomic Request Descriptor
  2320. * @ioc: per adapter object
  2321. * @smid: system request message index
  2322. * @msix_task: msix_task will be same as msix of IO incase of task abort else 0
  2323. *
  2324. * Return nothing.
  2325. */
  2326. static void
  2327. _base_put_smid_hi_priority_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  2328. u16 msix_task)
  2329. {
  2330. Mpi26AtomicRequestDescriptor_t descriptor;
  2331. u32 *request = (u32 *)&descriptor;
  2332. descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  2333. descriptor.MSIxIndex = msix_task;
  2334. descriptor.SMID = cpu_to_le16(smid);
  2335. writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
  2336. }
  2337. /**
  2338. * _base_put_smid_default - Default, primarily used for config pages
  2339. * use Atomic Request Descriptor
  2340. * @ioc: per adapter object
  2341. * @smid: system request message index
  2342. *
  2343. * Return nothing.
  2344. */
  2345. static void
  2346. _base_put_smid_default_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2347. {
  2348. Mpi26AtomicRequestDescriptor_t descriptor;
  2349. u32 *request = (u32 *)&descriptor;
  2350. descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  2351. descriptor.MSIxIndex = _base_get_msix_index(ioc);
  2352. descriptor.SMID = cpu_to_le16(smid);
  2353. writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
  2354. }
  2355. /**
  2356. * _base_display_OEMs_branding - Display branding string
  2357. * @ioc: per adapter object
  2358. *
  2359. * Return nothing.
  2360. */
  2361. static void
  2362. _base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
  2363. {
  2364. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  2365. return;
  2366. switch (ioc->pdev->subsystem_vendor) {
  2367. case PCI_VENDOR_ID_INTEL:
  2368. switch (ioc->pdev->device) {
  2369. case MPI2_MFGPAGE_DEVID_SAS2008:
  2370. switch (ioc->pdev->subsystem_device) {
  2371. case MPT2SAS_INTEL_RMS2LL080_SSDID:
  2372. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2373. MPT2SAS_INTEL_RMS2LL080_BRANDING);
  2374. break;
  2375. case MPT2SAS_INTEL_RMS2LL040_SSDID:
  2376. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2377. MPT2SAS_INTEL_RMS2LL040_BRANDING);
  2378. break;
  2379. case MPT2SAS_INTEL_SSD910_SSDID:
  2380. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2381. MPT2SAS_INTEL_SSD910_BRANDING);
  2382. break;
  2383. default:
  2384. pr_info(MPT3SAS_FMT
  2385. "Intel(R) Controller: Subsystem ID: 0x%X\n",
  2386. ioc->name, ioc->pdev->subsystem_device);
  2387. break;
  2388. }
  2389. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  2390. switch (ioc->pdev->subsystem_device) {
  2391. case MPT2SAS_INTEL_RS25GB008_SSDID:
  2392. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2393. MPT2SAS_INTEL_RS25GB008_BRANDING);
  2394. break;
  2395. case MPT2SAS_INTEL_RMS25JB080_SSDID:
  2396. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2397. MPT2SAS_INTEL_RMS25JB080_BRANDING);
  2398. break;
  2399. case MPT2SAS_INTEL_RMS25JB040_SSDID:
  2400. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2401. MPT2SAS_INTEL_RMS25JB040_BRANDING);
  2402. break;
  2403. case MPT2SAS_INTEL_RMS25KB080_SSDID:
  2404. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2405. MPT2SAS_INTEL_RMS25KB080_BRANDING);
  2406. break;
  2407. case MPT2SAS_INTEL_RMS25KB040_SSDID:
  2408. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2409. MPT2SAS_INTEL_RMS25KB040_BRANDING);
  2410. break;
  2411. case MPT2SAS_INTEL_RMS25LB040_SSDID:
  2412. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2413. MPT2SAS_INTEL_RMS25LB040_BRANDING);
  2414. break;
  2415. case MPT2SAS_INTEL_RMS25LB080_SSDID:
  2416. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2417. MPT2SAS_INTEL_RMS25LB080_BRANDING);
  2418. break;
  2419. default:
  2420. pr_info(MPT3SAS_FMT
  2421. "Intel(R) Controller: Subsystem ID: 0x%X\n",
  2422. ioc->name, ioc->pdev->subsystem_device);
  2423. break;
  2424. }
  2425. case MPI25_MFGPAGE_DEVID_SAS3008:
  2426. switch (ioc->pdev->subsystem_device) {
  2427. case MPT3SAS_INTEL_RMS3JC080_SSDID:
  2428. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2429. MPT3SAS_INTEL_RMS3JC080_BRANDING);
  2430. break;
  2431. case MPT3SAS_INTEL_RS3GC008_SSDID:
  2432. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2433. MPT3SAS_INTEL_RS3GC008_BRANDING);
  2434. break;
  2435. case MPT3SAS_INTEL_RS3FC044_SSDID:
  2436. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2437. MPT3SAS_INTEL_RS3FC044_BRANDING);
  2438. break;
  2439. case MPT3SAS_INTEL_RS3UC080_SSDID:
  2440. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2441. MPT3SAS_INTEL_RS3UC080_BRANDING);
  2442. break;
  2443. default:
  2444. pr_info(MPT3SAS_FMT
  2445. "Intel(R) Controller: Subsystem ID: 0x%X\n",
  2446. ioc->name, ioc->pdev->subsystem_device);
  2447. break;
  2448. }
  2449. break;
  2450. default:
  2451. pr_info(MPT3SAS_FMT
  2452. "Intel(R) Controller: Subsystem ID: 0x%X\n",
  2453. ioc->name, ioc->pdev->subsystem_device);
  2454. break;
  2455. }
  2456. break;
  2457. case PCI_VENDOR_ID_DELL:
  2458. switch (ioc->pdev->device) {
  2459. case MPI2_MFGPAGE_DEVID_SAS2008:
  2460. switch (ioc->pdev->subsystem_device) {
  2461. case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
  2462. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2463. MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
  2464. break;
  2465. case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
  2466. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2467. MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
  2468. break;
  2469. case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
  2470. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2471. MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
  2472. break;
  2473. case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
  2474. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2475. MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
  2476. break;
  2477. case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
  2478. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2479. MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
  2480. break;
  2481. case MPT2SAS_DELL_PERC_H200_SSDID:
  2482. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2483. MPT2SAS_DELL_PERC_H200_BRANDING);
  2484. break;
  2485. case MPT2SAS_DELL_6GBPS_SAS_SSDID:
  2486. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2487. MPT2SAS_DELL_6GBPS_SAS_BRANDING);
  2488. break;
  2489. default:
  2490. pr_info(MPT3SAS_FMT
  2491. "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
  2492. ioc->name, ioc->pdev->subsystem_device);
  2493. break;
  2494. }
  2495. break;
  2496. case MPI25_MFGPAGE_DEVID_SAS3008:
  2497. switch (ioc->pdev->subsystem_device) {
  2498. case MPT3SAS_DELL_12G_HBA_SSDID:
  2499. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2500. MPT3SAS_DELL_12G_HBA_BRANDING);
  2501. break;
  2502. default:
  2503. pr_info(MPT3SAS_FMT
  2504. "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
  2505. ioc->name, ioc->pdev->subsystem_device);
  2506. break;
  2507. }
  2508. break;
  2509. default:
  2510. pr_info(MPT3SAS_FMT
  2511. "Dell HBA: Subsystem ID: 0x%X\n", ioc->name,
  2512. ioc->pdev->subsystem_device);
  2513. break;
  2514. }
  2515. break;
  2516. case PCI_VENDOR_ID_CISCO:
  2517. switch (ioc->pdev->device) {
  2518. case MPI25_MFGPAGE_DEVID_SAS3008:
  2519. switch (ioc->pdev->subsystem_device) {
  2520. case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
  2521. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2522. MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
  2523. break;
  2524. case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
  2525. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2526. MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
  2527. break;
  2528. case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
  2529. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2530. MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
  2531. break;
  2532. default:
  2533. pr_info(MPT3SAS_FMT
  2534. "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
  2535. ioc->name, ioc->pdev->subsystem_device);
  2536. break;
  2537. }
  2538. break;
  2539. case MPI25_MFGPAGE_DEVID_SAS3108_1:
  2540. switch (ioc->pdev->subsystem_device) {
  2541. case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
  2542. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2543. MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
  2544. break;
  2545. case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
  2546. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2547. MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING
  2548. );
  2549. break;
  2550. default:
  2551. pr_info(MPT3SAS_FMT
  2552. "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
  2553. ioc->name, ioc->pdev->subsystem_device);
  2554. break;
  2555. }
  2556. break;
  2557. default:
  2558. pr_info(MPT3SAS_FMT
  2559. "Cisco SAS HBA: Subsystem ID: 0x%X\n",
  2560. ioc->name, ioc->pdev->subsystem_device);
  2561. break;
  2562. }
  2563. break;
  2564. case MPT2SAS_HP_3PAR_SSVID:
  2565. switch (ioc->pdev->device) {
  2566. case MPI2_MFGPAGE_DEVID_SAS2004:
  2567. switch (ioc->pdev->subsystem_device) {
  2568. case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
  2569. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2570. MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
  2571. break;
  2572. default:
  2573. pr_info(MPT3SAS_FMT
  2574. "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
  2575. ioc->name, ioc->pdev->subsystem_device);
  2576. break;
  2577. }
  2578. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  2579. switch (ioc->pdev->subsystem_device) {
  2580. case MPT2SAS_HP_2_4_INTERNAL_SSDID:
  2581. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2582. MPT2SAS_HP_2_4_INTERNAL_BRANDING);
  2583. break;
  2584. case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
  2585. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2586. MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
  2587. break;
  2588. case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
  2589. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2590. MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
  2591. break;
  2592. case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
  2593. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2594. MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
  2595. break;
  2596. default:
  2597. pr_info(MPT3SAS_FMT
  2598. "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
  2599. ioc->name, ioc->pdev->subsystem_device);
  2600. break;
  2601. }
  2602. default:
  2603. pr_info(MPT3SAS_FMT
  2604. "HP SAS HBA: Subsystem ID: 0x%X\n",
  2605. ioc->name, ioc->pdev->subsystem_device);
  2606. break;
  2607. }
  2608. default:
  2609. break;
  2610. }
  2611. }
  2612. /**
  2613. * _base_display_ioc_capabilities - Disply IOC's capabilities.
  2614. * @ioc: per adapter object
  2615. *
  2616. * Return nothing.
  2617. */
  2618. static void
  2619. _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
  2620. {
  2621. int i = 0;
  2622. char desc[16];
  2623. u32 iounit_pg1_flags;
  2624. u32 bios_version;
  2625. bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  2626. strncpy(desc, ioc->manu_pg0.ChipName, 16);
  2627. pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
  2628. "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
  2629. ioc->name, desc,
  2630. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  2631. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  2632. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  2633. ioc->facts.FWVersion.Word & 0x000000FF,
  2634. ioc->pdev->revision,
  2635. (bios_version & 0xFF000000) >> 24,
  2636. (bios_version & 0x00FF0000) >> 16,
  2637. (bios_version & 0x0000FF00) >> 8,
  2638. bios_version & 0x000000FF);
  2639. _base_display_OEMs_branding(ioc);
  2640. pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
  2641. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
  2642. pr_info("Initiator");
  2643. i++;
  2644. }
  2645. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
  2646. pr_info("%sTarget", i ? "," : "");
  2647. i++;
  2648. }
  2649. i = 0;
  2650. pr_info("), ");
  2651. pr_info("Capabilities=(");
  2652. if (!ioc->hide_ir_msg) {
  2653. if (ioc->facts.IOCCapabilities &
  2654. MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
  2655. pr_info("Raid");
  2656. i++;
  2657. }
  2658. }
  2659. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
  2660. pr_info("%sTLR", i ? "," : "");
  2661. i++;
  2662. }
  2663. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
  2664. pr_info("%sMulticast", i ? "," : "");
  2665. i++;
  2666. }
  2667. if (ioc->facts.IOCCapabilities &
  2668. MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
  2669. pr_info("%sBIDI Target", i ? "," : "");
  2670. i++;
  2671. }
  2672. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
  2673. pr_info("%sEEDP", i ? "," : "");
  2674. i++;
  2675. }
  2676. if (ioc->facts.IOCCapabilities &
  2677. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
  2678. pr_info("%sSnapshot Buffer", i ? "," : "");
  2679. i++;
  2680. }
  2681. if (ioc->facts.IOCCapabilities &
  2682. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
  2683. pr_info("%sDiag Trace Buffer", i ? "," : "");
  2684. i++;
  2685. }
  2686. if (ioc->facts.IOCCapabilities &
  2687. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
  2688. pr_info("%sDiag Extended Buffer", i ? "," : "");
  2689. i++;
  2690. }
  2691. if (ioc->facts.IOCCapabilities &
  2692. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
  2693. pr_info("%sTask Set Full", i ? "," : "");
  2694. i++;
  2695. }
  2696. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  2697. if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
  2698. pr_info("%sNCQ", i ? "," : "");
  2699. i++;
  2700. }
  2701. pr_info(")\n");
  2702. }
  2703. /**
  2704. * mpt3sas_base_update_missing_delay - change the missing delay timers
  2705. * @ioc: per adapter object
  2706. * @device_missing_delay: amount of time till device is reported missing
  2707. * @io_missing_delay: interval IO is returned when there is a missing device
  2708. *
  2709. * Return nothing.
  2710. *
  2711. * Passed on the command line, this function will modify the device missing
  2712. * delay, as well as the io missing delay. This should be called at driver
  2713. * load time.
  2714. */
  2715. void
  2716. mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
  2717. u16 device_missing_delay, u8 io_missing_delay)
  2718. {
  2719. u16 dmd, dmd_new, dmd_orignal;
  2720. u8 io_missing_delay_original;
  2721. u16 sz;
  2722. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  2723. Mpi2ConfigReply_t mpi_reply;
  2724. u8 num_phys = 0;
  2725. u16 ioc_status;
  2726. mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
  2727. if (!num_phys)
  2728. return;
  2729. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
  2730. sizeof(Mpi2SasIOUnit1PhyData_t));
  2731. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  2732. if (!sas_iounit_pg1) {
  2733. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  2734. ioc->name, __FILE__, __LINE__, __func__);
  2735. goto out;
  2736. }
  2737. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  2738. sas_iounit_pg1, sz))) {
  2739. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  2740. ioc->name, __FILE__, __LINE__, __func__);
  2741. goto out;
  2742. }
  2743. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2744. MPI2_IOCSTATUS_MASK;
  2745. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2746. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  2747. ioc->name, __FILE__, __LINE__, __func__);
  2748. goto out;
  2749. }
  2750. /* device missing delay */
  2751. dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
  2752. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2753. dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2754. else
  2755. dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2756. dmd_orignal = dmd;
  2757. if (device_missing_delay > 0x7F) {
  2758. dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
  2759. device_missing_delay;
  2760. dmd = dmd / 16;
  2761. dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
  2762. } else
  2763. dmd = device_missing_delay;
  2764. sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
  2765. /* io missing delay */
  2766. io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
  2767. sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
  2768. if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  2769. sz)) {
  2770. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2771. dmd_new = (dmd &
  2772. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2773. else
  2774. dmd_new =
  2775. dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2776. pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
  2777. ioc->name, dmd_orignal, dmd_new);
  2778. pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
  2779. ioc->name, io_missing_delay_original,
  2780. io_missing_delay);
  2781. ioc->device_missing_delay = dmd_new;
  2782. ioc->io_missing_delay = io_missing_delay;
  2783. }
  2784. out:
  2785. kfree(sas_iounit_pg1);
  2786. }
  2787. /**
  2788. * _base_static_config_pages - static start of day config pages
  2789. * @ioc: per adapter object
  2790. *
  2791. * Return nothing.
  2792. */
  2793. static void
  2794. _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
  2795. {
  2796. Mpi2ConfigReply_t mpi_reply;
  2797. u32 iounit_pg1_flags;
  2798. mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
  2799. if (ioc->ir_firmware)
  2800. mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
  2801. &ioc->manu_pg10);
  2802. /*
  2803. * Ensure correct T10 PI operation if vendor left EEDPTagMode
  2804. * flag unset in NVDATA.
  2805. */
  2806. mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
  2807. if (ioc->manu_pg11.EEDPTagMode == 0) {
  2808. pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
  2809. ioc->name);
  2810. ioc->manu_pg11.EEDPTagMode &= ~0x3;
  2811. ioc->manu_pg11.EEDPTagMode |= 0x1;
  2812. mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
  2813. &ioc->manu_pg11);
  2814. }
  2815. mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
  2816. mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
  2817. mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
  2818. mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
  2819. mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2820. mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
  2821. _base_display_ioc_capabilities(ioc);
  2822. /*
  2823. * Enable task_set_full handling in iounit_pg1 when the
  2824. * facts capabilities indicate that its supported.
  2825. */
  2826. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  2827. if ((ioc->facts.IOCCapabilities &
  2828. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
  2829. iounit_pg1_flags &=
  2830. ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2831. else
  2832. iounit_pg1_flags |=
  2833. MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2834. ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
  2835. mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2836. if (ioc->iounit_pg8.NumSensors)
  2837. ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
  2838. }
  2839. /**
  2840. * _base_release_memory_pools - release memory
  2841. * @ioc: per adapter object
  2842. *
  2843. * Free memory allocated from _base_allocate_memory_pools.
  2844. *
  2845. * Return nothing.
  2846. */
  2847. static void
  2848. _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
  2849. {
  2850. int i = 0;
  2851. struct reply_post_struct *rps;
  2852. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2853. __func__));
  2854. if (ioc->request) {
  2855. pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
  2856. ioc->request, ioc->request_dma);
  2857. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2858. "request_pool(0x%p): free\n",
  2859. ioc->name, ioc->request));
  2860. ioc->request = NULL;
  2861. }
  2862. if (ioc->sense) {
  2863. pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  2864. if (ioc->sense_dma_pool)
  2865. pci_pool_destroy(ioc->sense_dma_pool);
  2866. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2867. "sense_pool(0x%p): free\n",
  2868. ioc->name, ioc->sense));
  2869. ioc->sense = NULL;
  2870. }
  2871. if (ioc->reply) {
  2872. pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
  2873. if (ioc->reply_dma_pool)
  2874. pci_pool_destroy(ioc->reply_dma_pool);
  2875. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2876. "reply_pool(0x%p): free\n",
  2877. ioc->name, ioc->reply));
  2878. ioc->reply = NULL;
  2879. }
  2880. if (ioc->reply_free) {
  2881. pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
  2882. ioc->reply_free_dma);
  2883. if (ioc->reply_free_dma_pool)
  2884. pci_pool_destroy(ioc->reply_free_dma_pool);
  2885. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2886. "reply_free_pool(0x%p): free\n",
  2887. ioc->name, ioc->reply_free));
  2888. ioc->reply_free = NULL;
  2889. }
  2890. if (ioc->reply_post) {
  2891. do {
  2892. rps = &ioc->reply_post[i];
  2893. if (rps->reply_post_free) {
  2894. pci_pool_free(
  2895. ioc->reply_post_free_dma_pool,
  2896. rps->reply_post_free,
  2897. rps->reply_post_free_dma);
  2898. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2899. "reply_post_free_pool(0x%p): free\n",
  2900. ioc->name, rps->reply_post_free));
  2901. rps->reply_post_free = NULL;
  2902. }
  2903. } while (ioc->rdpq_array_enable &&
  2904. (++i < ioc->reply_queue_count));
  2905. if (ioc->reply_post_free_dma_pool)
  2906. pci_pool_destroy(ioc->reply_post_free_dma_pool);
  2907. kfree(ioc->reply_post);
  2908. }
  2909. if (ioc->config_page) {
  2910. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2911. "config_page(0x%p): free\n", ioc->name,
  2912. ioc->config_page));
  2913. pci_free_consistent(ioc->pdev, ioc->config_page_sz,
  2914. ioc->config_page, ioc->config_page_dma);
  2915. }
  2916. if (ioc->scsi_lookup) {
  2917. free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
  2918. ioc->scsi_lookup = NULL;
  2919. }
  2920. kfree(ioc->hpr_lookup);
  2921. kfree(ioc->internal_lookup);
  2922. if (ioc->chain_lookup) {
  2923. for (i = 0; i < ioc->chain_depth; i++) {
  2924. if (ioc->chain_lookup[i].chain_buffer)
  2925. pci_pool_free(ioc->chain_dma_pool,
  2926. ioc->chain_lookup[i].chain_buffer,
  2927. ioc->chain_lookup[i].chain_buffer_dma);
  2928. }
  2929. if (ioc->chain_dma_pool)
  2930. pci_pool_destroy(ioc->chain_dma_pool);
  2931. free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
  2932. ioc->chain_lookup = NULL;
  2933. }
  2934. }
  2935. /**
  2936. * _base_allocate_memory_pools - allocate start of day memory pools
  2937. * @ioc: per adapter object
  2938. *
  2939. * Returns 0 success, anything else error
  2940. */
  2941. static int
  2942. _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
  2943. {
  2944. struct mpt3sas_facts *facts;
  2945. u16 max_sge_elements;
  2946. u16 chains_needed_per_io;
  2947. u32 sz, total_sz, reply_post_free_sz;
  2948. u32 retry_sz;
  2949. u16 max_request_credit;
  2950. unsigned short sg_tablesize;
  2951. u16 sge_size;
  2952. int i;
  2953. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2954. __func__));
  2955. retry_sz = 0;
  2956. facts = &ioc->facts;
  2957. /* command line tunables for max sgl entries */
  2958. if (max_sgl_entries != -1)
  2959. sg_tablesize = max_sgl_entries;
  2960. else {
  2961. if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
  2962. sg_tablesize = MPT2SAS_SG_DEPTH;
  2963. else
  2964. sg_tablesize = MPT3SAS_SG_DEPTH;
  2965. }
  2966. if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
  2967. sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
  2968. else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
  2969. sg_tablesize = min_t(unsigned short, sg_tablesize,
  2970. SG_MAX_SEGMENTS);
  2971. pr_warn(MPT3SAS_FMT
  2972. "sg_tablesize(%u) is bigger than kernel"
  2973. " defined SG_CHUNK_SIZE(%u)\n", ioc->name,
  2974. sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
  2975. }
  2976. ioc->shost->sg_tablesize = sg_tablesize;
  2977. ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
  2978. (facts->RequestCredit / 4));
  2979. if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
  2980. if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
  2981. INTERNAL_SCSIIO_CMDS_COUNT)) {
  2982. pr_err(MPT3SAS_FMT "IOC doesn't have enough Request \
  2983. Credits, it has just %d number of credits\n",
  2984. ioc->name, facts->RequestCredit);
  2985. return -ENOMEM;
  2986. }
  2987. ioc->internal_depth = 10;
  2988. }
  2989. ioc->hi_priority_depth = ioc->internal_depth - (5);
  2990. /* command line tunables for max controller queue depth */
  2991. if (max_queue_depth != -1 && max_queue_depth != 0) {
  2992. max_request_credit = min_t(u16, max_queue_depth +
  2993. ioc->internal_depth, facts->RequestCredit);
  2994. if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
  2995. max_request_credit = MAX_HBA_QUEUE_DEPTH;
  2996. } else
  2997. max_request_credit = min_t(u16, facts->RequestCredit,
  2998. MAX_HBA_QUEUE_DEPTH);
  2999. /* Firmware maintains additional facts->HighPriorityCredit number of
  3000. * credits for HiPriprity Request messages, so hba queue depth will be
  3001. * sum of max_request_credit and high priority queue depth.
  3002. */
  3003. ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
  3004. /* request frame size */
  3005. ioc->request_sz = facts->IOCRequestFrameSize * 4;
  3006. /* reply frame size */
  3007. ioc->reply_sz = facts->ReplyFrameSize * 4;
  3008. /* chain segment size */
  3009. if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  3010. if (facts->IOCMaxChainSegmentSize)
  3011. ioc->chain_segment_sz =
  3012. facts->IOCMaxChainSegmentSize *
  3013. MAX_CHAIN_ELEMT_SZ;
  3014. else
  3015. /* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
  3016. ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
  3017. MAX_CHAIN_ELEMT_SZ;
  3018. } else
  3019. ioc->chain_segment_sz = ioc->request_sz;
  3020. /* calculate the max scatter element size */
  3021. sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
  3022. retry_allocation:
  3023. total_sz = 0;
  3024. /* calculate number of sg elements left over in the 1st frame */
  3025. max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
  3026. sizeof(Mpi2SGEIOUnion_t)) + sge_size);
  3027. ioc->max_sges_in_main_message = max_sge_elements/sge_size;
  3028. /* now do the same for a chain buffer */
  3029. max_sge_elements = ioc->chain_segment_sz - sge_size;
  3030. ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
  3031. /*
  3032. * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
  3033. */
  3034. chains_needed_per_io = ((ioc->shost->sg_tablesize -
  3035. ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
  3036. + 1;
  3037. if (chains_needed_per_io > facts->MaxChainDepth) {
  3038. chains_needed_per_io = facts->MaxChainDepth;
  3039. ioc->shost->sg_tablesize = min_t(u16,
  3040. ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
  3041. * chains_needed_per_io), ioc->shost->sg_tablesize);
  3042. }
  3043. ioc->chains_needed_per_io = chains_needed_per_io;
  3044. /* reply free queue sizing - taking into account for 64 FW events */
  3045. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  3046. /* calculate reply descriptor post queue depth */
  3047. ioc->reply_post_queue_depth = ioc->hba_queue_depth +
  3048. ioc->reply_free_queue_depth + 1 ;
  3049. /* align the reply post queue on the next 16 count boundary */
  3050. if (ioc->reply_post_queue_depth % 16)
  3051. ioc->reply_post_queue_depth += 16 -
  3052. (ioc->reply_post_queue_depth % 16);
  3053. if (ioc->reply_post_queue_depth >
  3054. facts->MaxReplyDescriptorPostQueueDepth) {
  3055. ioc->reply_post_queue_depth =
  3056. facts->MaxReplyDescriptorPostQueueDepth -
  3057. (facts->MaxReplyDescriptorPostQueueDepth % 16);
  3058. ioc->hba_queue_depth =
  3059. ((ioc->reply_post_queue_depth - 64) / 2) - 1;
  3060. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  3061. }
  3062. dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
  3063. "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
  3064. "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
  3065. ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
  3066. ioc->chains_needed_per_io));
  3067. /* reply post queue, 16 byte align */
  3068. reply_post_free_sz = ioc->reply_post_queue_depth *
  3069. sizeof(Mpi2DefaultReplyDescriptor_t);
  3070. sz = reply_post_free_sz;
  3071. if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
  3072. sz *= ioc->reply_queue_count;
  3073. ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
  3074. (ioc->reply_queue_count):1,
  3075. sizeof(struct reply_post_struct), GFP_KERNEL);
  3076. if (!ioc->reply_post) {
  3077. pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
  3078. ioc->name);
  3079. goto out;
  3080. }
  3081. ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
  3082. ioc->pdev, sz, 16, 0);
  3083. if (!ioc->reply_post_free_dma_pool) {
  3084. pr_err(MPT3SAS_FMT
  3085. "reply_post_free pool: pci_pool_create failed\n",
  3086. ioc->name);
  3087. goto out;
  3088. }
  3089. i = 0;
  3090. do {
  3091. ioc->reply_post[i].reply_post_free =
  3092. pci_pool_alloc(ioc->reply_post_free_dma_pool,
  3093. GFP_KERNEL,
  3094. &ioc->reply_post[i].reply_post_free_dma);
  3095. if (!ioc->reply_post[i].reply_post_free) {
  3096. pr_err(MPT3SAS_FMT
  3097. "reply_post_free pool: pci_pool_alloc failed\n",
  3098. ioc->name);
  3099. goto out;
  3100. }
  3101. memset(ioc->reply_post[i].reply_post_free, 0, sz);
  3102. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3103. "reply post free pool (0x%p): depth(%d),"
  3104. "element_size(%d), pool_size(%d kB)\n", ioc->name,
  3105. ioc->reply_post[i].reply_post_free,
  3106. ioc->reply_post_queue_depth, 8, sz/1024));
  3107. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3108. "reply_post_free_dma = (0x%llx)\n", ioc->name,
  3109. (unsigned long long)
  3110. ioc->reply_post[i].reply_post_free_dma));
  3111. total_sz += sz;
  3112. } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
  3113. if (ioc->dma_mask == 64) {
  3114. if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
  3115. pr_warn(MPT3SAS_FMT
  3116. "no suitable consistent DMA mask for %s\n",
  3117. ioc->name, pci_name(ioc->pdev));
  3118. goto out;
  3119. }
  3120. }
  3121. ioc->scsiio_depth = ioc->hba_queue_depth -
  3122. ioc->hi_priority_depth - ioc->internal_depth;
  3123. /* set the scsi host can_queue depth
  3124. * with some internal commands that could be outstanding
  3125. */
  3126. ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
  3127. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3128. "scsi host: can_queue depth (%d)\n",
  3129. ioc->name, ioc->shost->can_queue));
  3130. /* contiguous pool for request and chains, 16 byte align, one extra "
  3131. * "frame for smid=0
  3132. */
  3133. ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
  3134. sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
  3135. /* hi-priority queue */
  3136. sz += (ioc->hi_priority_depth * ioc->request_sz);
  3137. /* internal queue */
  3138. sz += (ioc->internal_depth * ioc->request_sz);
  3139. ioc->request_dma_sz = sz;
  3140. ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
  3141. if (!ioc->request) {
  3142. pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
  3143. "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  3144. "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
  3145. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  3146. if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
  3147. goto out;
  3148. retry_sz = 64;
  3149. ioc->hba_queue_depth -= retry_sz;
  3150. _base_release_memory_pools(ioc);
  3151. goto retry_allocation;
  3152. }
  3153. if (retry_sz)
  3154. pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
  3155. "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  3156. "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
  3157. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  3158. /* hi-priority queue */
  3159. ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
  3160. ioc->request_sz);
  3161. ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
  3162. ioc->request_sz);
  3163. /* internal queue */
  3164. ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
  3165. ioc->request_sz);
  3166. ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
  3167. ioc->request_sz);
  3168. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3169. "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
  3170. ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
  3171. (ioc->hba_queue_depth * ioc->request_sz)/1024));
  3172. dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
  3173. ioc->name, (unsigned long long) ioc->request_dma));
  3174. total_sz += sz;
  3175. sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
  3176. ioc->scsi_lookup_pages = get_order(sz);
  3177. ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
  3178. GFP_KERNEL, ioc->scsi_lookup_pages);
  3179. if (!ioc->scsi_lookup) {
  3180. pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
  3181. ioc->name, (int)sz);
  3182. goto out;
  3183. }
  3184. dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
  3185. ioc->name, ioc->request, ioc->scsiio_depth));
  3186. ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
  3187. sz = ioc->chain_depth * sizeof(struct chain_tracker);
  3188. ioc->chain_pages = get_order(sz);
  3189. ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
  3190. GFP_KERNEL, ioc->chain_pages);
  3191. if (!ioc->chain_lookup) {
  3192. pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
  3193. ioc->name);
  3194. goto out;
  3195. }
  3196. ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
  3197. ioc->chain_segment_sz, 16, 0);
  3198. if (!ioc->chain_dma_pool) {
  3199. pr_err(MPT3SAS_FMT "chain_dma_pool: pci_pool_create failed\n",
  3200. ioc->name);
  3201. goto out;
  3202. }
  3203. for (i = 0; i < ioc->chain_depth; i++) {
  3204. ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
  3205. ioc->chain_dma_pool , GFP_KERNEL,
  3206. &ioc->chain_lookup[i].chain_buffer_dma);
  3207. if (!ioc->chain_lookup[i].chain_buffer) {
  3208. ioc->chain_depth = i;
  3209. goto chain_done;
  3210. }
  3211. total_sz += ioc->chain_segment_sz;
  3212. }
  3213. chain_done:
  3214. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3215. "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
  3216. ioc->name, ioc->chain_depth, ioc->chain_segment_sz,
  3217. ((ioc->chain_depth * ioc->chain_segment_sz))/1024));
  3218. /* initialize hi-priority queue smid's */
  3219. ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
  3220. sizeof(struct request_tracker), GFP_KERNEL);
  3221. if (!ioc->hpr_lookup) {
  3222. pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
  3223. ioc->name);
  3224. goto out;
  3225. }
  3226. ioc->hi_priority_smid = ioc->scsiio_depth + 1;
  3227. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3228. "hi_priority(0x%p): depth(%d), start smid(%d)\n",
  3229. ioc->name, ioc->hi_priority,
  3230. ioc->hi_priority_depth, ioc->hi_priority_smid));
  3231. /* initialize internal queue smid's */
  3232. ioc->internal_lookup = kcalloc(ioc->internal_depth,
  3233. sizeof(struct request_tracker), GFP_KERNEL);
  3234. if (!ioc->internal_lookup) {
  3235. pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
  3236. ioc->name);
  3237. goto out;
  3238. }
  3239. ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
  3240. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3241. "internal(0x%p): depth(%d), start smid(%d)\n",
  3242. ioc->name, ioc->internal,
  3243. ioc->internal_depth, ioc->internal_smid));
  3244. /* sense buffers, 4 byte align */
  3245. sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
  3246. ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
  3247. 0);
  3248. if (!ioc->sense_dma_pool) {
  3249. pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
  3250. ioc->name);
  3251. goto out;
  3252. }
  3253. ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
  3254. &ioc->sense_dma);
  3255. if (!ioc->sense) {
  3256. pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
  3257. ioc->name);
  3258. goto out;
  3259. }
  3260. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3261. "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
  3262. "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
  3263. SCSI_SENSE_BUFFERSIZE, sz/1024));
  3264. dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
  3265. ioc->name, (unsigned long long)ioc->sense_dma));
  3266. total_sz += sz;
  3267. /* reply pool, 4 byte align */
  3268. sz = ioc->reply_free_queue_depth * ioc->reply_sz;
  3269. ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
  3270. 0);
  3271. if (!ioc->reply_dma_pool) {
  3272. pr_err(MPT3SAS_FMT "reply pool: pci_pool_create failed\n",
  3273. ioc->name);
  3274. goto out;
  3275. }
  3276. ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
  3277. &ioc->reply_dma);
  3278. if (!ioc->reply) {
  3279. pr_err(MPT3SAS_FMT "reply pool: pci_pool_alloc failed\n",
  3280. ioc->name);
  3281. goto out;
  3282. }
  3283. ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
  3284. ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
  3285. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3286. "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
  3287. ioc->name, ioc->reply,
  3288. ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
  3289. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
  3290. ioc->name, (unsigned long long)ioc->reply_dma));
  3291. total_sz += sz;
  3292. /* reply free queue, 16 byte align */
  3293. sz = ioc->reply_free_queue_depth * 4;
  3294. ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
  3295. ioc->pdev, sz, 16, 0);
  3296. if (!ioc->reply_free_dma_pool) {
  3297. pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_create failed\n",
  3298. ioc->name);
  3299. goto out;
  3300. }
  3301. ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
  3302. &ioc->reply_free_dma);
  3303. if (!ioc->reply_free) {
  3304. pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_alloc failed\n",
  3305. ioc->name);
  3306. goto out;
  3307. }
  3308. memset(ioc->reply_free, 0, sz);
  3309. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
  3310. "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
  3311. ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
  3312. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3313. "reply_free_dma (0x%llx)\n",
  3314. ioc->name, (unsigned long long)ioc->reply_free_dma));
  3315. total_sz += sz;
  3316. ioc->config_page_sz = 512;
  3317. ioc->config_page = pci_alloc_consistent(ioc->pdev,
  3318. ioc->config_page_sz, &ioc->config_page_dma);
  3319. if (!ioc->config_page) {
  3320. pr_err(MPT3SAS_FMT
  3321. "config page: pci_pool_alloc failed\n",
  3322. ioc->name);
  3323. goto out;
  3324. }
  3325. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3326. "config page(0x%p): size(%d)\n",
  3327. ioc->name, ioc->config_page, ioc->config_page_sz));
  3328. dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
  3329. ioc->name, (unsigned long long)ioc->config_page_dma));
  3330. total_sz += ioc->config_page_sz;
  3331. pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
  3332. ioc->name, total_sz/1024);
  3333. pr_info(MPT3SAS_FMT
  3334. "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
  3335. ioc->name, ioc->shost->can_queue, facts->RequestCredit);
  3336. pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
  3337. ioc->name, ioc->shost->sg_tablesize);
  3338. return 0;
  3339. out:
  3340. return -ENOMEM;
  3341. }
  3342. /**
  3343. * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
  3344. * @ioc: Pointer to MPT_ADAPTER structure
  3345. * @cooked: Request raw or cooked IOC state
  3346. *
  3347. * Returns all IOC Doorbell register bits if cooked==0, else just the
  3348. * Doorbell bits in MPI_IOC_STATE_MASK.
  3349. */
  3350. u32
  3351. mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
  3352. {
  3353. u32 s, sc;
  3354. s = readl(&ioc->chip->Doorbell);
  3355. sc = s & MPI2_IOC_STATE_MASK;
  3356. return cooked ? sc : s;
  3357. }
  3358. /**
  3359. * _base_wait_on_iocstate - waiting on a particular ioc state
  3360. * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
  3361. * @timeout: timeout in second
  3362. *
  3363. * Returns 0 for success, non-zero for failure.
  3364. */
  3365. static int
  3366. _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout)
  3367. {
  3368. u32 count, cntdn;
  3369. u32 current_state;
  3370. count = 0;
  3371. cntdn = 1000 * timeout;
  3372. do {
  3373. current_state = mpt3sas_base_get_iocstate(ioc, 1);
  3374. if (current_state == ioc_state)
  3375. return 0;
  3376. if (count && current_state == MPI2_IOC_STATE_FAULT)
  3377. break;
  3378. usleep_range(1000, 1500);
  3379. count++;
  3380. } while (--cntdn);
  3381. return current_state;
  3382. }
  3383. /**
  3384. * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
  3385. * a write to the doorbell)
  3386. * @ioc: per adapter object
  3387. * @timeout: timeout in second
  3388. *
  3389. * Returns 0 for success, non-zero for failure.
  3390. *
  3391. * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
  3392. */
  3393. static int
  3394. _base_diag_reset(struct MPT3SAS_ADAPTER *ioc);
  3395. static int
  3396. _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
  3397. {
  3398. u32 cntdn, count;
  3399. u32 int_status;
  3400. count = 0;
  3401. cntdn = 1000 * timeout;
  3402. do {
  3403. int_status = readl(&ioc->chip->HostInterruptStatus);
  3404. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  3405. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  3406. "%s: successful count(%d), timeout(%d)\n",
  3407. ioc->name, __func__, count, timeout));
  3408. return 0;
  3409. }
  3410. usleep_range(1000, 1500);
  3411. count++;
  3412. } while (--cntdn);
  3413. pr_err(MPT3SAS_FMT
  3414. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  3415. ioc->name, __func__, count, int_status);
  3416. return -EFAULT;
  3417. }
  3418. static int
  3419. _base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
  3420. {
  3421. u32 cntdn, count;
  3422. u32 int_status;
  3423. count = 0;
  3424. cntdn = 2000 * timeout;
  3425. do {
  3426. int_status = readl(&ioc->chip->HostInterruptStatus);
  3427. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  3428. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  3429. "%s: successful count(%d), timeout(%d)\n",
  3430. ioc->name, __func__, count, timeout));
  3431. return 0;
  3432. }
  3433. udelay(500);
  3434. count++;
  3435. } while (--cntdn);
  3436. pr_err(MPT3SAS_FMT
  3437. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  3438. ioc->name, __func__, count, int_status);
  3439. return -EFAULT;
  3440. }
  3441. /**
  3442. * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
  3443. * @ioc: per adapter object
  3444. * @timeout: timeout in second
  3445. *
  3446. * Returns 0 for success, non-zero for failure.
  3447. *
  3448. * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
  3449. * doorbell.
  3450. */
  3451. static int
  3452. _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout)
  3453. {
  3454. u32 cntdn, count;
  3455. u32 int_status;
  3456. u32 doorbell;
  3457. count = 0;
  3458. cntdn = 1000 * timeout;
  3459. do {
  3460. int_status = readl(&ioc->chip->HostInterruptStatus);
  3461. if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
  3462. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  3463. "%s: successful count(%d), timeout(%d)\n",
  3464. ioc->name, __func__, count, timeout));
  3465. return 0;
  3466. } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  3467. doorbell = readl(&ioc->chip->Doorbell);
  3468. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  3469. MPI2_IOC_STATE_FAULT) {
  3470. mpt3sas_base_fault_info(ioc , doorbell);
  3471. return -EFAULT;
  3472. }
  3473. } else if (int_status == 0xFFFFFFFF)
  3474. goto out;
  3475. usleep_range(1000, 1500);
  3476. count++;
  3477. } while (--cntdn);
  3478. out:
  3479. pr_err(MPT3SAS_FMT
  3480. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  3481. ioc->name, __func__, count, int_status);
  3482. return -EFAULT;
  3483. }
  3484. /**
  3485. * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
  3486. * @ioc: per adapter object
  3487. * @timeout: timeout in second
  3488. *
  3489. * Returns 0 for success, non-zero for failure.
  3490. *
  3491. */
  3492. static int
  3493. _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout)
  3494. {
  3495. u32 cntdn, count;
  3496. u32 doorbell_reg;
  3497. count = 0;
  3498. cntdn = 1000 * timeout;
  3499. do {
  3500. doorbell_reg = readl(&ioc->chip->Doorbell);
  3501. if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
  3502. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  3503. "%s: successful count(%d), timeout(%d)\n",
  3504. ioc->name, __func__, count, timeout));
  3505. return 0;
  3506. }
  3507. usleep_range(1000, 1500);
  3508. count++;
  3509. } while (--cntdn);
  3510. pr_err(MPT3SAS_FMT
  3511. "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
  3512. ioc->name, __func__, count, doorbell_reg);
  3513. return -EFAULT;
  3514. }
  3515. /**
  3516. * _base_send_ioc_reset - send doorbell reset
  3517. * @ioc: per adapter object
  3518. * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
  3519. * @timeout: timeout in second
  3520. *
  3521. * Returns 0 for success, non-zero for failure.
  3522. */
  3523. static int
  3524. _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout)
  3525. {
  3526. u32 ioc_state;
  3527. int r = 0;
  3528. if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
  3529. pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
  3530. ioc->name, __func__);
  3531. return -EFAULT;
  3532. }
  3533. if (!(ioc->facts.IOCCapabilities &
  3534. MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
  3535. return -EFAULT;
  3536. pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
  3537. writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
  3538. &ioc->chip->Doorbell);
  3539. if ((_base_wait_for_doorbell_ack(ioc, 15))) {
  3540. r = -EFAULT;
  3541. goto out;
  3542. }
  3543. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
  3544. if (ioc_state) {
  3545. pr_err(MPT3SAS_FMT
  3546. "%s: failed going to ready state (ioc_state=0x%x)\n",
  3547. ioc->name, __func__, ioc_state);
  3548. r = -EFAULT;
  3549. goto out;
  3550. }
  3551. out:
  3552. pr_info(MPT3SAS_FMT "message unit reset: %s\n",
  3553. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  3554. return r;
  3555. }
  3556. /**
  3557. * _base_handshake_req_reply_wait - send request thru doorbell interface
  3558. * @ioc: per adapter object
  3559. * @request_bytes: request length
  3560. * @request: pointer having request payload
  3561. * @reply_bytes: reply length
  3562. * @reply: pointer to reply payload
  3563. * @timeout: timeout in second
  3564. *
  3565. * Returns 0 for success, non-zero for failure.
  3566. */
  3567. static int
  3568. _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
  3569. u32 *request, int reply_bytes, u16 *reply, int timeout)
  3570. {
  3571. MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
  3572. int i;
  3573. u8 failed;
  3574. __le32 *mfp;
  3575. /* make sure doorbell is not in use */
  3576. if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
  3577. pr_err(MPT3SAS_FMT
  3578. "doorbell is in use (line=%d)\n",
  3579. ioc->name, __LINE__);
  3580. return -EFAULT;
  3581. }
  3582. /* clear pending doorbell interrupts from previous state changes */
  3583. if (readl(&ioc->chip->HostInterruptStatus) &
  3584. MPI2_HIS_IOC2SYS_DB_STATUS)
  3585. writel(0, &ioc->chip->HostInterruptStatus);
  3586. /* send message to ioc */
  3587. writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
  3588. ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
  3589. &ioc->chip->Doorbell);
  3590. if ((_base_spin_on_doorbell_int(ioc, 5))) {
  3591. pr_err(MPT3SAS_FMT
  3592. "doorbell handshake int failed (line=%d)\n",
  3593. ioc->name, __LINE__);
  3594. return -EFAULT;
  3595. }
  3596. writel(0, &ioc->chip->HostInterruptStatus);
  3597. if ((_base_wait_for_doorbell_ack(ioc, 5))) {
  3598. pr_err(MPT3SAS_FMT
  3599. "doorbell handshake ack failed (line=%d)\n",
  3600. ioc->name, __LINE__);
  3601. return -EFAULT;
  3602. }
  3603. /* send message 32-bits at a time */
  3604. for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
  3605. writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
  3606. if ((_base_wait_for_doorbell_ack(ioc, 5)))
  3607. failed = 1;
  3608. }
  3609. if (failed) {
  3610. pr_err(MPT3SAS_FMT
  3611. "doorbell handshake sending request failed (line=%d)\n",
  3612. ioc->name, __LINE__);
  3613. return -EFAULT;
  3614. }
  3615. /* now wait for the reply */
  3616. if ((_base_wait_for_doorbell_int(ioc, timeout))) {
  3617. pr_err(MPT3SAS_FMT
  3618. "doorbell handshake int failed (line=%d)\n",
  3619. ioc->name, __LINE__);
  3620. return -EFAULT;
  3621. }
  3622. /* read the first two 16-bits, it gives the total length of the reply */
  3623. reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  3624. & MPI2_DOORBELL_DATA_MASK);
  3625. writel(0, &ioc->chip->HostInterruptStatus);
  3626. if ((_base_wait_for_doorbell_int(ioc, 5))) {
  3627. pr_err(MPT3SAS_FMT
  3628. "doorbell handshake int failed (line=%d)\n",
  3629. ioc->name, __LINE__);
  3630. return -EFAULT;
  3631. }
  3632. reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  3633. & MPI2_DOORBELL_DATA_MASK);
  3634. writel(0, &ioc->chip->HostInterruptStatus);
  3635. for (i = 2; i < default_reply->MsgLength * 2; i++) {
  3636. if ((_base_wait_for_doorbell_int(ioc, 5))) {
  3637. pr_err(MPT3SAS_FMT
  3638. "doorbell handshake int failed (line=%d)\n",
  3639. ioc->name, __LINE__);
  3640. return -EFAULT;
  3641. }
  3642. if (i >= reply_bytes/2) /* overflow case */
  3643. readl(&ioc->chip->Doorbell);
  3644. else
  3645. reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  3646. & MPI2_DOORBELL_DATA_MASK);
  3647. writel(0, &ioc->chip->HostInterruptStatus);
  3648. }
  3649. _base_wait_for_doorbell_int(ioc, 5);
  3650. if (_base_wait_for_doorbell_not_used(ioc, 5) != 0) {
  3651. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  3652. "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
  3653. }
  3654. writel(0, &ioc->chip->HostInterruptStatus);
  3655. if (ioc->logging_level & MPT_DEBUG_INIT) {
  3656. mfp = (__le32 *)reply;
  3657. pr_info("\toffset:data\n");
  3658. for (i = 0; i < reply_bytes/4; i++)
  3659. pr_info("\t[0x%02x]:%08x\n", i*4,
  3660. le32_to_cpu(mfp[i]));
  3661. }
  3662. return 0;
  3663. }
  3664. /**
  3665. * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
  3666. * @ioc: per adapter object
  3667. * @mpi_reply: the reply payload from FW
  3668. * @mpi_request: the request payload sent to FW
  3669. *
  3670. * The SAS IO Unit Control Request message allows the host to perform low-level
  3671. * operations, such as resets on the PHYs of the IO Unit, also allows the host
  3672. * to obtain the IOC assigned device handles for a device if it has other
  3673. * identifying information about the device, in addition allows the host to
  3674. * remove IOC resources associated with the device.
  3675. *
  3676. * Returns 0 for success, non-zero for failure.
  3677. */
  3678. int
  3679. mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
  3680. Mpi2SasIoUnitControlReply_t *mpi_reply,
  3681. Mpi2SasIoUnitControlRequest_t *mpi_request)
  3682. {
  3683. u16 smid;
  3684. u32 ioc_state;
  3685. bool issue_reset = false;
  3686. int rc;
  3687. void *request;
  3688. u16 wait_state_count;
  3689. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3690. __func__));
  3691. mutex_lock(&ioc->base_cmds.mutex);
  3692. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  3693. pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
  3694. ioc->name, __func__);
  3695. rc = -EAGAIN;
  3696. goto out;
  3697. }
  3698. wait_state_count = 0;
  3699. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3700. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  3701. if (wait_state_count++ == 10) {
  3702. pr_err(MPT3SAS_FMT
  3703. "%s: failed due to ioc not operational\n",
  3704. ioc->name, __func__);
  3705. rc = -EFAULT;
  3706. goto out;
  3707. }
  3708. ssleep(1);
  3709. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3710. pr_info(MPT3SAS_FMT
  3711. "%s: waiting for operational state(count=%d)\n",
  3712. ioc->name, __func__, wait_state_count);
  3713. }
  3714. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  3715. if (!smid) {
  3716. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3717. ioc->name, __func__);
  3718. rc = -EAGAIN;
  3719. goto out;
  3720. }
  3721. rc = 0;
  3722. ioc->base_cmds.status = MPT3_CMD_PENDING;
  3723. request = mpt3sas_base_get_msg_frame(ioc, smid);
  3724. ioc->base_cmds.smid = smid;
  3725. memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
  3726. if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  3727. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
  3728. ioc->ioc_link_reset_in_progress = 1;
  3729. init_completion(&ioc->base_cmds.done);
  3730. ioc->put_smid_default(ioc, smid);
  3731. wait_for_completion_timeout(&ioc->base_cmds.done,
  3732. msecs_to_jiffies(10000));
  3733. if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  3734. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
  3735. ioc->ioc_link_reset_in_progress)
  3736. ioc->ioc_link_reset_in_progress = 0;
  3737. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  3738. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3739. ioc->name, __func__);
  3740. _debug_dump_mf(mpi_request,
  3741. sizeof(Mpi2SasIoUnitControlRequest_t)/4);
  3742. if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
  3743. issue_reset = true;
  3744. goto issue_host_reset;
  3745. }
  3746. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  3747. memcpy(mpi_reply, ioc->base_cmds.reply,
  3748. sizeof(Mpi2SasIoUnitControlReply_t));
  3749. else
  3750. memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  3751. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3752. goto out;
  3753. issue_host_reset:
  3754. if (issue_reset)
  3755. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  3756. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3757. rc = -EFAULT;
  3758. out:
  3759. mutex_unlock(&ioc->base_cmds.mutex);
  3760. return rc;
  3761. }
  3762. /**
  3763. * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
  3764. * @ioc: per adapter object
  3765. * @mpi_reply: the reply payload from FW
  3766. * @mpi_request: the request payload sent to FW
  3767. *
  3768. * The SCSI Enclosure Processor request message causes the IOC to
  3769. * communicate with SES devices to control LED status signals.
  3770. *
  3771. * Returns 0 for success, non-zero for failure.
  3772. */
  3773. int
  3774. mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
  3775. Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
  3776. {
  3777. u16 smid;
  3778. u32 ioc_state;
  3779. bool issue_reset = false;
  3780. int rc;
  3781. void *request;
  3782. u16 wait_state_count;
  3783. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3784. __func__));
  3785. mutex_lock(&ioc->base_cmds.mutex);
  3786. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  3787. pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
  3788. ioc->name, __func__);
  3789. rc = -EAGAIN;
  3790. goto out;
  3791. }
  3792. wait_state_count = 0;
  3793. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3794. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  3795. if (wait_state_count++ == 10) {
  3796. pr_err(MPT3SAS_FMT
  3797. "%s: failed due to ioc not operational\n",
  3798. ioc->name, __func__);
  3799. rc = -EFAULT;
  3800. goto out;
  3801. }
  3802. ssleep(1);
  3803. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3804. pr_info(MPT3SAS_FMT
  3805. "%s: waiting for operational state(count=%d)\n",
  3806. ioc->name,
  3807. __func__, wait_state_count);
  3808. }
  3809. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  3810. if (!smid) {
  3811. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3812. ioc->name, __func__);
  3813. rc = -EAGAIN;
  3814. goto out;
  3815. }
  3816. rc = 0;
  3817. ioc->base_cmds.status = MPT3_CMD_PENDING;
  3818. request = mpt3sas_base_get_msg_frame(ioc, smid);
  3819. ioc->base_cmds.smid = smid;
  3820. memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
  3821. init_completion(&ioc->base_cmds.done);
  3822. ioc->put_smid_default(ioc, smid);
  3823. wait_for_completion_timeout(&ioc->base_cmds.done,
  3824. msecs_to_jiffies(10000));
  3825. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  3826. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3827. ioc->name, __func__);
  3828. _debug_dump_mf(mpi_request,
  3829. sizeof(Mpi2SepRequest_t)/4);
  3830. if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
  3831. issue_reset = false;
  3832. goto issue_host_reset;
  3833. }
  3834. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  3835. memcpy(mpi_reply, ioc->base_cmds.reply,
  3836. sizeof(Mpi2SepReply_t));
  3837. else
  3838. memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
  3839. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3840. goto out;
  3841. issue_host_reset:
  3842. if (issue_reset)
  3843. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  3844. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3845. rc = -EFAULT;
  3846. out:
  3847. mutex_unlock(&ioc->base_cmds.mutex);
  3848. return rc;
  3849. }
  3850. /**
  3851. * _base_get_port_facts - obtain port facts reply and save in ioc
  3852. * @ioc: per adapter object
  3853. *
  3854. * Returns 0 for success, non-zero for failure.
  3855. */
  3856. static int
  3857. _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port)
  3858. {
  3859. Mpi2PortFactsRequest_t mpi_request;
  3860. Mpi2PortFactsReply_t mpi_reply;
  3861. struct mpt3sas_port_facts *pfacts;
  3862. int mpi_reply_sz, mpi_request_sz, r;
  3863. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3864. __func__));
  3865. mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
  3866. mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
  3867. memset(&mpi_request, 0, mpi_request_sz);
  3868. mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
  3869. mpi_request.PortNumber = port;
  3870. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3871. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
  3872. if (r != 0) {
  3873. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  3874. ioc->name, __func__, r);
  3875. return r;
  3876. }
  3877. pfacts = &ioc->pfacts[port];
  3878. memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
  3879. pfacts->PortNumber = mpi_reply.PortNumber;
  3880. pfacts->VP_ID = mpi_reply.VP_ID;
  3881. pfacts->VF_ID = mpi_reply.VF_ID;
  3882. pfacts->MaxPostedCmdBuffers =
  3883. le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
  3884. return 0;
  3885. }
  3886. /**
  3887. * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
  3888. * @ioc: per adapter object
  3889. * @timeout:
  3890. *
  3891. * Returns 0 for success, non-zero for failure.
  3892. */
  3893. static int
  3894. _base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout)
  3895. {
  3896. u32 ioc_state;
  3897. int rc;
  3898. dinitprintk(ioc, printk(MPT3SAS_FMT "%s\n", ioc->name,
  3899. __func__));
  3900. if (ioc->pci_error_recovery) {
  3901. dfailprintk(ioc, printk(MPT3SAS_FMT
  3902. "%s: host in pci error recovery\n", ioc->name, __func__));
  3903. return -EFAULT;
  3904. }
  3905. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  3906. dhsprintk(ioc, printk(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
  3907. ioc->name, __func__, ioc_state));
  3908. if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
  3909. (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  3910. return 0;
  3911. if (ioc_state & MPI2_DOORBELL_USED) {
  3912. dhsprintk(ioc, printk(MPT3SAS_FMT
  3913. "unexpected doorbell active!\n", ioc->name));
  3914. goto issue_diag_reset;
  3915. }
  3916. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  3917. mpt3sas_base_fault_info(ioc, ioc_state &
  3918. MPI2_DOORBELL_DATA_MASK);
  3919. goto issue_diag_reset;
  3920. }
  3921. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
  3922. if (ioc_state) {
  3923. dfailprintk(ioc, printk(MPT3SAS_FMT
  3924. "%s: failed going to ready state (ioc_state=0x%x)\n",
  3925. ioc->name, __func__, ioc_state));
  3926. return -EFAULT;
  3927. }
  3928. issue_diag_reset:
  3929. rc = _base_diag_reset(ioc);
  3930. return rc;
  3931. }
  3932. /**
  3933. * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
  3934. * @ioc: per adapter object
  3935. *
  3936. * Returns 0 for success, non-zero for failure.
  3937. */
  3938. static int
  3939. _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc)
  3940. {
  3941. Mpi2IOCFactsRequest_t mpi_request;
  3942. Mpi2IOCFactsReply_t mpi_reply;
  3943. struct mpt3sas_facts *facts;
  3944. int mpi_reply_sz, mpi_request_sz, r;
  3945. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3946. __func__));
  3947. r = _base_wait_for_iocstate(ioc, 10);
  3948. if (r) {
  3949. dfailprintk(ioc, printk(MPT3SAS_FMT
  3950. "%s: failed getting to correct state\n",
  3951. ioc->name, __func__));
  3952. return r;
  3953. }
  3954. mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
  3955. mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
  3956. memset(&mpi_request, 0, mpi_request_sz);
  3957. mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
  3958. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3959. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
  3960. if (r != 0) {
  3961. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  3962. ioc->name, __func__, r);
  3963. return r;
  3964. }
  3965. facts = &ioc->facts;
  3966. memset(facts, 0, sizeof(struct mpt3sas_facts));
  3967. facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
  3968. facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
  3969. facts->VP_ID = mpi_reply.VP_ID;
  3970. facts->VF_ID = mpi_reply.VF_ID;
  3971. facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
  3972. facts->MaxChainDepth = mpi_reply.MaxChainDepth;
  3973. facts->WhoInit = mpi_reply.WhoInit;
  3974. facts->NumberOfPorts = mpi_reply.NumberOfPorts;
  3975. facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
  3976. facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
  3977. facts->MaxReplyDescriptorPostQueueDepth =
  3978. le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
  3979. facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
  3980. facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
  3981. if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
  3982. ioc->ir_firmware = 1;
  3983. if ((facts->IOCCapabilities &
  3984. MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE))
  3985. ioc->rdpq_array_capable = 1;
  3986. if (facts->IOCCapabilities & MPI26_IOCFACTS_CAPABILITY_ATOMIC_REQ)
  3987. ioc->atomic_desc_capable = 1;
  3988. facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
  3989. facts->IOCRequestFrameSize =
  3990. le16_to_cpu(mpi_reply.IOCRequestFrameSize);
  3991. if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  3992. facts->IOCMaxChainSegmentSize =
  3993. le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
  3994. }
  3995. facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
  3996. facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
  3997. ioc->shost->max_id = -1;
  3998. facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
  3999. facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
  4000. facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
  4001. facts->HighPriorityCredit =
  4002. le16_to_cpu(mpi_reply.HighPriorityCredit);
  4003. facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
  4004. facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
  4005. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4006. "hba queue depth(%d), max chains per io(%d)\n",
  4007. ioc->name, facts->RequestCredit,
  4008. facts->MaxChainDepth));
  4009. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4010. "request frame size(%d), reply frame size(%d)\n", ioc->name,
  4011. facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
  4012. return 0;
  4013. }
  4014. /**
  4015. * _base_send_ioc_init - send ioc_init to firmware
  4016. * @ioc: per adapter object
  4017. *
  4018. * Returns 0 for success, non-zero for failure.
  4019. */
  4020. static int
  4021. _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc)
  4022. {
  4023. Mpi2IOCInitRequest_t mpi_request;
  4024. Mpi2IOCInitReply_t mpi_reply;
  4025. int i, r = 0;
  4026. ktime_t current_time;
  4027. u16 ioc_status;
  4028. u32 reply_post_free_array_sz = 0;
  4029. Mpi2IOCInitRDPQArrayEntry *reply_post_free_array = NULL;
  4030. dma_addr_t reply_post_free_array_dma;
  4031. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4032. __func__));
  4033. memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
  4034. mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
  4035. mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  4036. mpi_request.VF_ID = 0; /* TODO */
  4037. mpi_request.VP_ID = 0;
  4038. mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
  4039. mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  4040. if (_base_is_controller_msix_enabled(ioc))
  4041. mpi_request.HostMSIxVectors = ioc->reply_queue_count;
  4042. mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
  4043. mpi_request.ReplyDescriptorPostQueueDepth =
  4044. cpu_to_le16(ioc->reply_post_queue_depth);
  4045. mpi_request.ReplyFreeQueueDepth =
  4046. cpu_to_le16(ioc->reply_free_queue_depth);
  4047. mpi_request.SenseBufferAddressHigh =
  4048. cpu_to_le32((u64)ioc->sense_dma >> 32);
  4049. mpi_request.SystemReplyAddressHigh =
  4050. cpu_to_le32((u64)ioc->reply_dma >> 32);
  4051. mpi_request.SystemRequestFrameBaseAddress =
  4052. cpu_to_le64((u64)ioc->request_dma);
  4053. mpi_request.ReplyFreeQueueAddress =
  4054. cpu_to_le64((u64)ioc->reply_free_dma);
  4055. if (ioc->rdpq_array_enable) {
  4056. reply_post_free_array_sz = ioc->reply_queue_count *
  4057. sizeof(Mpi2IOCInitRDPQArrayEntry);
  4058. reply_post_free_array = pci_alloc_consistent(ioc->pdev,
  4059. reply_post_free_array_sz, &reply_post_free_array_dma);
  4060. if (!reply_post_free_array) {
  4061. pr_err(MPT3SAS_FMT
  4062. "reply_post_free_array: pci_alloc_consistent failed\n",
  4063. ioc->name);
  4064. r = -ENOMEM;
  4065. goto out;
  4066. }
  4067. memset(reply_post_free_array, 0, reply_post_free_array_sz);
  4068. for (i = 0; i < ioc->reply_queue_count; i++)
  4069. reply_post_free_array[i].RDPQBaseAddress =
  4070. cpu_to_le64(
  4071. (u64)ioc->reply_post[i].reply_post_free_dma);
  4072. mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
  4073. mpi_request.ReplyDescriptorPostQueueAddress =
  4074. cpu_to_le64((u64)reply_post_free_array_dma);
  4075. } else {
  4076. mpi_request.ReplyDescriptorPostQueueAddress =
  4077. cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
  4078. }
  4079. /* This time stamp specifies number of milliseconds
  4080. * since epoch ~ midnight January 1, 1970.
  4081. */
  4082. current_time = ktime_get_real();
  4083. mpi_request.TimeStamp = cpu_to_le64(ktime_to_ms(current_time));
  4084. if (ioc->logging_level & MPT_DEBUG_INIT) {
  4085. __le32 *mfp;
  4086. int i;
  4087. mfp = (__le32 *)&mpi_request;
  4088. pr_info("\toffset:data\n");
  4089. for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
  4090. pr_info("\t[0x%02x]:%08x\n", i*4,
  4091. le32_to_cpu(mfp[i]));
  4092. }
  4093. r = _base_handshake_req_reply_wait(ioc,
  4094. sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
  4095. sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10);
  4096. if (r != 0) {
  4097. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  4098. ioc->name, __func__, r);
  4099. goto out;
  4100. }
  4101. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  4102. if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
  4103. mpi_reply.IOCLogInfo) {
  4104. pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
  4105. r = -EIO;
  4106. }
  4107. out:
  4108. if (reply_post_free_array)
  4109. pci_free_consistent(ioc->pdev, reply_post_free_array_sz,
  4110. reply_post_free_array,
  4111. reply_post_free_array_dma);
  4112. return r;
  4113. }
  4114. /**
  4115. * mpt3sas_port_enable_done - command completion routine for port enable
  4116. * @ioc: per adapter object
  4117. * @smid: system request message index
  4118. * @msix_index: MSIX table index supplied by the OS
  4119. * @reply: reply message frame(lower 32bit addr)
  4120. *
  4121. * Return 1 meaning mf should be freed from _base_interrupt
  4122. * 0 means the mf is freed from this function.
  4123. */
  4124. u8
  4125. mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  4126. u32 reply)
  4127. {
  4128. MPI2DefaultReply_t *mpi_reply;
  4129. u16 ioc_status;
  4130. if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
  4131. return 1;
  4132. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  4133. if (!mpi_reply)
  4134. return 1;
  4135. if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
  4136. return 1;
  4137. ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
  4138. ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
  4139. ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
  4140. memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  4141. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  4142. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  4143. ioc->port_enable_failed = 1;
  4144. if (ioc->is_driver_loading) {
  4145. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  4146. mpt3sas_port_enable_complete(ioc);
  4147. return 1;
  4148. } else {
  4149. ioc->start_scan_failed = ioc_status;
  4150. ioc->start_scan = 0;
  4151. return 1;
  4152. }
  4153. }
  4154. complete(&ioc->port_enable_cmds.done);
  4155. return 1;
  4156. }
  4157. /**
  4158. * _base_send_port_enable - send port_enable(discovery stuff) to firmware
  4159. * @ioc: per adapter object
  4160. *
  4161. * Returns 0 for success, non-zero for failure.
  4162. */
  4163. static int
  4164. _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc)
  4165. {
  4166. Mpi2PortEnableRequest_t *mpi_request;
  4167. Mpi2PortEnableReply_t *mpi_reply;
  4168. int r = 0;
  4169. u16 smid;
  4170. u16 ioc_status;
  4171. pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
  4172. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  4173. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  4174. ioc->name, __func__);
  4175. return -EAGAIN;
  4176. }
  4177. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  4178. if (!smid) {
  4179. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  4180. ioc->name, __func__);
  4181. return -EAGAIN;
  4182. }
  4183. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  4184. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  4185. ioc->port_enable_cmds.smid = smid;
  4186. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  4187. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  4188. init_completion(&ioc->port_enable_cmds.done);
  4189. ioc->put_smid_default(ioc, smid);
  4190. wait_for_completion_timeout(&ioc->port_enable_cmds.done, 300*HZ);
  4191. if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
  4192. pr_err(MPT3SAS_FMT "%s: timeout\n",
  4193. ioc->name, __func__);
  4194. _debug_dump_mf(mpi_request,
  4195. sizeof(Mpi2PortEnableRequest_t)/4);
  4196. if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
  4197. r = -EFAULT;
  4198. else
  4199. r = -ETIME;
  4200. goto out;
  4201. }
  4202. mpi_reply = ioc->port_enable_cmds.reply;
  4203. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  4204. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4205. pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
  4206. ioc->name, __func__, ioc_status);
  4207. r = -EFAULT;
  4208. goto out;
  4209. }
  4210. out:
  4211. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  4212. pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
  4213. "SUCCESS" : "FAILED"));
  4214. return r;
  4215. }
  4216. /**
  4217. * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
  4218. * @ioc: per adapter object
  4219. *
  4220. * Returns 0 for success, non-zero for failure.
  4221. */
  4222. int
  4223. mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
  4224. {
  4225. Mpi2PortEnableRequest_t *mpi_request;
  4226. u16 smid;
  4227. pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
  4228. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  4229. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  4230. ioc->name, __func__);
  4231. return -EAGAIN;
  4232. }
  4233. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  4234. if (!smid) {
  4235. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  4236. ioc->name, __func__);
  4237. return -EAGAIN;
  4238. }
  4239. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  4240. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  4241. ioc->port_enable_cmds.smid = smid;
  4242. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  4243. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  4244. ioc->put_smid_default(ioc, smid);
  4245. return 0;
  4246. }
  4247. /**
  4248. * _base_determine_wait_on_discovery - desposition
  4249. * @ioc: per adapter object
  4250. *
  4251. * Decide whether to wait on discovery to complete. Used to either
  4252. * locate boot device, or report volumes ahead of physical devices.
  4253. *
  4254. * Returns 1 for wait, 0 for don't wait
  4255. */
  4256. static int
  4257. _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
  4258. {
  4259. /* We wait for discovery to complete if IR firmware is loaded.
  4260. * The sas topology events arrive before PD events, so we need time to
  4261. * turn on the bit in ioc->pd_handles to indicate PD
  4262. * Also, it maybe required to report Volumes ahead of physical
  4263. * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
  4264. */
  4265. if (ioc->ir_firmware)
  4266. return 1;
  4267. /* if no Bios, then we don't need to wait */
  4268. if (!ioc->bios_pg3.BiosVersion)
  4269. return 0;
  4270. /* Bios is present, then we drop down here.
  4271. *
  4272. * If there any entries in the Bios Page 2, then we wait
  4273. * for discovery to complete.
  4274. */
  4275. /* Current Boot Device */
  4276. if ((ioc->bios_pg2.CurrentBootDeviceForm &
  4277. MPI2_BIOSPAGE2_FORM_MASK) ==
  4278. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  4279. /* Request Boot Device */
  4280. (ioc->bios_pg2.ReqBootDeviceForm &
  4281. MPI2_BIOSPAGE2_FORM_MASK) ==
  4282. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  4283. /* Alternate Request Boot Device */
  4284. (ioc->bios_pg2.ReqAltBootDeviceForm &
  4285. MPI2_BIOSPAGE2_FORM_MASK) ==
  4286. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
  4287. return 0;
  4288. return 1;
  4289. }
  4290. /**
  4291. * _base_unmask_events - turn on notification for this event
  4292. * @ioc: per adapter object
  4293. * @event: firmware event
  4294. *
  4295. * The mask is stored in ioc->event_masks.
  4296. */
  4297. static void
  4298. _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
  4299. {
  4300. u32 desired_event;
  4301. if (event >= 128)
  4302. return;
  4303. desired_event = (1 << (event % 32));
  4304. if (event < 32)
  4305. ioc->event_masks[0] &= ~desired_event;
  4306. else if (event < 64)
  4307. ioc->event_masks[1] &= ~desired_event;
  4308. else if (event < 96)
  4309. ioc->event_masks[2] &= ~desired_event;
  4310. else if (event < 128)
  4311. ioc->event_masks[3] &= ~desired_event;
  4312. }
  4313. /**
  4314. * _base_event_notification - send event notification
  4315. * @ioc: per adapter object
  4316. *
  4317. * Returns 0 for success, non-zero for failure.
  4318. */
  4319. static int
  4320. _base_event_notification(struct MPT3SAS_ADAPTER *ioc)
  4321. {
  4322. Mpi2EventNotificationRequest_t *mpi_request;
  4323. u16 smid;
  4324. int r = 0;
  4325. int i;
  4326. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4327. __func__));
  4328. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  4329. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  4330. ioc->name, __func__);
  4331. return -EAGAIN;
  4332. }
  4333. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  4334. if (!smid) {
  4335. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  4336. ioc->name, __func__);
  4337. return -EAGAIN;
  4338. }
  4339. ioc->base_cmds.status = MPT3_CMD_PENDING;
  4340. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  4341. ioc->base_cmds.smid = smid;
  4342. memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
  4343. mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  4344. mpi_request->VF_ID = 0; /* TODO */
  4345. mpi_request->VP_ID = 0;
  4346. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  4347. mpi_request->EventMasks[i] =
  4348. cpu_to_le32(ioc->event_masks[i]);
  4349. init_completion(&ioc->base_cmds.done);
  4350. ioc->put_smid_default(ioc, smid);
  4351. wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
  4352. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  4353. pr_err(MPT3SAS_FMT "%s: timeout\n",
  4354. ioc->name, __func__);
  4355. _debug_dump_mf(mpi_request,
  4356. sizeof(Mpi2EventNotificationRequest_t)/4);
  4357. if (ioc->base_cmds.status & MPT3_CMD_RESET)
  4358. r = -EFAULT;
  4359. else
  4360. r = -ETIME;
  4361. } else
  4362. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
  4363. ioc->name, __func__));
  4364. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  4365. return r;
  4366. }
  4367. /**
  4368. * mpt3sas_base_validate_event_type - validating event types
  4369. * @ioc: per adapter object
  4370. * @event: firmware event
  4371. *
  4372. * This will turn on firmware event notification when application
  4373. * ask for that event. We don't mask events that are already enabled.
  4374. */
  4375. void
  4376. mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
  4377. {
  4378. int i, j;
  4379. u32 event_mask, desired_event;
  4380. u8 send_update_to_fw;
  4381. for (i = 0, send_update_to_fw = 0; i <
  4382. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
  4383. event_mask = ~event_type[i];
  4384. desired_event = 1;
  4385. for (j = 0; j < 32; j++) {
  4386. if (!(event_mask & desired_event) &&
  4387. (ioc->event_masks[i] & desired_event)) {
  4388. ioc->event_masks[i] &= ~desired_event;
  4389. send_update_to_fw = 1;
  4390. }
  4391. desired_event = (desired_event << 1);
  4392. }
  4393. }
  4394. if (!send_update_to_fw)
  4395. return;
  4396. mutex_lock(&ioc->base_cmds.mutex);
  4397. _base_event_notification(ioc);
  4398. mutex_unlock(&ioc->base_cmds.mutex);
  4399. }
  4400. /**
  4401. * _base_diag_reset - the "big hammer" start of day reset
  4402. * @ioc: per adapter object
  4403. *
  4404. * Returns 0 for success, non-zero for failure.
  4405. */
  4406. static int
  4407. _base_diag_reset(struct MPT3SAS_ADAPTER *ioc)
  4408. {
  4409. u32 host_diagnostic;
  4410. u32 ioc_state;
  4411. u32 count;
  4412. u32 hcb_size;
  4413. pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
  4414. drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
  4415. ioc->name));
  4416. count = 0;
  4417. do {
  4418. /* Write magic sequence to WriteSequence register
  4419. * Loop until in diagnostic mode
  4420. */
  4421. drsprintk(ioc, pr_info(MPT3SAS_FMT
  4422. "write magic sequence\n", ioc->name));
  4423. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  4424. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
  4425. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
  4426. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
  4427. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
  4428. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
  4429. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
  4430. /* wait 100 msec */
  4431. msleep(100);
  4432. if (count++ > 20)
  4433. goto out;
  4434. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  4435. drsprintk(ioc, pr_info(MPT3SAS_FMT
  4436. "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
  4437. ioc->name, count, host_diagnostic));
  4438. } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
  4439. hcb_size = readl(&ioc->chip->HCBSize);
  4440. drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
  4441. ioc->name));
  4442. writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
  4443. &ioc->chip->HostDiagnostic);
  4444. /*This delay allows the chip PCIe hardware time to finish reset tasks*/
  4445. msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
  4446. /* Approximately 300 second max wait */
  4447. for (count = 0; count < (300000000 /
  4448. MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
  4449. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  4450. if (host_diagnostic == 0xFFFFFFFF)
  4451. goto out;
  4452. if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
  4453. break;
  4454. msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC / 1000);
  4455. }
  4456. if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
  4457. drsprintk(ioc, pr_info(MPT3SAS_FMT
  4458. "restart the adapter assuming the HCB Address points to good F/W\n",
  4459. ioc->name));
  4460. host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
  4461. host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
  4462. writel(host_diagnostic, &ioc->chip->HostDiagnostic);
  4463. drsprintk(ioc, pr_info(MPT3SAS_FMT
  4464. "re-enable the HCDW\n", ioc->name));
  4465. writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
  4466. &ioc->chip->HCBSize);
  4467. }
  4468. drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
  4469. ioc->name));
  4470. writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
  4471. &ioc->chip->HostDiagnostic);
  4472. drsprintk(ioc, pr_info(MPT3SAS_FMT
  4473. "disable writes to the diagnostic register\n", ioc->name));
  4474. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  4475. drsprintk(ioc, pr_info(MPT3SAS_FMT
  4476. "Wait for FW to go to the READY state\n", ioc->name));
  4477. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20);
  4478. if (ioc_state) {
  4479. pr_err(MPT3SAS_FMT
  4480. "%s: failed going to ready state (ioc_state=0x%x)\n",
  4481. ioc->name, __func__, ioc_state);
  4482. goto out;
  4483. }
  4484. pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
  4485. return 0;
  4486. out:
  4487. pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
  4488. return -EFAULT;
  4489. }
  4490. /**
  4491. * _base_make_ioc_ready - put controller in READY state
  4492. * @ioc: per adapter object
  4493. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  4494. *
  4495. * Returns 0 for success, non-zero for failure.
  4496. */
  4497. static int
  4498. _base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
  4499. {
  4500. u32 ioc_state;
  4501. int rc;
  4502. int count;
  4503. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4504. __func__));
  4505. if (ioc->pci_error_recovery)
  4506. return 0;
  4507. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  4508. dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
  4509. ioc->name, __func__, ioc_state));
  4510. /* if in RESET state, it should move to READY state shortly */
  4511. count = 0;
  4512. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
  4513. while ((ioc_state & MPI2_IOC_STATE_MASK) !=
  4514. MPI2_IOC_STATE_READY) {
  4515. if (count++ == 10) {
  4516. pr_err(MPT3SAS_FMT
  4517. "%s: failed going to ready state (ioc_state=0x%x)\n",
  4518. ioc->name, __func__, ioc_state);
  4519. return -EFAULT;
  4520. }
  4521. ssleep(1);
  4522. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  4523. }
  4524. }
  4525. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
  4526. return 0;
  4527. if (ioc_state & MPI2_DOORBELL_USED) {
  4528. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  4529. "unexpected doorbell active!\n",
  4530. ioc->name));
  4531. goto issue_diag_reset;
  4532. }
  4533. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  4534. mpt3sas_base_fault_info(ioc, ioc_state &
  4535. MPI2_DOORBELL_DATA_MASK);
  4536. goto issue_diag_reset;
  4537. }
  4538. if (type == FORCE_BIG_HAMMER)
  4539. goto issue_diag_reset;
  4540. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  4541. if (!(_base_send_ioc_reset(ioc,
  4542. MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15))) {
  4543. return 0;
  4544. }
  4545. issue_diag_reset:
  4546. rc = _base_diag_reset(ioc);
  4547. return rc;
  4548. }
  4549. /**
  4550. * _base_make_ioc_operational - put controller in OPERATIONAL state
  4551. * @ioc: per adapter object
  4552. *
  4553. * Returns 0 for success, non-zero for failure.
  4554. */
  4555. static int
  4556. _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc)
  4557. {
  4558. int r, i, index;
  4559. unsigned long flags;
  4560. u32 reply_address;
  4561. u16 smid;
  4562. struct _tr_list *delayed_tr, *delayed_tr_next;
  4563. struct _sc_list *delayed_sc, *delayed_sc_next;
  4564. struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
  4565. u8 hide_flag;
  4566. struct adapter_reply_queue *reply_q;
  4567. Mpi2ReplyDescriptorsUnion_t *reply_post_free_contig;
  4568. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4569. __func__));
  4570. /* clean the delayed target reset list */
  4571. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  4572. &ioc->delayed_tr_list, list) {
  4573. list_del(&delayed_tr->list);
  4574. kfree(delayed_tr);
  4575. }
  4576. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  4577. &ioc->delayed_tr_volume_list, list) {
  4578. list_del(&delayed_tr->list);
  4579. kfree(delayed_tr);
  4580. }
  4581. list_for_each_entry_safe(delayed_sc, delayed_sc_next,
  4582. &ioc->delayed_sc_list, list) {
  4583. list_del(&delayed_sc->list);
  4584. kfree(delayed_sc);
  4585. }
  4586. list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
  4587. &ioc->delayed_event_ack_list, list) {
  4588. list_del(&delayed_event_ack->list);
  4589. kfree(delayed_event_ack);
  4590. }
  4591. /* initialize the scsi lookup free list */
  4592. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  4593. INIT_LIST_HEAD(&ioc->free_list);
  4594. smid = 1;
  4595. for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
  4596. INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
  4597. ioc->scsi_lookup[i].cb_idx = 0xFF;
  4598. ioc->scsi_lookup[i].smid = smid;
  4599. ioc->scsi_lookup[i].scmd = NULL;
  4600. ioc->scsi_lookup[i].direct_io = 0;
  4601. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  4602. &ioc->free_list);
  4603. }
  4604. /* hi-priority queue */
  4605. INIT_LIST_HEAD(&ioc->hpr_free_list);
  4606. smid = ioc->hi_priority_smid;
  4607. for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
  4608. ioc->hpr_lookup[i].cb_idx = 0xFF;
  4609. ioc->hpr_lookup[i].smid = smid;
  4610. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  4611. &ioc->hpr_free_list);
  4612. }
  4613. /* internal queue */
  4614. INIT_LIST_HEAD(&ioc->internal_free_list);
  4615. smid = ioc->internal_smid;
  4616. for (i = 0; i < ioc->internal_depth; i++, smid++) {
  4617. ioc->internal_lookup[i].cb_idx = 0xFF;
  4618. ioc->internal_lookup[i].smid = smid;
  4619. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  4620. &ioc->internal_free_list);
  4621. }
  4622. /* chain pool */
  4623. INIT_LIST_HEAD(&ioc->free_chain_list);
  4624. for (i = 0; i < ioc->chain_depth; i++)
  4625. list_add_tail(&ioc->chain_lookup[i].tracker_list,
  4626. &ioc->free_chain_list);
  4627. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  4628. /* initialize Reply Free Queue */
  4629. for (i = 0, reply_address = (u32)ioc->reply_dma ;
  4630. i < ioc->reply_free_queue_depth ; i++, reply_address +=
  4631. ioc->reply_sz)
  4632. ioc->reply_free[i] = cpu_to_le32(reply_address);
  4633. /* initialize reply queues */
  4634. if (ioc->is_driver_loading)
  4635. _base_assign_reply_queues(ioc);
  4636. /* initialize Reply Post Free Queue */
  4637. index = 0;
  4638. reply_post_free_contig = ioc->reply_post[0].reply_post_free;
  4639. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  4640. /*
  4641. * If RDPQ is enabled, switch to the next allocation.
  4642. * Otherwise advance within the contiguous region.
  4643. */
  4644. if (ioc->rdpq_array_enable) {
  4645. reply_q->reply_post_free =
  4646. ioc->reply_post[index++].reply_post_free;
  4647. } else {
  4648. reply_q->reply_post_free = reply_post_free_contig;
  4649. reply_post_free_contig += ioc->reply_post_queue_depth;
  4650. }
  4651. reply_q->reply_post_host_index = 0;
  4652. for (i = 0; i < ioc->reply_post_queue_depth; i++)
  4653. reply_q->reply_post_free[i].Words =
  4654. cpu_to_le64(ULLONG_MAX);
  4655. if (!_base_is_controller_msix_enabled(ioc))
  4656. goto skip_init_reply_post_free_queue;
  4657. }
  4658. skip_init_reply_post_free_queue:
  4659. r = _base_send_ioc_init(ioc);
  4660. if (r)
  4661. return r;
  4662. /* initialize reply free host index */
  4663. ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
  4664. writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
  4665. /* initialize reply post host index */
  4666. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  4667. if (ioc->combined_reply_queue)
  4668. writel((reply_q->msix_index & 7)<<
  4669. MPI2_RPHI_MSIX_INDEX_SHIFT,
  4670. ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
  4671. else
  4672. writel(reply_q->msix_index <<
  4673. MPI2_RPHI_MSIX_INDEX_SHIFT,
  4674. &ioc->chip->ReplyPostHostIndex);
  4675. if (!_base_is_controller_msix_enabled(ioc))
  4676. goto skip_init_reply_post_host_index;
  4677. }
  4678. skip_init_reply_post_host_index:
  4679. _base_unmask_interrupts(ioc);
  4680. r = _base_event_notification(ioc);
  4681. if (r)
  4682. return r;
  4683. _base_static_config_pages(ioc);
  4684. if (ioc->is_driver_loading) {
  4685. if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
  4686. == 0x80) {
  4687. hide_flag = (u8) (
  4688. le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
  4689. MFG_PAGE10_HIDE_SSDS_MASK);
  4690. if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
  4691. ioc->mfg_pg10_hide_flag = hide_flag;
  4692. }
  4693. ioc->wait_for_discovery_to_complete =
  4694. _base_determine_wait_on_discovery(ioc);
  4695. return r; /* scan_start and scan_finished support */
  4696. }
  4697. r = _base_send_port_enable(ioc);
  4698. if (r)
  4699. return r;
  4700. return r;
  4701. }
  4702. /**
  4703. * mpt3sas_base_free_resources - free resources controller resources
  4704. * @ioc: per adapter object
  4705. *
  4706. * Return nothing.
  4707. */
  4708. void
  4709. mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
  4710. {
  4711. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4712. __func__));
  4713. /* synchronizing freeing resource with pci_access_mutex lock */
  4714. mutex_lock(&ioc->pci_access_mutex);
  4715. if (ioc->chip_phys && ioc->chip) {
  4716. _base_mask_interrupts(ioc);
  4717. ioc->shost_recovery = 1;
  4718. _base_make_ioc_ready(ioc, SOFT_RESET);
  4719. ioc->shost_recovery = 0;
  4720. }
  4721. mpt3sas_base_unmap_resources(ioc);
  4722. mutex_unlock(&ioc->pci_access_mutex);
  4723. return;
  4724. }
  4725. /**
  4726. * mpt3sas_base_attach - attach controller instance
  4727. * @ioc: per adapter object
  4728. *
  4729. * Returns 0 for success, non-zero for failure.
  4730. */
  4731. int
  4732. mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
  4733. {
  4734. int r, i;
  4735. int cpu_id, last_cpu_id = 0;
  4736. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4737. __func__));
  4738. /* setup cpu_msix_table */
  4739. ioc->cpu_count = num_online_cpus();
  4740. for_each_online_cpu(cpu_id)
  4741. last_cpu_id = cpu_id;
  4742. ioc->cpu_msix_table_sz = last_cpu_id + 1;
  4743. ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
  4744. ioc->reply_queue_count = 1;
  4745. if (!ioc->cpu_msix_table) {
  4746. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  4747. "allocation for cpu_msix_table failed!!!\n",
  4748. ioc->name));
  4749. r = -ENOMEM;
  4750. goto out_free_resources;
  4751. }
  4752. if (ioc->is_warpdrive) {
  4753. ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
  4754. sizeof(resource_size_t *), GFP_KERNEL);
  4755. if (!ioc->reply_post_host_index) {
  4756. dfailprintk(ioc, pr_info(MPT3SAS_FMT "allocation "
  4757. "for cpu_msix_table failed!!!\n", ioc->name));
  4758. r = -ENOMEM;
  4759. goto out_free_resources;
  4760. }
  4761. }
  4762. ioc->rdpq_array_enable_assigned = 0;
  4763. ioc->dma_mask = 0;
  4764. r = mpt3sas_base_map_resources(ioc);
  4765. if (r)
  4766. goto out_free_resources;
  4767. pci_set_drvdata(ioc->pdev, ioc->shost);
  4768. r = _base_get_ioc_facts(ioc);
  4769. if (r)
  4770. goto out_free_resources;
  4771. switch (ioc->hba_mpi_version_belonged) {
  4772. case MPI2_VERSION:
  4773. ioc->build_sg_scmd = &_base_build_sg_scmd;
  4774. ioc->build_sg = &_base_build_sg;
  4775. ioc->build_zero_len_sge = &_base_build_zero_len_sge;
  4776. break;
  4777. case MPI25_VERSION:
  4778. case MPI26_VERSION:
  4779. /*
  4780. * In SAS3.0,
  4781. * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
  4782. * Target Status - all require the IEEE formated scatter gather
  4783. * elements.
  4784. */
  4785. ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
  4786. ioc->build_sg = &_base_build_sg_ieee;
  4787. ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
  4788. ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
  4789. break;
  4790. }
  4791. if (ioc->atomic_desc_capable) {
  4792. ioc->put_smid_default = &_base_put_smid_default_atomic;
  4793. ioc->put_smid_scsi_io = &_base_put_smid_scsi_io_atomic;
  4794. ioc->put_smid_fast_path = &_base_put_smid_fast_path_atomic;
  4795. ioc->put_smid_hi_priority = &_base_put_smid_hi_priority_atomic;
  4796. } else {
  4797. ioc->put_smid_default = &_base_put_smid_default;
  4798. ioc->put_smid_scsi_io = &_base_put_smid_scsi_io;
  4799. ioc->put_smid_fast_path = &_base_put_smid_fast_path;
  4800. ioc->put_smid_hi_priority = &_base_put_smid_hi_priority;
  4801. }
  4802. /*
  4803. * These function pointers for other requests that don't
  4804. * the require IEEE scatter gather elements.
  4805. *
  4806. * For example Configuration Pages and SAS IOUNIT Control don't.
  4807. */
  4808. ioc->build_sg_mpi = &_base_build_sg;
  4809. ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
  4810. r = _base_make_ioc_ready(ioc, SOFT_RESET);
  4811. if (r)
  4812. goto out_free_resources;
  4813. ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
  4814. sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
  4815. if (!ioc->pfacts) {
  4816. r = -ENOMEM;
  4817. goto out_free_resources;
  4818. }
  4819. for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
  4820. r = _base_get_port_facts(ioc, i);
  4821. if (r)
  4822. goto out_free_resources;
  4823. }
  4824. r = _base_allocate_memory_pools(ioc);
  4825. if (r)
  4826. goto out_free_resources;
  4827. init_waitqueue_head(&ioc->reset_wq);
  4828. /* allocate memory pd handle bitmask list */
  4829. ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
  4830. if (ioc->facts.MaxDevHandle % 8)
  4831. ioc->pd_handles_sz++;
  4832. ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
  4833. GFP_KERNEL);
  4834. if (!ioc->pd_handles) {
  4835. r = -ENOMEM;
  4836. goto out_free_resources;
  4837. }
  4838. ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
  4839. GFP_KERNEL);
  4840. if (!ioc->blocking_handles) {
  4841. r = -ENOMEM;
  4842. goto out_free_resources;
  4843. }
  4844. /* allocate memory for pending OS device add list */
  4845. ioc->pend_os_device_add_sz = (ioc->facts.MaxDevHandle / 8);
  4846. if (ioc->facts.MaxDevHandle % 8)
  4847. ioc->pend_os_device_add_sz++;
  4848. ioc->pend_os_device_add = kzalloc(ioc->pend_os_device_add_sz,
  4849. GFP_KERNEL);
  4850. if (!ioc->pend_os_device_add)
  4851. goto out_free_resources;
  4852. ioc->device_remove_in_progress_sz = ioc->pend_os_device_add_sz;
  4853. ioc->device_remove_in_progress =
  4854. kzalloc(ioc->device_remove_in_progress_sz, GFP_KERNEL);
  4855. if (!ioc->device_remove_in_progress)
  4856. goto out_free_resources;
  4857. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  4858. /* base internal command bits */
  4859. mutex_init(&ioc->base_cmds.mutex);
  4860. ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4861. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  4862. /* port_enable command bits */
  4863. ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4864. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  4865. /* transport internal command bits */
  4866. ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4867. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  4868. mutex_init(&ioc->transport_cmds.mutex);
  4869. /* scsih internal command bits */
  4870. ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4871. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  4872. mutex_init(&ioc->scsih_cmds.mutex);
  4873. /* task management internal command bits */
  4874. ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4875. ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
  4876. mutex_init(&ioc->tm_cmds.mutex);
  4877. /* config page internal command bits */
  4878. ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4879. ioc->config_cmds.status = MPT3_CMD_NOT_USED;
  4880. mutex_init(&ioc->config_cmds.mutex);
  4881. /* ctl module internal command bits */
  4882. ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4883. ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  4884. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  4885. mutex_init(&ioc->ctl_cmds.mutex);
  4886. if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
  4887. !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
  4888. !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
  4889. !ioc->ctl_cmds.sense) {
  4890. r = -ENOMEM;
  4891. goto out_free_resources;
  4892. }
  4893. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  4894. ioc->event_masks[i] = -1;
  4895. /* here we enable the events we care about */
  4896. _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
  4897. _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
  4898. _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  4899. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  4900. _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
  4901. _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
  4902. _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
  4903. _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
  4904. _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
  4905. _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
  4906. _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
  4907. if (ioc->hba_mpi_version_belonged == MPI26_VERSION)
  4908. _base_unmask_events(ioc, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
  4909. r = _base_make_ioc_operational(ioc);
  4910. if (r)
  4911. goto out_free_resources;
  4912. ioc->non_operational_loop = 0;
  4913. return 0;
  4914. out_free_resources:
  4915. ioc->remove_host = 1;
  4916. mpt3sas_base_free_resources(ioc);
  4917. _base_release_memory_pools(ioc);
  4918. pci_set_drvdata(ioc->pdev, NULL);
  4919. kfree(ioc->cpu_msix_table);
  4920. if (ioc->is_warpdrive)
  4921. kfree(ioc->reply_post_host_index);
  4922. kfree(ioc->pd_handles);
  4923. kfree(ioc->blocking_handles);
  4924. kfree(ioc->device_remove_in_progress);
  4925. kfree(ioc->pend_os_device_add);
  4926. kfree(ioc->tm_cmds.reply);
  4927. kfree(ioc->transport_cmds.reply);
  4928. kfree(ioc->scsih_cmds.reply);
  4929. kfree(ioc->config_cmds.reply);
  4930. kfree(ioc->base_cmds.reply);
  4931. kfree(ioc->port_enable_cmds.reply);
  4932. kfree(ioc->ctl_cmds.reply);
  4933. kfree(ioc->ctl_cmds.sense);
  4934. kfree(ioc->pfacts);
  4935. ioc->ctl_cmds.reply = NULL;
  4936. ioc->base_cmds.reply = NULL;
  4937. ioc->tm_cmds.reply = NULL;
  4938. ioc->scsih_cmds.reply = NULL;
  4939. ioc->transport_cmds.reply = NULL;
  4940. ioc->config_cmds.reply = NULL;
  4941. ioc->pfacts = NULL;
  4942. return r;
  4943. }
  4944. /**
  4945. * mpt3sas_base_detach - remove controller instance
  4946. * @ioc: per adapter object
  4947. *
  4948. * Return nothing.
  4949. */
  4950. void
  4951. mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
  4952. {
  4953. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4954. __func__));
  4955. mpt3sas_base_stop_watchdog(ioc);
  4956. mpt3sas_base_free_resources(ioc);
  4957. _base_release_memory_pools(ioc);
  4958. pci_set_drvdata(ioc->pdev, NULL);
  4959. kfree(ioc->cpu_msix_table);
  4960. if (ioc->is_warpdrive)
  4961. kfree(ioc->reply_post_host_index);
  4962. kfree(ioc->pd_handles);
  4963. kfree(ioc->blocking_handles);
  4964. kfree(ioc->device_remove_in_progress);
  4965. kfree(ioc->pend_os_device_add);
  4966. kfree(ioc->pfacts);
  4967. kfree(ioc->ctl_cmds.reply);
  4968. kfree(ioc->ctl_cmds.sense);
  4969. kfree(ioc->base_cmds.reply);
  4970. kfree(ioc->port_enable_cmds.reply);
  4971. kfree(ioc->tm_cmds.reply);
  4972. kfree(ioc->transport_cmds.reply);
  4973. kfree(ioc->scsih_cmds.reply);
  4974. kfree(ioc->config_cmds.reply);
  4975. }
  4976. /**
  4977. * _base_reset_handler - reset callback handler (for base)
  4978. * @ioc: per adapter object
  4979. * @reset_phase: phase
  4980. *
  4981. * The handler for doing any required cleanup or initialization.
  4982. *
  4983. * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
  4984. * MPT3_IOC_DONE_RESET
  4985. *
  4986. * Return nothing.
  4987. */
  4988. static void
  4989. _base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
  4990. {
  4991. mpt3sas_scsih_reset_handler(ioc, reset_phase);
  4992. mpt3sas_ctl_reset_handler(ioc, reset_phase);
  4993. switch (reset_phase) {
  4994. case MPT3_IOC_PRE_RESET:
  4995. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  4996. "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
  4997. break;
  4998. case MPT3_IOC_AFTER_RESET:
  4999. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  5000. "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
  5001. if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
  5002. ioc->transport_cmds.status |= MPT3_CMD_RESET;
  5003. mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
  5004. complete(&ioc->transport_cmds.done);
  5005. }
  5006. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  5007. ioc->base_cmds.status |= MPT3_CMD_RESET;
  5008. mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
  5009. complete(&ioc->base_cmds.done);
  5010. }
  5011. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  5012. ioc->port_enable_failed = 1;
  5013. ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
  5014. mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
  5015. if (ioc->is_driver_loading) {
  5016. ioc->start_scan_failed =
  5017. MPI2_IOCSTATUS_INTERNAL_ERROR;
  5018. ioc->start_scan = 0;
  5019. ioc->port_enable_cmds.status =
  5020. MPT3_CMD_NOT_USED;
  5021. } else
  5022. complete(&ioc->port_enable_cmds.done);
  5023. }
  5024. if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
  5025. ioc->config_cmds.status |= MPT3_CMD_RESET;
  5026. mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
  5027. ioc->config_cmds.smid = USHRT_MAX;
  5028. complete(&ioc->config_cmds.done);
  5029. }
  5030. break;
  5031. case MPT3_IOC_DONE_RESET:
  5032. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  5033. "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
  5034. break;
  5035. }
  5036. }
  5037. /**
  5038. * _wait_for_commands_to_complete - reset controller
  5039. * @ioc: Pointer to MPT_ADAPTER structure
  5040. *
  5041. * This function waiting(3s) for all pending commands to complete
  5042. * prior to putting controller in reset.
  5043. */
  5044. static void
  5045. _wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc)
  5046. {
  5047. u32 ioc_state;
  5048. unsigned long flags;
  5049. u16 i;
  5050. ioc->pending_io_count = 0;
  5051. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  5052. if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
  5053. return;
  5054. /* pending command count */
  5055. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5056. for (i = 0; i < ioc->scsiio_depth; i++)
  5057. if (ioc->scsi_lookup[i].cb_idx != 0xFF)
  5058. ioc->pending_io_count++;
  5059. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  5060. if (!ioc->pending_io_count)
  5061. return;
  5062. /* wait for pending commands to complete */
  5063. wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
  5064. }
  5065. /**
  5066. * mpt3sas_base_hard_reset_handler - reset controller
  5067. * @ioc: Pointer to MPT_ADAPTER structure
  5068. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  5069. *
  5070. * Returns 0 for success, non-zero for failure.
  5071. */
  5072. int
  5073. mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
  5074. enum reset_type type)
  5075. {
  5076. int r;
  5077. unsigned long flags;
  5078. u32 ioc_state;
  5079. u8 is_fault = 0, is_trigger = 0;
  5080. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  5081. __func__));
  5082. if (ioc->pci_error_recovery) {
  5083. pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
  5084. ioc->name, __func__);
  5085. r = 0;
  5086. goto out_unlocked;
  5087. }
  5088. if (mpt3sas_fwfault_debug)
  5089. mpt3sas_halt_firmware(ioc);
  5090. /* wait for an active reset in progress to complete */
  5091. if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
  5092. do {
  5093. ssleep(1);
  5094. } while (ioc->shost_recovery == 1);
  5095. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
  5096. __func__));
  5097. return ioc->ioc_reset_in_progress_status;
  5098. }
  5099. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  5100. ioc->shost_recovery = 1;
  5101. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  5102. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  5103. MPT3_DIAG_BUFFER_IS_REGISTERED) &&
  5104. (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  5105. MPT3_DIAG_BUFFER_IS_RELEASED))) {
  5106. is_trigger = 1;
  5107. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  5108. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  5109. is_fault = 1;
  5110. }
  5111. _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
  5112. _wait_for_commands_to_complete(ioc);
  5113. _base_mask_interrupts(ioc);
  5114. r = _base_make_ioc_ready(ioc, type);
  5115. if (r)
  5116. goto out;
  5117. _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
  5118. /* If this hard reset is called while port enable is active, then
  5119. * there is no reason to call make_ioc_operational
  5120. */
  5121. if (ioc->is_driver_loading && ioc->port_enable_failed) {
  5122. ioc->remove_host = 1;
  5123. r = -EFAULT;
  5124. goto out;
  5125. }
  5126. r = _base_get_ioc_facts(ioc);
  5127. if (r)
  5128. goto out;
  5129. if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
  5130. panic("%s: Issue occurred with flashing controller firmware."
  5131. "Please reboot the system and ensure that the correct"
  5132. " firmware version is running\n", ioc->name);
  5133. r = _base_make_ioc_operational(ioc);
  5134. if (!r)
  5135. _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
  5136. out:
  5137. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
  5138. ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
  5139. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  5140. ioc->ioc_reset_in_progress_status = r;
  5141. ioc->shost_recovery = 0;
  5142. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  5143. ioc->ioc_reset_count++;
  5144. mutex_unlock(&ioc->reset_in_progress_mutex);
  5145. out_unlocked:
  5146. if ((r == 0) && is_trigger) {
  5147. if (is_fault)
  5148. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
  5149. else
  5150. mpt3sas_trigger_master(ioc,
  5151. MASTER_TRIGGER_ADAPTER_RESET);
  5152. }
  5153. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
  5154. __func__));
  5155. return r;
  5156. }