main.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/kernel.h>
  37. #include <linux/init.h>
  38. #include <linux/errno.h>
  39. #include <linux/pci.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/slab.h>
  42. #include <linux/io-mapping.h>
  43. #include <linux/delay.h>
  44. #include <linux/etherdevice.h>
  45. #include <net/devlink.h>
  46. #include <uapi/rdma/mlx4-abi.h>
  47. #include <linux/mlx4/device.h>
  48. #include <linux/mlx4/doorbell.h>
  49. #include "mlx4.h"
  50. #include "fw.h"
  51. #include "icm.h"
  52. MODULE_AUTHOR("Roland Dreier");
  53. MODULE_DESCRIPTION("Mellanox ConnectX HCA low-level driver");
  54. MODULE_LICENSE("Dual BSD/GPL");
  55. MODULE_VERSION(DRV_VERSION);
  56. struct workqueue_struct *mlx4_wq;
  57. #ifdef CONFIG_MLX4_DEBUG
  58. int mlx4_debug_level; /* 0 by default */
  59. module_param_named(debug_level, mlx4_debug_level, int, 0644);
  60. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  61. #endif /* CONFIG_MLX4_DEBUG */
  62. #ifdef CONFIG_PCI_MSI
  63. static int msi_x = 1;
  64. module_param(msi_x, int, 0444);
  65. MODULE_PARM_DESC(msi_x, "0 - don't use MSI-X, 1 - use MSI-X, >1 - limit number of MSI-X irqs to msi_x");
  66. #else /* CONFIG_PCI_MSI */
  67. #define msi_x (0)
  68. #endif /* CONFIG_PCI_MSI */
  69. static uint8_t num_vfs[3] = {0, 0, 0};
  70. static int num_vfs_argc;
  71. module_param_array(num_vfs, byte, &num_vfs_argc, 0444);
  72. MODULE_PARM_DESC(num_vfs, "enable #num_vfs functions if num_vfs > 0\n"
  73. "num_vfs=port1,port2,port1+2");
  74. static uint8_t probe_vf[3] = {0, 0, 0};
  75. static int probe_vfs_argc;
  76. module_param_array(probe_vf, byte, &probe_vfs_argc, 0444);
  77. MODULE_PARM_DESC(probe_vf, "number of vfs to probe by pf driver (num_vfs > 0)\n"
  78. "probe_vf=port1,port2,port1+2");
  79. static int mlx4_log_num_mgm_entry_size = MLX4_DEFAULT_MGM_LOG_ENTRY_SIZE;
  80. module_param_named(log_num_mgm_entry_size,
  81. mlx4_log_num_mgm_entry_size, int, 0444);
  82. MODULE_PARM_DESC(log_num_mgm_entry_size, "log mgm size, that defines the num"
  83. " of qp per mcg, for example:"
  84. " 10 gives 248.range: 7 <="
  85. " log_num_mgm_entry_size <= 12."
  86. " To activate device managed"
  87. " flow steering when available, set to -1");
  88. static bool enable_64b_cqe_eqe = true;
  89. module_param(enable_64b_cqe_eqe, bool, 0444);
  90. MODULE_PARM_DESC(enable_64b_cqe_eqe,
  91. "Enable 64 byte CQEs/EQEs when the FW supports this (default: True)");
  92. static bool enable_4k_uar;
  93. module_param(enable_4k_uar, bool, 0444);
  94. MODULE_PARM_DESC(enable_4k_uar,
  95. "Enable using 4K UAR. Should not be enabled if have VFs which do not support 4K UARs (default: false)");
  96. #define PF_CONTEXT_BEHAVIOUR_MASK (MLX4_FUNC_CAP_64B_EQE_CQE | \
  97. MLX4_FUNC_CAP_EQE_CQE_STRIDE | \
  98. MLX4_FUNC_CAP_DMFS_A0_STATIC)
  99. #define RESET_PERSIST_MASK_FLAGS (MLX4_FLAG_SRIOV)
  100. static char mlx4_version[] =
  101. DRV_NAME ": Mellanox ConnectX core driver v"
  102. DRV_VERSION "\n";
  103. static const struct mlx4_profile default_profile = {
  104. .num_qp = 1 << 18,
  105. .num_srq = 1 << 16,
  106. .rdmarc_per_qp = 1 << 4,
  107. .num_cq = 1 << 16,
  108. .num_mcg = 1 << 13,
  109. .num_mpt = 1 << 19,
  110. .num_mtt = 1 << 20, /* It is really num mtt segments */
  111. };
  112. static const struct mlx4_profile low_mem_profile = {
  113. .num_qp = 1 << 17,
  114. .num_srq = 1 << 6,
  115. .rdmarc_per_qp = 1 << 4,
  116. .num_cq = 1 << 8,
  117. .num_mcg = 1 << 8,
  118. .num_mpt = 1 << 9,
  119. .num_mtt = 1 << 7,
  120. };
  121. static int log_num_mac = 7;
  122. module_param_named(log_num_mac, log_num_mac, int, 0444);
  123. MODULE_PARM_DESC(log_num_mac, "Log2 max number of MACs per ETH port (1-7)");
  124. static int log_num_vlan;
  125. module_param_named(log_num_vlan, log_num_vlan, int, 0444);
  126. MODULE_PARM_DESC(log_num_vlan, "Log2 max number of VLANs per ETH port (0-7)");
  127. /* Log2 max number of VLANs per ETH port (0-7) */
  128. #define MLX4_LOG_NUM_VLANS 7
  129. #define MLX4_MIN_LOG_NUM_VLANS 0
  130. #define MLX4_MIN_LOG_NUM_MAC 1
  131. static bool use_prio;
  132. module_param_named(use_prio, use_prio, bool, 0444);
  133. MODULE_PARM_DESC(use_prio, "Enable steering by VLAN priority on ETH ports (deprecated)");
  134. int log_mtts_per_seg = ilog2(1);
  135. module_param_named(log_mtts_per_seg, log_mtts_per_seg, int, 0444);
  136. MODULE_PARM_DESC(log_mtts_per_seg, "Log2 number of MTT entries per segment "
  137. "(0-7) (default: 0)");
  138. static int port_type_array[2] = {MLX4_PORT_TYPE_NONE, MLX4_PORT_TYPE_NONE};
  139. static int arr_argc = 2;
  140. module_param_array(port_type_array, int, &arr_argc, 0444);
  141. MODULE_PARM_DESC(port_type_array, "Array of port types: HW_DEFAULT (0) is default "
  142. "1 for IB, 2 for Ethernet");
  143. static atomic_t pf_loading = ATOMIC_INIT(0);
  144. static int mlx4_devlink_ierr_reset_get(struct devlink *devlink, u32 id,
  145. struct devlink_param_gset_ctx *ctx)
  146. {
  147. ctx->val.vbool = !!mlx4_internal_err_reset;
  148. return 0;
  149. }
  150. static int mlx4_devlink_ierr_reset_set(struct devlink *devlink, u32 id,
  151. struct devlink_param_gset_ctx *ctx,
  152. struct netlink_ext_ack *extack)
  153. {
  154. mlx4_internal_err_reset = ctx->val.vbool;
  155. return 0;
  156. }
  157. static int mlx4_devlink_crdump_snapshot_get(struct devlink *devlink, u32 id,
  158. struct devlink_param_gset_ctx *ctx)
  159. {
  160. struct mlx4_priv *priv = devlink_priv(devlink);
  161. struct mlx4_dev *dev = &priv->dev;
  162. ctx->val.vbool = dev->persist->crdump.snapshot_enable;
  163. return 0;
  164. }
  165. static int mlx4_devlink_crdump_snapshot_set(struct devlink *devlink, u32 id,
  166. struct devlink_param_gset_ctx *ctx,
  167. struct netlink_ext_ack *extack)
  168. {
  169. struct mlx4_priv *priv = devlink_priv(devlink);
  170. struct mlx4_dev *dev = &priv->dev;
  171. dev->persist->crdump.snapshot_enable = ctx->val.vbool;
  172. return 0;
  173. }
  174. static int
  175. mlx4_devlink_max_macs_validate(struct devlink *devlink, u32 id,
  176. union devlink_param_value val,
  177. struct netlink_ext_ack *extack)
  178. {
  179. u32 value = val.vu32;
  180. if (value < 1 || value > 128)
  181. return -ERANGE;
  182. if (!is_power_of_2(value)) {
  183. NL_SET_ERR_MSG_MOD(extack, "max_macs supported must be power of 2");
  184. return -EINVAL;
  185. }
  186. return 0;
  187. }
  188. enum mlx4_devlink_param_id {
  189. MLX4_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
  190. MLX4_DEVLINK_PARAM_ID_ENABLE_64B_CQE_EQE,
  191. MLX4_DEVLINK_PARAM_ID_ENABLE_4K_UAR,
  192. };
  193. static const struct devlink_param mlx4_devlink_params[] = {
  194. DEVLINK_PARAM_GENERIC(INT_ERR_RESET,
  195. BIT(DEVLINK_PARAM_CMODE_RUNTIME) |
  196. BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
  197. mlx4_devlink_ierr_reset_get,
  198. mlx4_devlink_ierr_reset_set, NULL),
  199. DEVLINK_PARAM_GENERIC(MAX_MACS,
  200. BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
  201. NULL, NULL, mlx4_devlink_max_macs_validate),
  202. DEVLINK_PARAM_GENERIC(REGION_SNAPSHOT,
  203. BIT(DEVLINK_PARAM_CMODE_RUNTIME) |
  204. BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
  205. mlx4_devlink_crdump_snapshot_get,
  206. mlx4_devlink_crdump_snapshot_set, NULL),
  207. DEVLINK_PARAM_DRIVER(MLX4_DEVLINK_PARAM_ID_ENABLE_64B_CQE_EQE,
  208. "enable_64b_cqe_eqe", DEVLINK_PARAM_TYPE_BOOL,
  209. BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
  210. NULL, NULL, NULL),
  211. DEVLINK_PARAM_DRIVER(MLX4_DEVLINK_PARAM_ID_ENABLE_4K_UAR,
  212. "enable_4k_uar", DEVLINK_PARAM_TYPE_BOOL,
  213. BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
  214. NULL, NULL, NULL),
  215. };
  216. static void mlx4_devlink_set_params_init_values(struct devlink *devlink)
  217. {
  218. union devlink_param_value value;
  219. value.vbool = !!mlx4_internal_err_reset;
  220. devl_param_driverinit_value_set(devlink,
  221. DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
  222. value);
  223. value.vu32 = 1UL << log_num_mac;
  224. devl_param_driverinit_value_set(devlink,
  225. DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
  226. value);
  227. value.vbool = enable_64b_cqe_eqe;
  228. devl_param_driverinit_value_set(devlink,
  229. MLX4_DEVLINK_PARAM_ID_ENABLE_64B_CQE_EQE,
  230. value);
  231. value.vbool = enable_4k_uar;
  232. devl_param_driverinit_value_set(devlink,
  233. MLX4_DEVLINK_PARAM_ID_ENABLE_4K_UAR,
  234. value);
  235. value.vbool = false;
  236. devl_param_driverinit_value_set(devlink,
  237. DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
  238. value);
  239. }
  240. static inline void mlx4_set_num_reserved_uars(struct mlx4_dev *dev,
  241. struct mlx4_dev_cap *dev_cap)
  242. {
  243. /* The reserved_uars is calculated by system page size unit.
  244. * Therefore, adjustment is added when the uar page size is less
  245. * than the system page size
  246. */
  247. dev->caps.reserved_uars =
  248. max_t(int,
  249. mlx4_get_num_reserved_uar(dev),
  250. dev_cap->reserved_uars /
  251. (1 << (PAGE_SHIFT - dev->uar_page_shift)));
  252. }
  253. int mlx4_check_port_params(struct mlx4_dev *dev,
  254. enum mlx4_port_type *port_type)
  255. {
  256. int i;
  257. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
  258. for (i = 0; i < dev->caps.num_ports - 1; i++) {
  259. if (port_type[i] != port_type[i + 1]) {
  260. mlx4_err(dev, "Only same port types supported on this HCA, aborting\n");
  261. return -EOPNOTSUPP;
  262. }
  263. }
  264. }
  265. for (i = 0; i < dev->caps.num_ports; i++) {
  266. if (!(port_type[i] & dev->caps.supported_type[i+1])) {
  267. mlx4_err(dev, "Requested port type for port %d is not supported on this HCA\n",
  268. i + 1);
  269. return -EOPNOTSUPP;
  270. }
  271. }
  272. return 0;
  273. }
  274. static void mlx4_set_port_mask(struct mlx4_dev *dev)
  275. {
  276. int i;
  277. for (i = 1; i <= dev->caps.num_ports; ++i)
  278. dev->caps.port_mask[i] = dev->caps.port_type[i];
  279. }
  280. enum {
  281. MLX4_QUERY_FUNC_NUM_SYS_EQS = 1 << 0,
  282. };
  283. static int mlx4_query_func(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
  284. {
  285. int err = 0;
  286. struct mlx4_func func;
  287. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS) {
  288. err = mlx4_QUERY_FUNC(dev, &func, 0);
  289. if (err) {
  290. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  291. return err;
  292. }
  293. dev_cap->max_eqs = func.max_eq;
  294. dev_cap->reserved_eqs = func.rsvd_eqs;
  295. dev_cap->reserved_uars = func.rsvd_uars;
  296. err |= MLX4_QUERY_FUNC_NUM_SYS_EQS;
  297. }
  298. return err;
  299. }
  300. static void mlx4_enable_cqe_eqe_stride(struct mlx4_dev *dev)
  301. {
  302. struct mlx4_caps *dev_cap = &dev->caps;
  303. /* FW not supporting or cancelled by user */
  304. if (!(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_EQE_STRIDE) ||
  305. !(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_CQE_STRIDE))
  306. return;
  307. /* Must have 64B CQE_EQE enabled by FW to use bigger stride
  308. * When FW has NCSI it may decide not to report 64B CQE/EQEs
  309. */
  310. if (!(dev_cap->flags & MLX4_DEV_CAP_FLAG_64B_EQE) ||
  311. !(dev_cap->flags & MLX4_DEV_CAP_FLAG_64B_CQE)) {
  312. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_CQE_STRIDE;
  313. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_EQE_STRIDE;
  314. return;
  315. }
  316. if (cache_line_size() == 128 || cache_line_size() == 256) {
  317. mlx4_dbg(dev, "Enabling CQE stride cacheLine supported\n");
  318. /* Changing the real data inside CQE size to 32B */
  319. dev_cap->flags &= ~MLX4_DEV_CAP_FLAG_64B_CQE;
  320. dev_cap->flags &= ~MLX4_DEV_CAP_FLAG_64B_EQE;
  321. if (mlx4_is_master(dev))
  322. dev_cap->function_caps |= MLX4_FUNC_CAP_EQE_CQE_STRIDE;
  323. } else {
  324. if (cache_line_size() != 32 && cache_line_size() != 64)
  325. mlx4_dbg(dev, "Disabling CQE stride, cacheLine size unsupported\n");
  326. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_CQE_STRIDE;
  327. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_EQE_STRIDE;
  328. }
  329. }
  330. static int _mlx4_dev_port(struct mlx4_dev *dev, int port,
  331. struct mlx4_port_cap *port_cap)
  332. {
  333. dev->caps.vl_cap[port] = port_cap->max_vl;
  334. dev->caps.ib_mtu_cap[port] = port_cap->ib_mtu;
  335. dev->phys_caps.gid_phys_table_len[port] = port_cap->max_gids;
  336. dev->phys_caps.pkey_phys_table_len[port] = port_cap->max_pkeys;
  337. /* set gid and pkey table operating lengths by default
  338. * to non-sriov values
  339. */
  340. dev->caps.gid_table_len[port] = port_cap->max_gids;
  341. dev->caps.pkey_table_len[port] = port_cap->max_pkeys;
  342. dev->caps.port_width_cap[port] = port_cap->max_port_width;
  343. dev->caps.eth_mtu_cap[port] = port_cap->eth_mtu;
  344. dev->caps.max_tc_eth = port_cap->max_tc_eth;
  345. dev->caps.def_mac[port] = port_cap->def_mac;
  346. dev->caps.supported_type[port] = port_cap->supported_port_types;
  347. dev->caps.suggested_type[port] = port_cap->suggested_type;
  348. dev->caps.default_sense[port] = port_cap->default_sense;
  349. dev->caps.trans_type[port] = port_cap->trans_type;
  350. dev->caps.vendor_oui[port] = port_cap->vendor_oui;
  351. dev->caps.wavelength[port] = port_cap->wavelength;
  352. dev->caps.trans_code[port] = port_cap->trans_code;
  353. return 0;
  354. }
  355. static int mlx4_dev_port(struct mlx4_dev *dev, int port,
  356. struct mlx4_port_cap *port_cap)
  357. {
  358. int err = 0;
  359. err = mlx4_QUERY_PORT(dev, port, port_cap);
  360. if (err)
  361. mlx4_err(dev, "QUERY_PORT command failed.\n");
  362. return err;
  363. }
  364. static inline void mlx4_enable_ignore_fcs(struct mlx4_dev *dev)
  365. {
  366. if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_IGNORE_FCS))
  367. return;
  368. if (mlx4_is_mfunc(dev)) {
  369. mlx4_dbg(dev, "SRIOV mode - Disabling Ignore FCS");
  370. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_IGNORE_FCS;
  371. return;
  372. }
  373. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)) {
  374. mlx4_dbg(dev,
  375. "Keep FCS is not supported - Disabling Ignore FCS");
  376. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_IGNORE_FCS;
  377. return;
  378. }
  379. }
  380. #define MLX4_A0_STEERING_TABLE_SIZE 256
  381. static int mlx4_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
  382. {
  383. int err;
  384. int i;
  385. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  386. if (err) {
  387. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting\n");
  388. return err;
  389. }
  390. mlx4_dev_cap_dump(dev, dev_cap);
  391. if (dev_cap->min_page_sz > PAGE_SIZE) {
  392. mlx4_err(dev, "HCA minimum page size of %d bigger than kernel PAGE_SIZE of %ld, aborting\n",
  393. dev_cap->min_page_sz, PAGE_SIZE);
  394. return -ENODEV;
  395. }
  396. if (dev_cap->num_ports > MLX4_MAX_PORTS) {
  397. mlx4_err(dev, "HCA has %d ports, but we only support %d, aborting\n",
  398. dev_cap->num_ports, MLX4_MAX_PORTS);
  399. return -ENODEV;
  400. }
  401. if (dev_cap->uar_size > pci_resource_len(dev->persist->pdev, 2)) {
  402. mlx4_err(dev, "HCA reported UAR size of 0x%x bigger than PCI resource 2 size of 0x%llx, aborting\n",
  403. dev_cap->uar_size,
  404. (unsigned long long)
  405. pci_resource_len(dev->persist->pdev, 2));
  406. return -ENODEV;
  407. }
  408. dev->caps.num_ports = dev_cap->num_ports;
  409. dev->caps.num_sys_eqs = dev_cap->num_sys_eqs;
  410. dev->phys_caps.num_phys_eqs = dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS ?
  411. dev->caps.num_sys_eqs :
  412. MLX4_MAX_EQ_NUM;
  413. for (i = 1; i <= dev->caps.num_ports; ++i) {
  414. err = _mlx4_dev_port(dev, i, dev_cap->port_cap + i);
  415. if (err) {
  416. mlx4_err(dev, "QUERY_PORT command failed, aborting\n");
  417. return err;
  418. }
  419. }
  420. dev->caps.map_clock_to_user = dev_cap->map_clock_to_user;
  421. dev->caps.uar_page_size = PAGE_SIZE;
  422. dev->caps.num_uars = dev_cap->uar_size / PAGE_SIZE;
  423. dev->caps.local_ca_ack_delay = dev_cap->local_ca_ack_delay;
  424. dev->caps.bf_reg_size = dev_cap->bf_reg_size;
  425. dev->caps.bf_regs_per_page = dev_cap->bf_regs_per_page;
  426. dev->caps.max_sq_sg = dev_cap->max_sq_sg;
  427. dev->caps.max_rq_sg = dev_cap->max_rq_sg;
  428. dev->caps.max_wqes = dev_cap->max_qp_sz;
  429. dev->caps.max_qp_init_rdma = dev_cap->max_requester_per_qp;
  430. dev->caps.max_srq_wqes = dev_cap->max_srq_sz;
  431. dev->caps.max_srq_sge = dev_cap->max_rq_sg - 1;
  432. dev->caps.reserved_srqs = dev_cap->reserved_srqs;
  433. dev->caps.max_sq_desc_sz = dev_cap->max_sq_desc_sz;
  434. dev->caps.max_rq_desc_sz = dev_cap->max_rq_desc_sz;
  435. /*
  436. * Subtract 1 from the limit because we need to allocate a
  437. * spare CQE to enable resizing the CQ.
  438. */
  439. dev->caps.max_cqes = dev_cap->max_cq_sz - 1;
  440. dev->caps.reserved_cqs = dev_cap->reserved_cqs;
  441. dev->caps.reserved_eqs = dev_cap->reserved_eqs;
  442. dev->caps.reserved_mtts = dev_cap->reserved_mtts;
  443. dev->caps.reserved_mrws = dev_cap->reserved_mrws;
  444. dev->caps.reserved_pds = dev_cap->reserved_pds;
  445. dev->caps.reserved_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
  446. dev_cap->reserved_xrcds : 0;
  447. dev->caps.max_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
  448. dev_cap->max_xrcds : 0;
  449. dev->caps.mtt_entry_sz = dev_cap->mtt_entry_sz;
  450. dev->caps.max_msg_sz = dev_cap->max_msg_sz;
  451. dev->caps.page_size_cap = ~(u32) (dev_cap->min_page_sz - 1);
  452. dev->caps.flags = dev_cap->flags;
  453. dev->caps.flags2 = dev_cap->flags2;
  454. dev->caps.bmme_flags = dev_cap->bmme_flags;
  455. dev->caps.reserved_lkey = dev_cap->reserved_lkey;
  456. dev->caps.stat_rate_support = dev_cap->stat_rate_support;
  457. dev->caps.max_gso_sz = dev_cap->max_gso_sz;
  458. dev->caps.max_rss_tbl_sz = dev_cap->max_rss_tbl_sz;
  459. dev->caps.wol_port[1] = dev_cap->wol_port[1];
  460. dev->caps.wol_port[2] = dev_cap->wol_port[2];
  461. dev->caps.health_buffer_addrs = dev_cap->health_buffer_addrs;
  462. /* Save uar page shift */
  463. if (!mlx4_is_slave(dev)) {
  464. /* Virtual PCI function needs to determine UAR page size from
  465. * firmware. Only master PCI function can set the uar page size
  466. */
  467. if (enable_4k_uar || !dev->persist->num_vfs)
  468. dev->uar_page_shift = DEFAULT_UAR_PAGE_SHIFT;
  469. else
  470. dev->uar_page_shift = PAGE_SHIFT;
  471. mlx4_set_num_reserved_uars(dev, dev_cap);
  472. }
  473. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PHV_EN) {
  474. struct mlx4_init_hca_param hca_param;
  475. memset(&hca_param, 0, sizeof(hca_param));
  476. err = mlx4_QUERY_HCA(dev, &hca_param);
  477. /* Turn off PHV_EN flag in case phv_check_en is set.
  478. * phv_check_en is a HW check that parse the packet and verify
  479. * phv bit was reported correctly in the wqe. To allow QinQ
  480. * PHV_EN flag should be set and phv_check_en must be cleared
  481. * otherwise QinQ packets will be drop by the HW.
  482. */
  483. if (err || hca_param.phv_check_en)
  484. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_PHV_EN;
  485. }
  486. /* Sense port always allowed on supported devices for ConnectX-1 and -2 */
  487. if (mlx4_priv(dev)->pci_dev_data & MLX4_PCI_DEV_FORCE_SENSE_PORT)
  488. dev->caps.flags |= MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
  489. /* Don't do sense port on multifunction devices (for now at least) */
  490. if (mlx4_is_mfunc(dev))
  491. dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
  492. if (mlx4_low_memory_profile()) {
  493. dev->caps.log_num_macs = MLX4_MIN_LOG_NUM_MAC;
  494. dev->caps.log_num_vlans = MLX4_MIN_LOG_NUM_VLANS;
  495. } else {
  496. dev->caps.log_num_macs = log_num_mac;
  497. dev->caps.log_num_vlans = MLX4_LOG_NUM_VLANS;
  498. }
  499. for (i = 1; i <= dev->caps.num_ports; ++i) {
  500. dev->caps.port_type[i] = MLX4_PORT_TYPE_NONE;
  501. if (dev->caps.supported_type[i]) {
  502. /* if only ETH is supported - assign ETH */
  503. if (dev->caps.supported_type[i] == MLX4_PORT_TYPE_ETH)
  504. dev->caps.port_type[i] = MLX4_PORT_TYPE_ETH;
  505. /* if only IB is supported, assign IB */
  506. else if (dev->caps.supported_type[i] ==
  507. MLX4_PORT_TYPE_IB)
  508. dev->caps.port_type[i] = MLX4_PORT_TYPE_IB;
  509. else {
  510. /* if IB and ETH are supported, we set the port
  511. * type according to user selection of port type;
  512. * if user selected none, take the FW hint */
  513. if (port_type_array[i - 1] == MLX4_PORT_TYPE_NONE)
  514. dev->caps.port_type[i] = dev->caps.suggested_type[i] ?
  515. MLX4_PORT_TYPE_ETH : MLX4_PORT_TYPE_IB;
  516. else
  517. dev->caps.port_type[i] = port_type_array[i - 1];
  518. }
  519. }
  520. /*
  521. * Link sensing is allowed on the port if 3 conditions are true:
  522. * 1. Both protocols are supported on the port.
  523. * 2. Different types are supported on the port
  524. * 3. FW declared that it supports link sensing
  525. */
  526. mlx4_priv(dev)->sense.sense_allowed[i] =
  527. ((dev->caps.supported_type[i] == MLX4_PORT_TYPE_AUTO) &&
  528. (dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
  529. (dev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT));
  530. /*
  531. * If "default_sense" bit is set, we move the port to "AUTO" mode
  532. * and perform sense_port FW command to try and set the correct
  533. * port type from beginning
  534. */
  535. if (mlx4_priv(dev)->sense.sense_allowed[i] && dev->caps.default_sense[i]) {
  536. enum mlx4_port_type sensed_port = MLX4_PORT_TYPE_NONE;
  537. dev->caps.possible_type[i] = MLX4_PORT_TYPE_AUTO;
  538. mlx4_SENSE_PORT(dev, i, &sensed_port);
  539. if (sensed_port != MLX4_PORT_TYPE_NONE)
  540. dev->caps.port_type[i] = sensed_port;
  541. } else {
  542. dev->caps.possible_type[i] = dev->caps.port_type[i];
  543. }
  544. if (dev->caps.log_num_macs > dev_cap->port_cap[i].log_max_macs) {
  545. dev->caps.log_num_macs = dev_cap->port_cap[i].log_max_macs;
  546. mlx4_warn(dev, "Requested number of MACs is too much for port %d, reducing to %d\n",
  547. i, 1 << dev->caps.log_num_macs);
  548. }
  549. if (dev->caps.log_num_vlans > dev_cap->port_cap[i].log_max_vlans) {
  550. dev->caps.log_num_vlans = dev_cap->port_cap[i].log_max_vlans;
  551. mlx4_warn(dev, "Requested number of VLANs is too much for port %d, reducing to %d\n",
  552. i, 1 << dev->caps.log_num_vlans);
  553. }
  554. }
  555. if (mlx4_is_master(dev) && (dev->caps.num_ports == 2) &&
  556. (port_type_array[0] == MLX4_PORT_TYPE_IB) &&
  557. (port_type_array[1] == MLX4_PORT_TYPE_ETH)) {
  558. mlx4_warn(dev,
  559. "Granular QoS per VF not supported with IB/Eth configuration\n");
  560. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_QOS_VPP;
  561. }
  562. dev->caps.max_counters = dev_cap->max_counters;
  563. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] = dev_cap->reserved_qps;
  564. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] =
  565. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] =
  566. (1 << dev->caps.log_num_macs) *
  567. (1 << dev->caps.log_num_vlans) *
  568. dev->caps.num_ports;
  569. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH] = MLX4_NUM_FEXCH;
  570. if (dev_cap->dmfs_high_rate_qpn_base > 0 &&
  571. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN)
  572. dev->caps.dmfs_high_rate_qpn_base = dev_cap->dmfs_high_rate_qpn_base;
  573. else
  574. dev->caps.dmfs_high_rate_qpn_base =
  575. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
  576. if (dev_cap->dmfs_high_rate_qpn_range > 0 &&
  577. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN) {
  578. dev->caps.dmfs_high_rate_qpn_range = dev_cap->dmfs_high_rate_qpn_range;
  579. dev->caps.dmfs_high_steer_mode = MLX4_STEERING_DMFS_A0_DEFAULT;
  580. dev->caps.flags2 |= MLX4_DEV_CAP_FLAG2_FS_A0;
  581. } else {
  582. dev->caps.dmfs_high_steer_mode = MLX4_STEERING_DMFS_A0_NOT_SUPPORTED;
  583. dev->caps.dmfs_high_rate_qpn_base =
  584. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
  585. dev->caps.dmfs_high_rate_qpn_range = MLX4_A0_STEERING_TABLE_SIZE;
  586. }
  587. dev->caps.rl_caps = dev_cap->rl_caps;
  588. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_RSS_RAW_ETH] =
  589. dev->caps.dmfs_high_rate_qpn_range;
  590. dev->caps.reserved_qps = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] +
  591. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] +
  592. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] +
  593. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH];
  594. dev->caps.sqp_demux = (mlx4_is_master(dev)) ? MLX4_MAX_NUM_SLAVES : 0;
  595. if (!enable_64b_cqe_eqe && !mlx4_is_slave(dev)) {
  596. if (dev_cap->flags &
  597. (MLX4_DEV_CAP_FLAG_64B_CQE | MLX4_DEV_CAP_FLAG_64B_EQE)) {
  598. mlx4_warn(dev, "64B EQEs/CQEs supported by the device but not enabled\n");
  599. dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_64B_CQE;
  600. dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_64B_EQE;
  601. }
  602. if (dev_cap->flags2 &
  603. (MLX4_DEV_CAP_FLAG2_CQE_STRIDE |
  604. MLX4_DEV_CAP_FLAG2_EQE_STRIDE)) {
  605. mlx4_warn(dev, "Disabling EQE/CQE stride per user request\n");
  606. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_CQE_STRIDE;
  607. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_EQE_STRIDE;
  608. }
  609. }
  610. if ((dev->caps.flags &
  611. (MLX4_DEV_CAP_FLAG_64B_CQE | MLX4_DEV_CAP_FLAG_64B_EQE)) &&
  612. mlx4_is_master(dev))
  613. dev->caps.function_caps |= MLX4_FUNC_CAP_64B_EQE_CQE;
  614. if (!mlx4_is_slave(dev)) {
  615. mlx4_enable_cqe_eqe_stride(dev);
  616. dev->caps.alloc_res_qp_mask =
  617. (dev->caps.bf_reg_size ? MLX4_RESERVE_ETH_BF_QP : 0) |
  618. MLX4_RESERVE_A0_QP;
  619. if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) &&
  620. dev->caps.flags & MLX4_DEV_CAP_FLAG_SET_ETH_SCHED) {
  621. mlx4_warn(dev, "Old device ETS support detected\n");
  622. mlx4_warn(dev, "Consider upgrading device FW.\n");
  623. dev->caps.flags2 |= MLX4_DEV_CAP_FLAG2_ETS_CFG;
  624. }
  625. } else {
  626. dev->caps.alloc_res_qp_mask = 0;
  627. }
  628. mlx4_enable_ignore_fcs(dev);
  629. return 0;
  630. }
  631. /*The function checks if there are live vf, return the num of them*/
  632. static int mlx4_how_many_lives_vf(struct mlx4_dev *dev)
  633. {
  634. struct mlx4_priv *priv = mlx4_priv(dev);
  635. struct mlx4_slave_state *s_state;
  636. int i;
  637. int ret = 0;
  638. for (i = 1/*the ppf is 0*/; i < dev->num_slaves; ++i) {
  639. s_state = &priv->mfunc.master.slave_state[i];
  640. if (s_state->active && s_state->last_cmd !=
  641. MLX4_COMM_CMD_RESET) {
  642. mlx4_warn(dev, "%s: slave: %d is still active\n",
  643. __func__, i);
  644. ret++;
  645. }
  646. }
  647. return ret;
  648. }
  649. int mlx4_get_parav_qkey(struct mlx4_dev *dev, u32 qpn, u32 *qkey)
  650. {
  651. u32 qk = MLX4_RESERVED_QKEY_BASE;
  652. if (qpn >= dev->phys_caps.base_tunnel_sqpn + 8 * MLX4_MFUNC_MAX ||
  653. qpn < dev->phys_caps.base_proxy_sqpn)
  654. return -EINVAL;
  655. if (qpn >= dev->phys_caps.base_tunnel_sqpn)
  656. /* tunnel qp */
  657. qk += qpn - dev->phys_caps.base_tunnel_sqpn;
  658. else
  659. qk += qpn - dev->phys_caps.base_proxy_sqpn;
  660. *qkey = qk;
  661. return 0;
  662. }
  663. EXPORT_SYMBOL(mlx4_get_parav_qkey);
  664. void mlx4_sync_pkey_table(struct mlx4_dev *dev, int slave, int port, int i, int val)
  665. {
  666. struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
  667. if (!mlx4_is_master(dev))
  668. return;
  669. priv->virt2phys_pkey[slave][port - 1][i] = val;
  670. }
  671. EXPORT_SYMBOL(mlx4_sync_pkey_table);
  672. void mlx4_put_slave_node_guid(struct mlx4_dev *dev, int slave, __be64 guid)
  673. {
  674. struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
  675. if (!mlx4_is_master(dev))
  676. return;
  677. priv->slave_node_guids[slave] = guid;
  678. }
  679. EXPORT_SYMBOL(mlx4_put_slave_node_guid);
  680. __be64 mlx4_get_slave_node_guid(struct mlx4_dev *dev, int slave)
  681. {
  682. struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
  683. if (!mlx4_is_master(dev))
  684. return 0;
  685. return priv->slave_node_guids[slave];
  686. }
  687. EXPORT_SYMBOL(mlx4_get_slave_node_guid);
  688. int mlx4_is_slave_active(struct mlx4_dev *dev, int slave)
  689. {
  690. struct mlx4_priv *priv = mlx4_priv(dev);
  691. struct mlx4_slave_state *s_slave;
  692. if (!mlx4_is_master(dev))
  693. return 0;
  694. s_slave = &priv->mfunc.master.slave_state[slave];
  695. return !!s_slave->active;
  696. }
  697. EXPORT_SYMBOL(mlx4_is_slave_active);
  698. void mlx4_handle_eth_header_mcast_prio(struct mlx4_net_trans_rule_hw_ctrl *ctrl,
  699. struct _rule_hw *eth_header)
  700. {
  701. if (is_multicast_ether_addr(eth_header->eth.dst_mac) ||
  702. is_broadcast_ether_addr(eth_header->eth.dst_mac)) {
  703. struct mlx4_net_trans_rule_hw_eth *eth =
  704. (struct mlx4_net_trans_rule_hw_eth *)eth_header;
  705. struct _rule_hw *next_rule = (struct _rule_hw *)(eth + 1);
  706. bool last_rule = next_rule->size == 0 && next_rule->id == 0 &&
  707. next_rule->rsvd == 0;
  708. if (last_rule)
  709. ctrl->prio = cpu_to_be16(MLX4_DOMAIN_NIC);
  710. }
  711. }
  712. EXPORT_SYMBOL(mlx4_handle_eth_header_mcast_prio);
  713. static void slave_adjust_steering_mode(struct mlx4_dev *dev,
  714. struct mlx4_dev_cap *dev_cap,
  715. struct mlx4_init_hca_param *hca_param)
  716. {
  717. dev->caps.steering_mode = hca_param->steering_mode;
  718. if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED) {
  719. dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
  720. dev->caps.fs_log_max_ucast_qp_range_size =
  721. dev_cap->fs_log_max_ucast_qp_range_size;
  722. } else
  723. dev->caps.num_qp_per_mgm =
  724. 4 * ((1 << hca_param->log_mc_entry_sz)/16 - 2);
  725. mlx4_dbg(dev, "Steering mode is: %s\n",
  726. mlx4_steering_mode_str(dev->caps.steering_mode));
  727. }
  728. static void mlx4_slave_destroy_special_qp_cap(struct mlx4_dev *dev)
  729. {
  730. kfree(dev->caps.spec_qps);
  731. dev->caps.spec_qps = NULL;
  732. }
  733. static int mlx4_slave_special_qp_cap(struct mlx4_dev *dev)
  734. {
  735. struct mlx4_func_cap *func_cap;
  736. struct mlx4_caps *caps = &dev->caps;
  737. int i, err = 0;
  738. func_cap = kzalloc(sizeof(*func_cap), GFP_KERNEL);
  739. caps->spec_qps = kcalloc(caps->num_ports, sizeof(*caps->spec_qps), GFP_KERNEL);
  740. if (!func_cap || !caps->spec_qps) {
  741. mlx4_err(dev, "Failed to allocate memory for special qps cap\n");
  742. err = -ENOMEM;
  743. goto err_mem;
  744. }
  745. for (i = 1; i <= caps->num_ports; ++i) {
  746. err = mlx4_QUERY_FUNC_CAP(dev, i, func_cap);
  747. if (err) {
  748. mlx4_err(dev, "QUERY_FUNC_CAP port command failed for port %d, aborting (%d)\n",
  749. i, err);
  750. goto err_mem;
  751. }
  752. caps->spec_qps[i - 1] = func_cap->spec_qps;
  753. caps->port_mask[i] = caps->port_type[i];
  754. caps->phys_port_id[i] = func_cap->phys_port_id;
  755. err = mlx4_get_slave_pkey_gid_tbl_len(dev, i,
  756. &caps->gid_table_len[i],
  757. &caps->pkey_table_len[i]);
  758. if (err) {
  759. mlx4_err(dev, "QUERY_PORT command failed for port %d, aborting (%d)\n",
  760. i, err);
  761. goto err_mem;
  762. }
  763. }
  764. err_mem:
  765. if (err)
  766. mlx4_slave_destroy_special_qp_cap(dev);
  767. kfree(func_cap);
  768. return err;
  769. }
  770. static int mlx4_slave_cap(struct mlx4_dev *dev)
  771. {
  772. int err;
  773. u32 page_size;
  774. struct mlx4_dev_cap *dev_cap;
  775. struct mlx4_func_cap *func_cap;
  776. struct mlx4_init_hca_param *hca_param;
  777. hca_param = kzalloc(sizeof(*hca_param), GFP_KERNEL);
  778. func_cap = kzalloc(sizeof(*func_cap), GFP_KERNEL);
  779. dev_cap = kzalloc(sizeof(*dev_cap), GFP_KERNEL);
  780. if (!hca_param || !func_cap || !dev_cap) {
  781. mlx4_err(dev, "Failed to allocate memory for slave_cap\n");
  782. err = -ENOMEM;
  783. goto free_mem;
  784. }
  785. err = mlx4_QUERY_HCA(dev, hca_param);
  786. if (err) {
  787. mlx4_err(dev, "QUERY_HCA command failed, aborting\n");
  788. goto free_mem;
  789. }
  790. /* fail if the hca has an unknown global capability
  791. * at this time global_caps should be always zeroed
  792. */
  793. if (hca_param->global_caps) {
  794. mlx4_err(dev, "Unknown hca global capabilities\n");
  795. err = -EINVAL;
  796. goto free_mem;
  797. }
  798. dev->caps.hca_core_clock = hca_param->hca_core_clock;
  799. dev->caps.max_qp_dest_rdma = 1 << hca_param->log_rd_per_qp;
  800. err = mlx4_dev_cap(dev, dev_cap);
  801. if (err) {
  802. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting\n");
  803. goto free_mem;
  804. }
  805. err = mlx4_QUERY_FW(dev);
  806. if (err)
  807. mlx4_err(dev, "QUERY_FW command failed: could not get FW version\n");
  808. page_size = ~dev->caps.page_size_cap + 1;
  809. mlx4_warn(dev, "HCA minimum page size:%d\n", page_size);
  810. if (page_size > PAGE_SIZE) {
  811. mlx4_err(dev, "HCA minimum page size of %d bigger than kernel PAGE_SIZE of %ld, aborting\n",
  812. page_size, PAGE_SIZE);
  813. err = -ENODEV;
  814. goto free_mem;
  815. }
  816. /* Set uar_page_shift for VF */
  817. dev->uar_page_shift = hca_param->uar_page_sz + 12;
  818. /* Make sure the master uar page size is valid */
  819. if (dev->uar_page_shift > PAGE_SHIFT) {
  820. mlx4_err(dev,
  821. "Invalid configuration: uar page size is larger than system page size\n");
  822. err = -ENODEV;
  823. goto free_mem;
  824. }
  825. /* Set reserved_uars based on the uar_page_shift */
  826. mlx4_set_num_reserved_uars(dev, dev_cap);
  827. /* Although uar page size in FW differs from system page size,
  828. * upper software layers (mlx4_ib, mlx4_en and part of mlx4_core)
  829. * still works with assumption that uar page size == system page size
  830. */
  831. dev->caps.uar_page_size = PAGE_SIZE;
  832. err = mlx4_QUERY_FUNC_CAP(dev, 0, func_cap);
  833. if (err) {
  834. mlx4_err(dev, "QUERY_FUNC_CAP general command failed, aborting (%d)\n",
  835. err);
  836. goto free_mem;
  837. }
  838. if ((func_cap->pf_context_behaviour | PF_CONTEXT_BEHAVIOUR_MASK) !=
  839. PF_CONTEXT_BEHAVIOUR_MASK) {
  840. mlx4_err(dev, "Unknown pf context behaviour %x known flags %x\n",
  841. func_cap->pf_context_behaviour,
  842. PF_CONTEXT_BEHAVIOUR_MASK);
  843. err = -EINVAL;
  844. goto free_mem;
  845. }
  846. dev->caps.num_ports = func_cap->num_ports;
  847. dev->quotas.qp = func_cap->qp_quota;
  848. dev->quotas.srq = func_cap->srq_quota;
  849. dev->quotas.cq = func_cap->cq_quota;
  850. dev->quotas.mpt = func_cap->mpt_quota;
  851. dev->quotas.mtt = func_cap->mtt_quota;
  852. dev->caps.num_qps = 1 << hca_param->log_num_qps;
  853. dev->caps.num_srqs = 1 << hca_param->log_num_srqs;
  854. dev->caps.num_cqs = 1 << hca_param->log_num_cqs;
  855. dev->caps.num_mpts = 1 << hca_param->log_mpt_sz;
  856. dev->caps.num_eqs = func_cap->max_eq;
  857. dev->caps.reserved_eqs = func_cap->reserved_eq;
  858. dev->caps.reserved_lkey = func_cap->reserved_lkey;
  859. dev->caps.num_pds = MLX4_NUM_PDS;
  860. dev->caps.num_mgms = 0;
  861. dev->caps.num_amgms = 0;
  862. if (dev->caps.num_ports > MLX4_MAX_PORTS) {
  863. mlx4_err(dev, "HCA has %d ports, but we only support %d, aborting\n",
  864. dev->caps.num_ports, MLX4_MAX_PORTS);
  865. err = -ENODEV;
  866. goto free_mem;
  867. }
  868. mlx4_replace_zero_macs(dev);
  869. err = mlx4_slave_special_qp_cap(dev);
  870. if (err) {
  871. mlx4_err(dev, "Set special QP caps failed. aborting\n");
  872. goto free_mem;
  873. }
  874. if (dev->caps.uar_page_size * (dev->caps.num_uars -
  875. dev->caps.reserved_uars) >
  876. pci_resource_len(dev->persist->pdev,
  877. 2)) {
  878. mlx4_err(dev, "HCA reported UAR region size of 0x%x bigger than PCI resource 2 size of 0x%llx, aborting\n",
  879. dev->caps.uar_page_size * dev->caps.num_uars,
  880. (unsigned long long)
  881. pci_resource_len(dev->persist->pdev, 2));
  882. err = -ENOMEM;
  883. goto err_mem;
  884. }
  885. if (hca_param->dev_cap_enabled & MLX4_DEV_CAP_64B_EQE_ENABLED) {
  886. dev->caps.eqe_size = 64;
  887. dev->caps.eqe_factor = 1;
  888. } else {
  889. dev->caps.eqe_size = 32;
  890. dev->caps.eqe_factor = 0;
  891. }
  892. if (hca_param->dev_cap_enabled & MLX4_DEV_CAP_64B_CQE_ENABLED) {
  893. dev->caps.cqe_size = 64;
  894. dev->caps.userspace_caps |= MLX4_USER_DEV_CAP_LARGE_CQE;
  895. } else {
  896. dev->caps.cqe_size = 32;
  897. }
  898. if (hca_param->dev_cap_enabled & MLX4_DEV_CAP_EQE_STRIDE_ENABLED) {
  899. dev->caps.eqe_size = hca_param->eqe_size;
  900. dev->caps.eqe_factor = 0;
  901. }
  902. if (hca_param->dev_cap_enabled & MLX4_DEV_CAP_CQE_STRIDE_ENABLED) {
  903. dev->caps.cqe_size = hca_param->cqe_size;
  904. /* User still need to know when CQE > 32B */
  905. dev->caps.userspace_caps |= MLX4_USER_DEV_CAP_LARGE_CQE;
  906. }
  907. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
  908. mlx4_warn(dev, "Timestamping is not supported in slave mode\n");
  909. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_USER_MAC_EN;
  910. mlx4_dbg(dev, "User MAC FW update is not supported in slave mode\n");
  911. slave_adjust_steering_mode(dev, dev_cap, hca_param);
  912. mlx4_dbg(dev, "RSS support for IP fragments is %s\n",
  913. hca_param->rss_ip_frags ? "on" : "off");
  914. if (func_cap->extra_flags & MLX4_QUERY_FUNC_FLAGS_BF_RES_QP &&
  915. dev->caps.bf_reg_size)
  916. dev->caps.alloc_res_qp_mask |= MLX4_RESERVE_ETH_BF_QP;
  917. if (func_cap->extra_flags & MLX4_QUERY_FUNC_FLAGS_A0_RES_QP)
  918. dev->caps.alloc_res_qp_mask |= MLX4_RESERVE_A0_QP;
  919. err_mem:
  920. if (err)
  921. mlx4_slave_destroy_special_qp_cap(dev);
  922. free_mem:
  923. kfree(hca_param);
  924. kfree(func_cap);
  925. kfree(dev_cap);
  926. return err;
  927. }
  928. /*
  929. * Change the port configuration of the device.
  930. * Every user of this function must hold the port mutex.
  931. */
  932. int mlx4_change_port_types(struct mlx4_dev *dev,
  933. enum mlx4_port_type *port_types)
  934. {
  935. int err = 0;
  936. int change = 0;
  937. int port;
  938. for (port = 0; port < dev->caps.num_ports; port++) {
  939. /* Change the port type only if the new type is different
  940. * from the current, and not set to Auto */
  941. if (port_types[port] != dev->caps.port_type[port + 1])
  942. change = 1;
  943. }
  944. if (change) {
  945. mlx4_unregister_device(dev);
  946. for (port = 1; port <= dev->caps.num_ports; port++) {
  947. mlx4_CLOSE_PORT(dev, port);
  948. dev->caps.port_type[port] = port_types[port - 1];
  949. err = mlx4_SET_PORT(dev, port, -1);
  950. if (err) {
  951. mlx4_err(dev, "Failed to set port %d, aborting\n",
  952. port);
  953. goto out;
  954. }
  955. }
  956. mlx4_set_port_mask(dev);
  957. err = mlx4_register_device(dev);
  958. if (err) {
  959. mlx4_err(dev, "Failed to register device\n");
  960. goto out;
  961. }
  962. }
  963. out:
  964. return err;
  965. }
  966. static ssize_t show_port_type(struct device *dev,
  967. struct device_attribute *attr,
  968. char *buf)
  969. {
  970. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  971. port_attr);
  972. struct mlx4_dev *mdev = info->dev;
  973. char type[8];
  974. sprintf(type, "%s",
  975. (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_IB) ?
  976. "ib" : "eth");
  977. if (mdev->caps.possible_type[info->port] == MLX4_PORT_TYPE_AUTO)
  978. sprintf(buf, "auto (%s)\n", type);
  979. else
  980. sprintf(buf, "%s\n", type);
  981. return strlen(buf);
  982. }
  983. static int __set_port_type(struct mlx4_port_info *info,
  984. enum mlx4_port_type port_type)
  985. {
  986. struct mlx4_dev *mdev = info->dev;
  987. struct mlx4_priv *priv = mlx4_priv(mdev);
  988. enum mlx4_port_type types[MLX4_MAX_PORTS];
  989. enum mlx4_port_type new_types[MLX4_MAX_PORTS];
  990. int i;
  991. int err = 0;
  992. if ((port_type & mdev->caps.supported_type[info->port]) != port_type) {
  993. mlx4_err(mdev,
  994. "Requested port type for port %d is not supported on this HCA\n",
  995. info->port);
  996. return -EOPNOTSUPP;
  997. }
  998. mlx4_stop_sense(mdev);
  999. mutex_lock(&priv->port_mutex);
  1000. info->tmp_type = port_type;
  1001. /* Possible type is always the one that was delivered */
  1002. mdev->caps.possible_type[info->port] = info->tmp_type;
  1003. for (i = 0; i < mdev->caps.num_ports; i++) {
  1004. types[i] = priv->port[i+1].tmp_type ? priv->port[i+1].tmp_type :
  1005. mdev->caps.possible_type[i+1];
  1006. if (types[i] == MLX4_PORT_TYPE_AUTO)
  1007. types[i] = mdev->caps.port_type[i+1];
  1008. }
  1009. if (!(mdev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
  1010. !(mdev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT)) {
  1011. for (i = 1; i <= mdev->caps.num_ports; i++) {
  1012. if (mdev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
  1013. mdev->caps.possible_type[i] = mdev->caps.port_type[i];
  1014. err = -EOPNOTSUPP;
  1015. }
  1016. }
  1017. }
  1018. if (err) {
  1019. mlx4_err(mdev, "Auto sensing is not supported on this HCA. Set only 'eth' or 'ib' for both ports (should be the same)\n");
  1020. goto out;
  1021. }
  1022. mlx4_do_sense_ports(mdev, new_types, types);
  1023. err = mlx4_check_port_params(mdev, new_types);
  1024. if (err)
  1025. goto out;
  1026. /* We are about to apply the changes after the configuration
  1027. * was verified, no need to remember the temporary types
  1028. * any more */
  1029. for (i = 0; i < mdev->caps.num_ports; i++)
  1030. priv->port[i + 1].tmp_type = 0;
  1031. err = mlx4_change_port_types(mdev, new_types);
  1032. out:
  1033. mlx4_start_sense(mdev);
  1034. mutex_unlock(&priv->port_mutex);
  1035. return err;
  1036. }
  1037. static ssize_t set_port_type(struct device *dev,
  1038. struct device_attribute *attr,
  1039. const char *buf, size_t count)
  1040. {
  1041. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  1042. port_attr);
  1043. struct mlx4_dev *mdev = info->dev;
  1044. enum mlx4_port_type port_type;
  1045. static DEFINE_MUTEX(set_port_type_mutex);
  1046. int err;
  1047. mutex_lock(&set_port_type_mutex);
  1048. if (!strcmp(buf, "ib\n")) {
  1049. port_type = MLX4_PORT_TYPE_IB;
  1050. } else if (!strcmp(buf, "eth\n")) {
  1051. port_type = MLX4_PORT_TYPE_ETH;
  1052. } else if (!strcmp(buf, "auto\n")) {
  1053. port_type = MLX4_PORT_TYPE_AUTO;
  1054. } else {
  1055. mlx4_err(mdev, "%s is not supported port type\n", buf);
  1056. err = -EINVAL;
  1057. goto err_out;
  1058. }
  1059. err = __set_port_type(info, port_type);
  1060. err_out:
  1061. mutex_unlock(&set_port_type_mutex);
  1062. return err ? err : count;
  1063. }
  1064. enum ibta_mtu {
  1065. IB_MTU_256 = 1,
  1066. IB_MTU_512 = 2,
  1067. IB_MTU_1024 = 3,
  1068. IB_MTU_2048 = 4,
  1069. IB_MTU_4096 = 5
  1070. };
  1071. static inline int int_to_ibta_mtu(int mtu)
  1072. {
  1073. switch (mtu) {
  1074. case 256: return IB_MTU_256;
  1075. case 512: return IB_MTU_512;
  1076. case 1024: return IB_MTU_1024;
  1077. case 2048: return IB_MTU_2048;
  1078. case 4096: return IB_MTU_4096;
  1079. default: return -1;
  1080. }
  1081. }
  1082. static inline int ibta_mtu_to_int(enum ibta_mtu mtu)
  1083. {
  1084. switch (mtu) {
  1085. case IB_MTU_256: return 256;
  1086. case IB_MTU_512: return 512;
  1087. case IB_MTU_1024: return 1024;
  1088. case IB_MTU_2048: return 2048;
  1089. case IB_MTU_4096: return 4096;
  1090. default: return -1;
  1091. }
  1092. }
  1093. static ssize_t show_port_ib_mtu(struct device *dev,
  1094. struct device_attribute *attr,
  1095. char *buf)
  1096. {
  1097. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  1098. port_mtu_attr);
  1099. struct mlx4_dev *mdev = info->dev;
  1100. if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH)
  1101. mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
  1102. sprintf(buf, "%d\n",
  1103. ibta_mtu_to_int(mdev->caps.port_ib_mtu[info->port]));
  1104. return strlen(buf);
  1105. }
  1106. static ssize_t set_port_ib_mtu(struct device *dev,
  1107. struct device_attribute *attr,
  1108. const char *buf, size_t count)
  1109. {
  1110. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  1111. port_mtu_attr);
  1112. struct mlx4_dev *mdev = info->dev;
  1113. struct mlx4_priv *priv = mlx4_priv(mdev);
  1114. int err, port, mtu, ibta_mtu = -1;
  1115. if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH) {
  1116. mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
  1117. return -EINVAL;
  1118. }
  1119. err = kstrtoint(buf, 0, &mtu);
  1120. if (!err)
  1121. ibta_mtu = int_to_ibta_mtu(mtu);
  1122. if (err || ibta_mtu < 0) {
  1123. mlx4_err(mdev, "%s is invalid IBTA mtu\n", buf);
  1124. return -EINVAL;
  1125. }
  1126. mdev->caps.port_ib_mtu[info->port] = ibta_mtu;
  1127. mlx4_stop_sense(mdev);
  1128. mutex_lock(&priv->port_mutex);
  1129. mlx4_unregister_device(mdev);
  1130. for (port = 1; port <= mdev->caps.num_ports; port++) {
  1131. mlx4_CLOSE_PORT(mdev, port);
  1132. err = mlx4_SET_PORT(mdev, port, -1);
  1133. if (err) {
  1134. mlx4_err(mdev, "Failed to set port %d, aborting\n",
  1135. port);
  1136. goto err_set_port;
  1137. }
  1138. }
  1139. err = mlx4_register_device(mdev);
  1140. err_set_port:
  1141. mutex_unlock(&priv->port_mutex);
  1142. mlx4_start_sense(mdev);
  1143. return err ? err : count;
  1144. }
  1145. /* bond for multi-function device */
  1146. #define MAX_MF_BOND_ALLOWED_SLAVES 63
  1147. static int mlx4_mf_bond(struct mlx4_dev *dev)
  1148. {
  1149. int err = 0;
  1150. int nvfs;
  1151. struct mlx4_slaves_pport slaves_port1;
  1152. struct mlx4_slaves_pport slaves_port2;
  1153. slaves_port1 = mlx4_phys_to_slaves_pport(dev, 1);
  1154. slaves_port2 = mlx4_phys_to_slaves_pport(dev, 2);
  1155. /* only single port vfs are allowed */
  1156. if (bitmap_weight_and(slaves_port1.slaves, slaves_port2.slaves,
  1157. dev->persist->num_vfs + 1) > 1) {
  1158. mlx4_warn(dev, "HA mode unsupported for dual ported VFs\n");
  1159. return -EINVAL;
  1160. }
  1161. /* number of virtual functions is number of total functions minus one
  1162. * physical function for each port.
  1163. */
  1164. nvfs = bitmap_weight(slaves_port1.slaves, dev->persist->num_vfs + 1) +
  1165. bitmap_weight(slaves_port2.slaves, dev->persist->num_vfs + 1) - 2;
  1166. /* limit on maximum allowed VFs */
  1167. if (nvfs > MAX_MF_BOND_ALLOWED_SLAVES) {
  1168. mlx4_warn(dev, "HA mode is not supported for %d VFs (max %d are allowed)\n",
  1169. nvfs, MAX_MF_BOND_ALLOWED_SLAVES);
  1170. return -EINVAL;
  1171. }
  1172. if (dev->caps.steering_mode != MLX4_STEERING_MODE_DEVICE_MANAGED) {
  1173. mlx4_warn(dev, "HA mode unsupported for NON DMFS steering\n");
  1174. return -EINVAL;
  1175. }
  1176. err = mlx4_bond_mac_table(dev);
  1177. if (err)
  1178. return err;
  1179. err = mlx4_bond_vlan_table(dev);
  1180. if (err)
  1181. goto err1;
  1182. err = mlx4_bond_fs_rules(dev);
  1183. if (err)
  1184. goto err2;
  1185. return 0;
  1186. err2:
  1187. (void)mlx4_unbond_vlan_table(dev);
  1188. err1:
  1189. (void)mlx4_unbond_mac_table(dev);
  1190. return err;
  1191. }
  1192. static int mlx4_mf_unbond(struct mlx4_dev *dev)
  1193. {
  1194. int ret, ret1;
  1195. ret = mlx4_unbond_fs_rules(dev);
  1196. if (ret)
  1197. mlx4_warn(dev, "multifunction unbond for flow rules failed (%d)\n", ret);
  1198. ret1 = mlx4_unbond_mac_table(dev);
  1199. if (ret1) {
  1200. mlx4_warn(dev, "multifunction unbond for MAC table failed (%d)\n", ret1);
  1201. ret = ret1;
  1202. }
  1203. ret1 = mlx4_unbond_vlan_table(dev);
  1204. if (ret1) {
  1205. mlx4_warn(dev, "multifunction unbond for VLAN table failed (%d)\n", ret1);
  1206. ret = ret1;
  1207. }
  1208. return ret;
  1209. }
  1210. static int mlx4_bond(struct mlx4_dev *dev)
  1211. {
  1212. int ret = 0;
  1213. struct mlx4_priv *priv = mlx4_priv(dev);
  1214. mutex_lock(&priv->bond_mutex);
  1215. if (!mlx4_is_bonded(dev)) {
  1216. ret = mlx4_do_bond(dev, true);
  1217. if (ret)
  1218. mlx4_err(dev, "Failed to bond device: %d\n", ret);
  1219. if (!ret && mlx4_is_master(dev)) {
  1220. ret = mlx4_mf_bond(dev);
  1221. if (ret) {
  1222. mlx4_err(dev, "bond for multifunction failed\n");
  1223. mlx4_do_bond(dev, false);
  1224. }
  1225. }
  1226. }
  1227. mutex_unlock(&priv->bond_mutex);
  1228. if (!ret)
  1229. mlx4_dbg(dev, "Device is bonded\n");
  1230. return ret;
  1231. }
  1232. static int mlx4_unbond(struct mlx4_dev *dev)
  1233. {
  1234. int ret = 0;
  1235. struct mlx4_priv *priv = mlx4_priv(dev);
  1236. mutex_lock(&priv->bond_mutex);
  1237. if (mlx4_is_bonded(dev)) {
  1238. int ret2 = 0;
  1239. ret = mlx4_do_bond(dev, false);
  1240. if (ret)
  1241. mlx4_err(dev, "Failed to unbond device: %d\n", ret);
  1242. if (mlx4_is_master(dev))
  1243. ret2 = mlx4_mf_unbond(dev);
  1244. if (ret2) {
  1245. mlx4_warn(dev, "Failed to unbond device for multifunction (%d)\n", ret2);
  1246. ret = ret2;
  1247. }
  1248. }
  1249. mutex_unlock(&priv->bond_mutex);
  1250. if (!ret)
  1251. mlx4_dbg(dev, "Device is unbonded\n");
  1252. return ret;
  1253. }
  1254. static int mlx4_port_map_set(struct mlx4_dev *dev, struct mlx4_port_map *v2p)
  1255. {
  1256. u8 port1 = v2p->port1;
  1257. u8 port2 = v2p->port2;
  1258. struct mlx4_priv *priv = mlx4_priv(dev);
  1259. int err;
  1260. if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PORT_REMAP))
  1261. return -EOPNOTSUPP;
  1262. mutex_lock(&priv->bond_mutex);
  1263. /* zero means keep current mapping for this port */
  1264. if (port1 == 0)
  1265. port1 = priv->v2p.port1;
  1266. if (port2 == 0)
  1267. port2 = priv->v2p.port2;
  1268. if ((port1 < 1) || (port1 > MLX4_MAX_PORTS) ||
  1269. (port2 < 1) || (port2 > MLX4_MAX_PORTS) ||
  1270. (port1 == 2 && port2 == 1)) {
  1271. /* besides boundary checks cross mapping makes
  1272. * no sense and therefore not allowed */
  1273. err = -EINVAL;
  1274. } else if ((port1 == priv->v2p.port1) &&
  1275. (port2 == priv->v2p.port2)) {
  1276. err = 0;
  1277. } else {
  1278. err = mlx4_virt2phy_port_map(dev, port1, port2);
  1279. if (!err) {
  1280. mlx4_dbg(dev, "port map changed: [%d][%d]\n",
  1281. port1, port2);
  1282. priv->v2p.port1 = port1;
  1283. priv->v2p.port2 = port2;
  1284. } else {
  1285. mlx4_err(dev, "Failed to change port map: %d\n", err);
  1286. }
  1287. }
  1288. mutex_unlock(&priv->bond_mutex);
  1289. return err;
  1290. }
  1291. struct mlx4_bond {
  1292. struct work_struct work;
  1293. struct mlx4_dev *dev;
  1294. int is_bonded;
  1295. struct mlx4_port_map port_map;
  1296. };
  1297. static void mlx4_bond_work(struct work_struct *work)
  1298. {
  1299. struct mlx4_bond *bond = container_of(work, struct mlx4_bond, work);
  1300. int err = 0;
  1301. if (bond->is_bonded) {
  1302. if (!mlx4_is_bonded(bond->dev)) {
  1303. err = mlx4_bond(bond->dev);
  1304. if (err)
  1305. mlx4_err(bond->dev, "Fail to bond device\n");
  1306. }
  1307. if (!err) {
  1308. err = mlx4_port_map_set(bond->dev, &bond->port_map);
  1309. if (err)
  1310. mlx4_err(bond->dev,
  1311. "Fail to set port map [%d][%d]: %d\n",
  1312. bond->port_map.port1,
  1313. bond->port_map.port2, err);
  1314. }
  1315. } else if (mlx4_is_bonded(bond->dev)) {
  1316. err = mlx4_unbond(bond->dev);
  1317. if (err)
  1318. mlx4_err(bond->dev, "Fail to unbond device\n");
  1319. }
  1320. put_device(&bond->dev->persist->pdev->dev);
  1321. kfree(bond);
  1322. }
  1323. int mlx4_queue_bond_work(struct mlx4_dev *dev, int is_bonded, u8 v2p_p1,
  1324. u8 v2p_p2)
  1325. {
  1326. struct mlx4_bond *bond;
  1327. bond = kzalloc(sizeof(*bond), GFP_ATOMIC);
  1328. if (!bond)
  1329. return -ENOMEM;
  1330. INIT_WORK(&bond->work, mlx4_bond_work);
  1331. get_device(&dev->persist->pdev->dev);
  1332. bond->dev = dev;
  1333. bond->is_bonded = is_bonded;
  1334. bond->port_map.port1 = v2p_p1;
  1335. bond->port_map.port2 = v2p_p2;
  1336. queue_work(mlx4_wq, &bond->work);
  1337. return 0;
  1338. }
  1339. EXPORT_SYMBOL(mlx4_queue_bond_work);
  1340. static int mlx4_load_fw(struct mlx4_dev *dev)
  1341. {
  1342. struct mlx4_priv *priv = mlx4_priv(dev);
  1343. int err;
  1344. priv->fw.fw_icm = mlx4_alloc_icm(dev, priv->fw.fw_pages,
  1345. GFP_HIGHUSER | __GFP_NOWARN, 0);
  1346. if (!priv->fw.fw_icm) {
  1347. mlx4_err(dev, "Couldn't allocate FW area, aborting\n");
  1348. return -ENOMEM;
  1349. }
  1350. err = mlx4_MAP_FA(dev, priv->fw.fw_icm);
  1351. if (err) {
  1352. mlx4_err(dev, "MAP_FA command failed, aborting\n");
  1353. goto err_free;
  1354. }
  1355. err = mlx4_RUN_FW(dev);
  1356. if (err) {
  1357. mlx4_err(dev, "RUN_FW command failed, aborting\n");
  1358. goto err_unmap_fa;
  1359. }
  1360. return 0;
  1361. err_unmap_fa:
  1362. mlx4_UNMAP_FA(dev);
  1363. err_free:
  1364. mlx4_free_icm(dev, priv->fw.fw_icm, 0);
  1365. return err;
  1366. }
  1367. static int mlx4_init_cmpt_table(struct mlx4_dev *dev, u64 cmpt_base,
  1368. int cmpt_entry_sz)
  1369. {
  1370. struct mlx4_priv *priv = mlx4_priv(dev);
  1371. int err;
  1372. int num_eqs;
  1373. err = mlx4_init_icm_table(dev, &priv->qp_table.cmpt_table,
  1374. cmpt_base +
  1375. ((u64) (MLX4_CMPT_TYPE_QP *
  1376. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  1377. cmpt_entry_sz, dev->caps.num_qps,
  1378. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1379. 0, 0);
  1380. if (err)
  1381. goto err;
  1382. err = mlx4_init_icm_table(dev, &priv->srq_table.cmpt_table,
  1383. cmpt_base +
  1384. ((u64) (MLX4_CMPT_TYPE_SRQ *
  1385. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  1386. cmpt_entry_sz, dev->caps.num_srqs,
  1387. dev->caps.reserved_srqs, 0, 0);
  1388. if (err)
  1389. goto err_qp;
  1390. err = mlx4_init_icm_table(dev, &priv->cq_table.cmpt_table,
  1391. cmpt_base +
  1392. ((u64) (MLX4_CMPT_TYPE_CQ *
  1393. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  1394. cmpt_entry_sz, dev->caps.num_cqs,
  1395. dev->caps.reserved_cqs, 0, 0);
  1396. if (err)
  1397. goto err_srq;
  1398. num_eqs = dev->phys_caps.num_phys_eqs;
  1399. err = mlx4_init_icm_table(dev, &priv->eq_table.cmpt_table,
  1400. cmpt_base +
  1401. ((u64) (MLX4_CMPT_TYPE_EQ *
  1402. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  1403. cmpt_entry_sz, num_eqs, num_eqs, 0, 0);
  1404. if (err)
  1405. goto err_cq;
  1406. return 0;
  1407. err_cq:
  1408. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  1409. err_srq:
  1410. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  1411. err_qp:
  1412. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  1413. err:
  1414. return err;
  1415. }
  1416. static int mlx4_init_icm(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
  1417. struct mlx4_init_hca_param *init_hca, u64 icm_size)
  1418. {
  1419. struct mlx4_priv *priv = mlx4_priv(dev);
  1420. u64 aux_pages;
  1421. int num_eqs;
  1422. int err;
  1423. err = mlx4_SET_ICM_SIZE(dev, icm_size, &aux_pages);
  1424. if (err) {
  1425. mlx4_err(dev, "SET_ICM_SIZE command failed, aborting\n");
  1426. return err;
  1427. }
  1428. mlx4_dbg(dev, "%lld KB of HCA context requires %lld KB aux memory\n",
  1429. (unsigned long long) icm_size >> 10,
  1430. (unsigned long long) aux_pages << 2);
  1431. priv->fw.aux_icm = mlx4_alloc_icm(dev, aux_pages,
  1432. GFP_HIGHUSER | __GFP_NOWARN, 0);
  1433. if (!priv->fw.aux_icm) {
  1434. mlx4_err(dev, "Couldn't allocate aux memory, aborting\n");
  1435. return -ENOMEM;
  1436. }
  1437. err = mlx4_MAP_ICM_AUX(dev, priv->fw.aux_icm);
  1438. if (err) {
  1439. mlx4_err(dev, "MAP_ICM_AUX command failed, aborting\n");
  1440. goto err_free_aux;
  1441. }
  1442. err = mlx4_init_cmpt_table(dev, init_hca->cmpt_base, dev_cap->cmpt_entry_sz);
  1443. if (err) {
  1444. mlx4_err(dev, "Failed to map cMPT context memory, aborting\n");
  1445. goto err_unmap_aux;
  1446. }
  1447. num_eqs = dev->phys_caps.num_phys_eqs;
  1448. err = mlx4_init_icm_table(dev, &priv->eq_table.table,
  1449. init_hca->eqc_base, dev_cap->eqc_entry_sz,
  1450. num_eqs, num_eqs, 0, 0);
  1451. if (err) {
  1452. mlx4_err(dev, "Failed to map EQ context memory, aborting\n");
  1453. goto err_unmap_cmpt;
  1454. }
  1455. /*
  1456. * Reserved MTT entries must be aligned up to a cacheline
  1457. * boundary, since the FW will write to them, while the driver
  1458. * writes to all other MTT entries. (The variable
  1459. * dev->caps.mtt_entry_sz below is really the MTT segment
  1460. * size, not the raw entry size)
  1461. */
  1462. dev->caps.reserved_mtts =
  1463. ALIGN(dev->caps.reserved_mtts * dev->caps.mtt_entry_sz,
  1464. dma_get_cache_alignment()) / dev->caps.mtt_entry_sz;
  1465. err = mlx4_init_icm_table(dev, &priv->mr_table.mtt_table,
  1466. init_hca->mtt_base,
  1467. dev->caps.mtt_entry_sz,
  1468. dev->caps.num_mtts,
  1469. dev->caps.reserved_mtts, 1, 0);
  1470. if (err) {
  1471. mlx4_err(dev, "Failed to map MTT context memory, aborting\n");
  1472. goto err_unmap_eq;
  1473. }
  1474. err = mlx4_init_icm_table(dev, &priv->mr_table.dmpt_table,
  1475. init_hca->dmpt_base,
  1476. dev_cap->dmpt_entry_sz,
  1477. dev->caps.num_mpts,
  1478. dev->caps.reserved_mrws, 1, 1);
  1479. if (err) {
  1480. mlx4_err(dev, "Failed to map dMPT context memory, aborting\n");
  1481. goto err_unmap_mtt;
  1482. }
  1483. err = mlx4_init_icm_table(dev, &priv->qp_table.qp_table,
  1484. init_hca->qpc_base,
  1485. dev_cap->qpc_entry_sz,
  1486. dev->caps.num_qps,
  1487. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1488. 0, 0);
  1489. if (err) {
  1490. mlx4_err(dev, "Failed to map QP context memory, aborting\n");
  1491. goto err_unmap_dmpt;
  1492. }
  1493. err = mlx4_init_icm_table(dev, &priv->qp_table.auxc_table,
  1494. init_hca->auxc_base,
  1495. dev_cap->aux_entry_sz,
  1496. dev->caps.num_qps,
  1497. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1498. 0, 0);
  1499. if (err) {
  1500. mlx4_err(dev, "Failed to map AUXC context memory, aborting\n");
  1501. goto err_unmap_qp;
  1502. }
  1503. err = mlx4_init_icm_table(dev, &priv->qp_table.altc_table,
  1504. init_hca->altc_base,
  1505. dev_cap->altc_entry_sz,
  1506. dev->caps.num_qps,
  1507. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1508. 0, 0);
  1509. if (err) {
  1510. mlx4_err(dev, "Failed to map ALTC context memory, aborting\n");
  1511. goto err_unmap_auxc;
  1512. }
  1513. err = mlx4_init_icm_table(dev, &priv->qp_table.rdmarc_table,
  1514. init_hca->rdmarc_base,
  1515. dev_cap->rdmarc_entry_sz << priv->qp_table.rdmarc_shift,
  1516. dev->caps.num_qps,
  1517. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1518. 0, 0);
  1519. if (err) {
  1520. mlx4_err(dev, "Failed to map RDMARC context memory, aborting\n");
  1521. goto err_unmap_altc;
  1522. }
  1523. err = mlx4_init_icm_table(dev, &priv->cq_table.table,
  1524. init_hca->cqc_base,
  1525. dev_cap->cqc_entry_sz,
  1526. dev->caps.num_cqs,
  1527. dev->caps.reserved_cqs, 0, 0);
  1528. if (err) {
  1529. mlx4_err(dev, "Failed to map CQ context memory, aborting\n");
  1530. goto err_unmap_rdmarc;
  1531. }
  1532. err = mlx4_init_icm_table(dev, &priv->srq_table.table,
  1533. init_hca->srqc_base,
  1534. dev_cap->srq_entry_sz,
  1535. dev->caps.num_srqs,
  1536. dev->caps.reserved_srqs, 0, 0);
  1537. if (err) {
  1538. mlx4_err(dev, "Failed to map SRQ context memory, aborting\n");
  1539. goto err_unmap_cq;
  1540. }
  1541. /*
  1542. * For flow steering device managed mode it is required to use
  1543. * mlx4_init_icm_table. For B0 steering mode it's not strictly
  1544. * required, but for simplicity just map the whole multicast
  1545. * group table now. The table isn't very big and it's a lot
  1546. * easier than trying to track ref counts.
  1547. */
  1548. err = mlx4_init_icm_table(dev, &priv->mcg_table.table,
  1549. init_hca->mc_base,
  1550. mlx4_get_mgm_entry_size(dev),
  1551. dev->caps.num_mgms + dev->caps.num_amgms,
  1552. dev->caps.num_mgms + dev->caps.num_amgms,
  1553. 0, 0);
  1554. if (err) {
  1555. mlx4_err(dev, "Failed to map MCG context memory, aborting\n");
  1556. goto err_unmap_srq;
  1557. }
  1558. return 0;
  1559. err_unmap_srq:
  1560. mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
  1561. err_unmap_cq:
  1562. mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
  1563. err_unmap_rdmarc:
  1564. mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
  1565. err_unmap_altc:
  1566. mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
  1567. err_unmap_auxc:
  1568. mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
  1569. err_unmap_qp:
  1570. mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
  1571. err_unmap_dmpt:
  1572. mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
  1573. err_unmap_mtt:
  1574. mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
  1575. err_unmap_eq:
  1576. mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
  1577. err_unmap_cmpt:
  1578. mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
  1579. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  1580. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  1581. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  1582. err_unmap_aux:
  1583. mlx4_UNMAP_ICM_AUX(dev);
  1584. err_free_aux:
  1585. mlx4_free_icm(dev, priv->fw.aux_icm, 0);
  1586. return err;
  1587. }
  1588. static void mlx4_free_icms(struct mlx4_dev *dev)
  1589. {
  1590. struct mlx4_priv *priv = mlx4_priv(dev);
  1591. mlx4_cleanup_icm_table(dev, &priv->mcg_table.table);
  1592. mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
  1593. mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
  1594. mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
  1595. mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
  1596. mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
  1597. mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
  1598. mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
  1599. mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
  1600. mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
  1601. mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
  1602. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  1603. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  1604. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  1605. mlx4_UNMAP_ICM_AUX(dev);
  1606. mlx4_free_icm(dev, priv->fw.aux_icm, 0);
  1607. }
  1608. static void mlx4_slave_exit(struct mlx4_dev *dev)
  1609. {
  1610. struct mlx4_priv *priv = mlx4_priv(dev);
  1611. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1612. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, MLX4_COMM_CMD_NA_OP,
  1613. MLX4_COMM_TIME))
  1614. mlx4_warn(dev, "Failed to close slave function\n");
  1615. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1616. }
  1617. static int map_bf_area(struct mlx4_dev *dev)
  1618. {
  1619. struct mlx4_priv *priv = mlx4_priv(dev);
  1620. resource_size_t bf_start;
  1621. resource_size_t bf_len;
  1622. int err = 0;
  1623. if (!dev->caps.bf_reg_size)
  1624. return -ENXIO;
  1625. bf_start = pci_resource_start(dev->persist->pdev, 2) +
  1626. (dev->caps.num_uars << PAGE_SHIFT);
  1627. bf_len = pci_resource_len(dev->persist->pdev, 2) -
  1628. (dev->caps.num_uars << PAGE_SHIFT);
  1629. priv->bf_mapping = io_mapping_create_wc(bf_start, bf_len);
  1630. if (!priv->bf_mapping)
  1631. err = -ENOMEM;
  1632. return err;
  1633. }
  1634. static void unmap_bf_area(struct mlx4_dev *dev)
  1635. {
  1636. if (mlx4_priv(dev)->bf_mapping)
  1637. io_mapping_free(mlx4_priv(dev)->bf_mapping);
  1638. }
  1639. u64 mlx4_read_clock(struct mlx4_dev *dev)
  1640. {
  1641. u32 clockhi, clocklo, clockhi1;
  1642. u64 cycles;
  1643. int i;
  1644. struct mlx4_priv *priv = mlx4_priv(dev);
  1645. for (i = 0; i < 10; i++) {
  1646. clockhi = swab32(readl(priv->clock_mapping));
  1647. clocklo = swab32(readl(priv->clock_mapping + 4));
  1648. clockhi1 = swab32(readl(priv->clock_mapping));
  1649. if (clockhi == clockhi1)
  1650. break;
  1651. }
  1652. cycles = (u64) clockhi << 32 | (u64) clocklo;
  1653. return cycles;
  1654. }
  1655. EXPORT_SYMBOL_GPL(mlx4_read_clock);
  1656. static int map_internal_clock(struct mlx4_dev *dev)
  1657. {
  1658. struct mlx4_priv *priv = mlx4_priv(dev);
  1659. priv->clock_mapping =
  1660. ioremap(pci_resource_start(dev->persist->pdev,
  1661. priv->fw.clock_bar) +
  1662. priv->fw.clock_offset, MLX4_CLOCK_SIZE);
  1663. if (!priv->clock_mapping)
  1664. return -ENOMEM;
  1665. return 0;
  1666. }
  1667. int mlx4_get_internal_clock_params(struct mlx4_dev *dev,
  1668. struct mlx4_clock_params *params)
  1669. {
  1670. struct mlx4_priv *priv = mlx4_priv(dev);
  1671. if (mlx4_is_slave(dev))
  1672. return -EOPNOTSUPP;
  1673. if (!dev->caps.map_clock_to_user) {
  1674. mlx4_dbg(dev, "Map clock to user is not supported.\n");
  1675. return -EOPNOTSUPP;
  1676. }
  1677. if (!params)
  1678. return -EINVAL;
  1679. params->bar = priv->fw.clock_bar;
  1680. params->offset = priv->fw.clock_offset;
  1681. params->size = MLX4_CLOCK_SIZE;
  1682. return 0;
  1683. }
  1684. EXPORT_SYMBOL_GPL(mlx4_get_internal_clock_params);
  1685. static void unmap_internal_clock(struct mlx4_dev *dev)
  1686. {
  1687. struct mlx4_priv *priv = mlx4_priv(dev);
  1688. if (priv->clock_mapping)
  1689. iounmap(priv->clock_mapping);
  1690. }
  1691. static void mlx4_close_hca(struct mlx4_dev *dev)
  1692. {
  1693. unmap_internal_clock(dev);
  1694. unmap_bf_area(dev);
  1695. if (mlx4_is_slave(dev))
  1696. mlx4_slave_exit(dev);
  1697. else {
  1698. mlx4_CLOSE_HCA(dev, 0);
  1699. mlx4_free_icms(dev);
  1700. }
  1701. }
  1702. static void mlx4_close_fw(struct mlx4_dev *dev)
  1703. {
  1704. if (!mlx4_is_slave(dev)) {
  1705. mlx4_UNMAP_FA(dev);
  1706. mlx4_free_icm(dev, mlx4_priv(dev)->fw.fw_icm, 0);
  1707. }
  1708. }
  1709. static int mlx4_comm_check_offline(struct mlx4_dev *dev)
  1710. {
  1711. #define COMM_CHAN_OFFLINE_OFFSET 0x09
  1712. u32 comm_flags;
  1713. u32 offline_bit;
  1714. unsigned long end;
  1715. struct mlx4_priv *priv = mlx4_priv(dev);
  1716. end = msecs_to_jiffies(MLX4_COMM_OFFLINE_TIME_OUT) + jiffies;
  1717. while (time_before(jiffies, end)) {
  1718. comm_flags = swab32(readl((__iomem char *)priv->mfunc.comm +
  1719. MLX4_COMM_CHAN_FLAGS));
  1720. offline_bit = (comm_flags &
  1721. (u32)(1 << COMM_CHAN_OFFLINE_OFFSET));
  1722. if (!offline_bit)
  1723. return 0;
  1724. /* If device removal has been requested,
  1725. * do not continue retrying.
  1726. */
  1727. if (dev->persist->interface_state &
  1728. MLX4_INTERFACE_STATE_NOWAIT)
  1729. break;
  1730. /* There are cases as part of AER/Reset flow that PF needs
  1731. * around 100 msec to load. We therefore sleep for 100 msec
  1732. * to allow other tasks to make use of that CPU during this
  1733. * time interval.
  1734. */
  1735. msleep(100);
  1736. }
  1737. mlx4_err(dev, "Communication channel is offline.\n");
  1738. return -EIO;
  1739. }
  1740. static void mlx4_reset_vf_support(struct mlx4_dev *dev)
  1741. {
  1742. #define COMM_CHAN_RST_OFFSET 0x1e
  1743. struct mlx4_priv *priv = mlx4_priv(dev);
  1744. u32 comm_rst;
  1745. u32 comm_caps;
  1746. comm_caps = swab32(readl((__iomem char *)priv->mfunc.comm +
  1747. MLX4_COMM_CHAN_CAPS));
  1748. comm_rst = (comm_caps & (u32)(1 << COMM_CHAN_RST_OFFSET));
  1749. if (comm_rst)
  1750. dev->caps.vf_caps |= MLX4_VF_CAP_FLAG_RESET;
  1751. }
  1752. static int mlx4_init_slave(struct mlx4_dev *dev)
  1753. {
  1754. struct mlx4_priv *priv = mlx4_priv(dev);
  1755. u64 dma = (u64) priv->mfunc.vhcr_dma;
  1756. int ret_from_reset = 0;
  1757. u32 slave_read;
  1758. u32 cmd_channel_ver;
  1759. if (atomic_read(&pf_loading)) {
  1760. mlx4_warn(dev, "PF is not ready - Deferring probe\n");
  1761. return -EPROBE_DEFER;
  1762. }
  1763. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1764. priv->cmd.max_cmds = 1;
  1765. if (mlx4_comm_check_offline(dev)) {
  1766. mlx4_err(dev, "PF is not responsive, skipping initialization\n");
  1767. goto err_offline;
  1768. }
  1769. mlx4_reset_vf_support(dev);
  1770. mlx4_warn(dev, "Sending reset\n");
  1771. ret_from_reset = mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0,
  1772. MLX4_COMM_CMD_NA_OP, MLX4_COMM_TIME);
  1773. /* if we are in the middle of flr the slave will try
  1774. * NUM_OF_RESET_RETRIES times before leaving.*/
  1775. if (ret_from_reset) {
  1776. if (MLX4_DELAY_RESET_SLAVE == ret_from_reset) {
  1777. mlx4_warn(dev, "slave is currently in the middle of FLR - Deferring probe\n");
  1778. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1779. return -EPROBE_DEFER;
  1780. } else
  1781. goto err;
  1782. }
  1783. /* check the driver version - the slave I/F revision
  1784. * must match the master's */
  1785. slave_read = swab32(readl(&priv->mfunc.comm->slave_read));
  1786. cmd_channel_ver = mlx4_comm_get_version();
  1787. if (MLX4_COMM_GET_IF_REV(cmd_channel_ver) !=
  1788. MLX4_COMM_GET_IF_REV(slave_read)) {
  1789. mlx4_err(dev, "slave driver version is not supported by the master\n");
  1790. goto err;
  1791. }
  1792. mlx4_warn(dev, "Sending vhcr0\n");
  1793. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR0, dma >> 48,
  1794. MLX4_COMM_CMD_NA_OP, MLX4_COMM_TIME))
  1795. goto err;
  1796. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR1, dma >> 32,
  1797. MLX4_COMM_CMD_NA_OP, MLX4_COMM_TIME))
  1798. goto err;
  1799. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR2, dma >> 16,
  1800. MLX4_COMM_CMD_NA_OP, MLX4_COMM_TIME))
  1801. goto err;
  1802. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_EN, dma,
  1803. MLX4_COMM_CMD_NA_OP, MLX4_COMM_TIME))
  1804. goto err;
  1805. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1806. return 0;
  1807. err:
  1808. mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, MLX4_COMM_CMD_NA_OP, 0);
  1809. err_offline:
  1810. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1811. return -EIO;
  1812. }
  1813. static void mlx4_parav_master_pf_caps(struct mlx4_dev *dev)
  1814. {
  1815. int i;
  1816. for (i = 1; i <= dev->caps.num_ports; i++) {
  1817. if (dev->caps.port_type[i] == MLX4_PORT_TYPE_ETH)
  1818. dev->caps.gid_table_len[i] =
  1819. mlx4_get_slave_num_gids(dev, 0, i);
  1820. else
  1821. dev->caps.gid_table_len[i] = 1;
  1822. dev->caps.pkey_table_len[i] =
  1823. dev->phys_caps.pkey_phys_table_len[i] - 1;
  1824. }
  1825. }
  1826. static int choose_log_fs_mgm_entry_size(int qp_per_entry)
  1827. {
  1828. int i = MLX4_MIN_MGM_LOG_ENTRY_SIZE;
  1829. for (i = MLX4_MIN_MGM_LOG_ENTRY_SIZE; i <= MLX4_MAX_MGM_LOG_ENTRY_SIZE;
  1830. i++) {
  1831. if (qp_per_entry <= 4 * ((1 << i) / 16 - 2))
  1832. break;
  1833. }
  1834. return (i <= MLX4_MAX_MGM_LOG_ENTRY_SIZE) ? i : -1;
  1835. }
  1836. static const char *dmfs_high_rate_steering_mode_str(int dmfs_high_steer_mode)
  1837. {
  1838. switch (dmfs_high_steer_mode) {
  1839. case MLX4_STEERING_DMFS_A0_DEFAULT:
  1840. return "default performance";
  1841. case MLX4_STEERING_DMFS_A0_DYNAMIC:
  1842. return "dynamic hybrid mode";
  1843. case MLX4_STEERING_DMFS_A0_STATIC:
  1844. return "performance optimized for limited rule configuration (static)";
  1845. case MLX4_STEERING_DMFS_A0_DISABLE:
  1846. return "disabled performance optimized steering";
  1847. case MLX4_STEERING_DMFS_A0_NOT_SUPPORTED:
  1848. return "performance optimized steering not supported";
  1849. default:
  1850. return "Unrecognized mode";
  1851. }
  1852. }
  1853. #define MLX4_DMFS_A0_STEERING (1UL << 2)
  1854. static void choose_steering_mode(struct mlx4_dev *dev,
  1855. struct mlx4_dev_cap *dev_cap)
  1856. {
  1857. if (mlx4_log_num_mgm_entry_size <= 0) {
  1858. if ((-mlx4_log_num_mgm_entry_size) & MLX4_DMFS_A0_STEERING) {
  1859. if (dev->caps.dmfs_high_steer_mode ==
  1860. MLX4_STEERING_DMFS_A0_NOT_SUPPORTED)
  1861. mlx4_err(dev, "DMFS high rate mode not supported\n");
  1862. else
  1863. dev->caps.dmfs_high_steer_mode =
  1864. MLX4_STEERING_DMFS_A0_STATIC;
  1865. }
  1866. }
  1867. if (mlx4_log_num_mgm_entry_size <= 0 &&
  1868. dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_FS_EN &&
  1869. (!mlx4_is_mfunc(dev) ||
  1870. (dev_cap->fs_max_num_qp_per_entry >=
  1871. (dev->persist->num_vfs + 1))) &&
  1872. choose_log_fs_mgm_entry_size(dev_cap->fs_max_num_qp_per_entry) >=
  1873. MLX4_MIN_MGM_LOG_ENTRY_SIZE) {
  1874. dev->oper_log_mgm_entry_size =
  1875. choose_log_fs_mgm_entry_size(dev_cap->fs_max_num_qp_per_entry);
  1876. dev->caps.steering_mode = MLX4_STEERING_MODE_DEVICE_MANAGED;
  1877. dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
  1878. dev->caps.fs_log_max_ucast_qp_range_size =
  1879. dev_cap->fs_log_max_ucast_qp_range_size;
  1880. } else {
  1881. if (dev->caps.dmfs_high_steer_mode !=
  1882. MLX4_STEERING_DMFS_A0_NOT_SUPPORTED)
  1883. dev->caps.dmfs_high_steer_mode = MLX4_STEERING_DMFS_A0_DISABLE;
  1884. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER &&
  1885. dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
  1886. dev->caps.steering_mode = MLX4_STEERING_MODE_B0;
  1887. else {
  1888. dev->caps.steering_mode = MLX4_STEERING_MODE_A0;
  1889. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER ||
  1890. dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
  1891. mlx4_warn(dev, "Must have both UC_STEER and MC_STEER flags set to use B0 steering - falling back to A0 steering mode\n");
  1892. }
  1893. dev->oper_log_mgm_entry_size =
  1894. mlx4_log_num_mgm_entry_size > 0 ?
  1895. mlx4_log_num_mgm_entry_size :
  1896. MLX4_DEFAULT_MGM_LOG_ENTRY_SIZE;
  1897. dev->caps.num_qp_per_mgm = mlx4_get_qp_per_mgm(dev);
  1898. }
  1899. mlx4_dbg(dev, "Steering mode is: %s, oper_log_mgm_entry_size = %d, modparam log_num_mgm_entry_size = %d\n",
  1900. mlx4_steering_mode_str(dev->caps.steering_mode),
  1901. dev->oper_log_mgm_entry_size,
  1902. mlx4_log_num_mgm_entry_size);
  1903. }
  1904. static void choose_tunnel_offload_mode(struct mlx4_dev *dev,
  1905. struct mlx4_dev_cap *dev_cap)
  1906. {
  1907. if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED &&
  1908. dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_VXLAN_OFFLOADS)
  1909. dev->caps.tunnel_offload_mode = MLX4_TUNNEL_OFFLOAD_MODE_VXLAN;
  1910. else
  1911. dev->caps.tunnel_offload_mode = MLX4_TUNNEL_OFFLOAD_MODE_NONE;
  1912. mlx4_dbg(dev, "Tunneling offload mode is: %s\n", (dev->caps.tunnel_offload_mode
  1913. == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) ? "vxlan" : "none");
  1914. }
  1915. static int mlx4_validate_optimized_steering(struct mlx4_dev *dev)
  1916. {
  1917. int i;
  1918. struct mlx4_port_cap port_cap;
  1919. if (dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_NOT_SUPPORTED)
  1920. return -EINVAL;
  1921. for (i = 1; i <= dev->caps.num_ports; i++) {
  1922. if (mlx4_dev_port(dev, i, &port_cap)) {
  1923. mlx4_err(dev,
  1924. "QUERY_DEV_CAP command failed, can't verify DMFS high rate steering.\n");
  1925. } else if ((dev->caps.dmfs_high_steer_mode !=
  1926. MLX4_STEERING_DMFS_A0_DEFAULT) &&
  1927. (port_cap.dmfs_optimized_state ==
  1928. !!(dev->caps.dmfs_high_steer_mode ==
  1929. MLX4_STEERING_DMFS_A0_DISABLE))) {
  1930. mlx4_err(dev,
  1931. "DMFS high rate steer mode differ, driver requested %s but %s in FW.\n",
  1932. dmfs_high_rate_steering_mode_str(
  1933. dev->caps.dmfs_high_steer_mode),
  1934. (port_cap.dmfs_optimized_state ?
  1935. "enabled" : "disabled"));
  1936. }
  1937. }
  1938. return 0;
  1939. }
  1940. static int mlx4_init_fw(struct mlx4_dev *dev)
  1941. {
  1942. struct mlx4_mod_stat_cfg mlx4_cfg;
  1943. int err = 0;
  1944. if (!mlx4_is_slave(dev)) {
  1945. err = mlx4_QUERY_FW(dev);
  1946. if (err) {
  1947. if (err == -EACCES)
  1948. mlx4_info(dev, "non-primary physical function, skipping\n");
  1949. else
  1950. mlx4_err(dev, "QUERY_FW command failed, aborting\n");
  1951. return err;
  1952. }
  1953. err = mlx4_load_fw(dev);
  1954. if (err) {
  1955. mlx4_err(dev, "Failed to start FW, aborting\n");
  1956. return err;
  1957. }
  1958. mlx4_cfg.log_pg_sz_m = 1;
  1959. mlx4_cfg.log_pg_sz = 0;
  1960. err = mlx4_MOD_STAT_CFG(dev, &mlx4_cfg);
  1961. if (err)
  1962. mlx4_warn(dev, "Failed to override log_pg_sz parameter\n");
  1963. }
  1964. return err;
  1965. }
  1966. static int mlx4_init_hca(struct mlx4_dev *dev)
  1967. {
  1968. struct mlx4_priv *priv = mlx4_priv(dev);
  1969. struct mlx4_init_hca_param *init_hca = NULL;
  1970. struct mlx4_dev_cap *dev_cap = NULL;
  1971. struct mlx4_adapter adapter;
  1972. struct mlx4_profile profile;
  1973. u64 icm_size;
  1974. struct mlx4_config_dev_params params;
  1975. int err;
  1976. if (!mlx4_is_slave(dev)) {
  1977. dev_cap = kzalloc(sizeof(*dev_cap), GFP_KERNEL);
  1978. init_hca = kzalloc(sizeof(*init_hca), GFP_KERNEL);
  1979. if (!dev_cap || !init_hca) {
  1980. err = -ENOMEM;
  1981. goto out_free;
  1982. }
  1983. err = mlx4_dev_cap(dev, dev_cap);
  1984. if (err) {
  1985. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting\n");
  1986. goto out_free;
  1987. }
  1988. choose_steering_mode(dev, dev_cap);
  1989. choose_tunnel_offload_mode(dev, dev_cap);
  1990. if (dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC &&
  1991. mlx4_is_master(dev))
  1992. dev->caps.function_caps |= MLX4_FUNC_CAP_DMFS_A0_STATIC;
  1993. err = mlx4_get_phys_port_id(dev);
  1994. if (err)
  1995. mlx4_err(dev, "Fail to get physical port id\n");
  1996. if (mlx4_is_master(dev))
  1997. mlx4_parav_master_pf_caps(dev);
  1998. if (mlx4_low_memory_profile()) {
  1999. mlx4_info(dev, "Running from within kdump kernel. Using low memory profile\n");
  2000. profile = low_mem_profile;
  2001. } else {
  2002. profile = default_profile;
  2003. }
  2004. if (dev->caps.steering_mode ==
  2005. MLX4_STEERING_MODE_DEVICE_MANAGED)
  2006. profile.num_mcg = MLX4_FS_NUM_MCG;
  2007. icm_size = mlx4_make_profile(dev, &profile, dev_cap,
  2008. init_hca);
  2009. if ((long long) icm_size < 0) {
  2010. err = icm_size;
  2011. goto out_free;
  2012. }
  2013. if (enable_4k_uar || !dev->persist->num_vfs) {
  2014. init_hca->log_uar_sz = ilog2(dev->caps.num_uars) +
  2015. PAGE_SHIFT - DEFAULT_UAR_PAGE_SHIFT;
  2016. init_hca->uar_page_sz = DEFAULT_UAR_PAGE_SHIFT - 12;
  2017. } else {
  2018. init_hca->log_uar_sz = ilog2(dev->caps.num_uars);
  2019. init_hca->uar_page_sz = PAGE_SHIFT - 12;
  2020. }
  2021. init_hca->mw_enabled = 0;
  2022. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW ||
  2023. dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN)
  2024. init_hca->mw_enabled = INIT_HCA_TPT_MW_ENABLE;
  2025. err = mlx4_init_icm(dev, dev_cap, init_hca, icm_size);
  2026. if (err)
  2027. goto out_free;
  2028. err = mlx4_INIT_HCA(dev, init_hca);
  2029. if (err) {
  2030. mlx4_err(dev, "INIT_HCA command failed, aborting\n");
  2031. goto err_free_icm;
  2032. }
  2033. if (dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS) {
  2034. err = mlx4_query_func(dev, dev_cap);
  2035. if (err < 0) {
  2036. mlx4_err(dev, "QUERY_FUNC command failed, aborting.\n");
  2037. goto err_close;
  2038. } else if (err & MLX4_QUERY_FUNC_NUM_SYS_EQS) {
  2039. dev->caps.num_eqs = dev_cap->max_eqs;
  2040. dev->caps.reserved_eqs = dev_cap->reserved_eqs;
  2041. dev->caps.reserved_uars = dev_cap->reserved_uars;
  2042. }
  2043. }
  2044. /*
  2045. * If TS is supported by FW
  2046. * read HCA frequency by QUERY_HCA command
  2047. */
  2048. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS) {
  2049. err = mlx4_QUERY_HCA(dev, init_hca);
  2050. if (err) {
  2051. mlx4_err(dev, "QUERY_HCA command failed, disable timestamp\n");
  2052. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
  2053. } else {
  2054. dev->caps.hca_core_clock =
  2055. init_hca->hca_core_clock;
  2056. }
  2057. /* In case we got HCA frequency 0 - disable timestamping
  2058. * to avoid dividing by zero
  2059. */
  2060. if (!dev->caps.hca_core_clock) {
  2061. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
  2062. mlx4_err(dev,
  2063. "HCA frequency is 0 - timestamping is not supported\n");
  2064. } else if (map_internal_clock(dev)) {
  2065. /*
  2066. * Map internal clock,
  2067. * in case of failure disable timestamping
  2068. */
  2069. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
  2070. mlx4_err(dev, "Failed to map internal clock. Timestamping is not supported\n");
  2071. }
  2072. }
  2073. if (dev->caps.dmfs_high_steer_mode !=
  2074. MLX4_STEERING_DMFS_A0_NOT_SUPPORTED) {
  2075. if (mlx4_validate_optimized_steering(dev))
  2076. mlx4_warn(dev, "Optimized steering validation failed\n");
  2077. if (dev->caps.dmfs_high_steer_mode ==
  2078. MLX4_STEERING_DMFS_A0_DISABLE) {
  2079. dev->caps.dmfs_high_rate_qpn_base =
  2080. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
  2081. dev->caps.dmfs_high_rate_qpn_range =
  2082. MLX4_A0_STEERING_TABLE_SIZE;
  2083. }
  2084. mlx4_info(dev, "DMFS high rate steer mode is: %s\n",
  2085. dmfs_high_rate_steering_mode_str(
  2086. dev->caps.dmfs_high_steer_mode));
  2087. }
  2088. } else {
  2089. err = mlx4_init_slave(dev);
  2090. if (err) {
  2091. if (err != -EPROBE_DEFER)
  2092. mlx4_err(dev, "Failed to initialize slave\n");
  2093. return err;
  2094. }
  2095. err = mlx4_slave_cap(dev);
  2096. if (err) {
  2097. mlx4_err(dev, "Failed to obtain slave caps\n");
  2098. goto err_close;
  2099. }
  2100. }
  2101. if (map_bf_area(dev))
  2102. mlx4_dbg(dev, "Failed to map blue flame area\n");
  2103. /*Only the master set the ports, all the rest got it from it.*/
  2104. if (!mlx4_is_slave(dev))
  2105. mlx4_set_port_mask(dev);
  2106. err = mlx4_QUERY_ADAPTER(dev, &adapter);
  2107. if (err) {
  2108. mlx4_err(dev, "QUERY_ADAPTER command failed, aborting\n");
  2109. goto unmap_bf;
  2110. }
  2111. /* Query CONFIG_DEV parameters */
  2112. err = mlx4_config_dev_retrieval(dev, &params);
  2113. if (err && err != -EOPNOTSUPP) {
  2114. mlx4_err(dev, "Failed to query CONFIG_DEV parameters\n");
  2115. } else if (!err) {
  2116. dev->caps.rx_checksum_flags_port[1] = params.rx_csum_flags_port_1;
  2117. dev->caps.rx_checksum_flags_port[2] = params.rx_csum_flags_port_2;
  2118. }
  2119. priv->eq_table.inta_pin = adapter.inta_pin;
  2120. memcpy(dev->board_id, adapter.board_id, sizeof(dev->board_id));
  2121. err = 0;
  2122. goto out_free;
  2123. unmap_bf:
  2124. unmap_internal_clock(dev);
  2125. unmap_bf_area(dev);
  2126. if (mlx4_is_slave(dev))
  2127. mlx4_slave_destroy_special_qp_cap(dev);
  2128. err_close:
  2129. if (mlx4_is_slave(dev))
  2130. mlx4_slave_exit(dev);
  2131. else
  2132. mlx4_CLOSE_HCA(dev, 0);
  2133. err_free_icm:
  2134. if (!mlx4_is_slave(dev))
  2135. mlx4_free_icms(dev);
  2136. out_free:
  2137. kfree(dev_cap);
  2138. kfree(init_hca);
  2139. return err;
  2140. }
  2141. static int mlx4_init_counters_table(struct mlx4_dev *dev)
  2142. {
  2143. struct mlx4_priv *priv = mlx4_priv(dev);
  2144. int nent_pow2;
  2145. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
  2146. return -ENOENT;
  2147. if (!dev->caps.max_counters)
  2148. return -ENOSPC;
  2149. nent_pow2 = roundup_pow_of_two(dev->caps.max_counters);
  2150. /* reserve last counter index for sink counter */
  2151. return mlx4_bitmap_init(&priv->counters_bitmap, nent_pow2,
  2152. nent_pow2 - 1, 0,
  2153. nent_pow2 - dev->caps.max_counters + 1);
  2154. }
  2155. static void mlx4_cleanup_counters_table(struct mlx4_dev *dev)
  2156. {
  2157. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
  2158. return;
  2159. if (!dev->caps.max_counters)
  2160. return;
  2161. mlx4_bitmap_cleanup(&mlx4_priv(dev)->counters_bitmap);
  2162. }
  2163. static void mlx4_cleanup_default_counters(struct mlx4_dev *dev)
  2164. {
  2165. struct mlx4_priv *priv = mlx4_priv(dev);
  2166. int port;
  2167. for (port = 0; port < dev->caps.num_ports; port++)
  2168. if (priv->def_counter[port] != -1)
  2169. mlx4_counter_free(dev, priv->def_counter[port]);
  2170. }
  2171. static int mlx4_allocate_default_counters(struct mlx4_dev *dev)
  2172. {
  2173. struct mlx4_priv *priv = mlx4_priv(dev);
  2174. int port, err = 0;
  2175. u32 idx;
  2176. for (port = 0; port < dev->caps.num_ports; port++)
  2177. priv->def_counter[port] = -1;
  2178. for (port = 0; port < dev->caps.num_ports; port++) {
  2179. err = mlx4_counter_alloc(dev, &idx, MLX4_RES_USAGE_DRIVER);
  2180. if (!err || err == -ENOSPC) {
  2181. priv->def_counter[port] = idx;
  2182. err = 0;
  2183. } else if (err == -ENOENT) {
  2184. err = 0;
  2185. continue;
  2186. } else if (mlx4_is_slave(dev) && err == -EINVAL) {
  2187. priv->def_counter[port] = MLX4_SINK_COUNTER_INDEX(dev);
  2188. mlx4_warn(dev, "can't allocate counter from old PF driver, using index %d\n",
  2189. MLX4_SINK_COUNTER_INDEX(dev));
  2190. err = 0;
  2191. } else {
  2192. mlx4_err(dev, "%s: failed to allocate default counter port %d err %d\n",
  2193. __func__, port + 1, err);
  2194. mlx4_cleanup_default_counters(dev);
  2195. return err;
  2196. }
  2197. mlx4_dbg(dev, "%s: default counter index %d for port %d\n",
  2198. __func__, priv->def_counter[port], port + 1);
  2199. }
  2200. return err;
  2201. }
  2202. int __mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx)
  2203. {
  2204. struct mlx4_priv *priv = mlx4_priv(dev);
  2205. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
  2206. return -ENOENT;
  2207. *idx = mlx4_bitmap_alloc(&priv->counters_bitmap);
  2208. if (*idx == -1) {
  2209. *idx = MLX4_SINK_COUNTER_INDEX(dev);
  2210. return -ENOSPC;
  2211. }
  2212. return 0;
  2213. }
  2214. int mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx, u8 usage)
  2215. {
  2216. u32 in_modifier = RES_COUNTER | (((u32)usage & 3) << 30);
  2217. u64 out_param;
  2218. int err;
  2219. if (mlx4_is_mfunc(dev)) {
  2220. err = mlx4_cmd_imm(dev, 0, &out_param, in_modifier,
  2221. RES_OP_RESERVE, MLX4_CMD_ALLOC_RES,
  2222. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  2223. if (!err)
  2224. *idx = get_param_l(&out_param);
  2225. if (WARN_ON(err == -ENOSPC))
  2226. err = -EINVAL;
  2227. return err;
  2228. }
  2229. return __mlx4_counter_alloc(dev, idx);
  2230. }
  2231. EXPORT_SYMBOL_GPL(mlx4_counter_alloc);
  2232. static int __mlx4_clear_if_stat(struct mlx4_dev *dev,
  2233. u8 counter_index)
  2234. {
  2235. struct mlx4_cmd_mailbox *if_stat_mailbox;
  2236. int err;
  2237. u32 if_stat_in_mod = (counter_index & 0xff) | MLX4_QUERY_IF_STAT_RESET;
  2238. if_stat_mailbox = mlx4_alloc_cmd_mailbox(dev);
  2239. if (IS_ERR(if_stat_mailbox))
  2240. return PTR_ERR(if_stat_mailbox);
  2241. err = mlx4_cmd_box(dev, 0, if_stat_mailbox->dma, if_stat_in_mod, 0,
  2242. MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
  2243. MLX4_CMD_NATIVE);
  2244. mlx4_free_cmd_mailbox(dev, if_stat_mailbox);
  2245. return err;
  2246. }
  2247. void __mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
  2248. {
  2249. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
  2250. return;
  2251. if (idx == MLX4_SINK_COUNTER_INDEX(dev))
  2252. return;
  2253. __mlx4_clear_if_stat(dev, idx);
  2254. mlx4_bitmap_free(&mlx4_priv(dev)->counters_bitmap, idx, MLX4_USE_RR);
  2255. return;
  2256. }
  2257. void mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
  2258. {
  2259. u64 in_param = 0;
  2260. if (mlx4_is_mfunc(dev)) {
  2261. set_param_l(&in_param, idx);
  2262. mlx4_cmd(dev, in_param, RES_COUNTER, RES_OP_RESERVE,
  2263. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  2264. MLX4_CMD_WRAPPED);
  2265. return;
  2266. }
  2267. __mlx4_counter_free(dev, idx);
  2268. }
  2269. EXPORT_SYMBOL_GPL(mlx4_counter_free);
  2270. int mlx4_get_default_counter_index(struct mlx4_dev *dev, int port)
  2271. {
  2272. struct mlx4_priv *priv = mlx4_priv(dev);
  2273. return priv->def_counter[port - 1];
  2274. }
  2275. EXPORT_SYMBOL_GPL(mlx4_get_default_counter_index);
  2276. void mlx4_set_admin_guid(struct mlx4_dev *dev, __be64 guid, int entry, int port)
  2277. {
  2278. struct mlx4_priv *priv = mlx4_priv(dev);
  2279. priv->mfunc.master.vf_admin[entry].vport[port].guid = guid;
  2280. }
  2281. EXPORT_SYMBOL_GPL(mlx4_set_admin_guid);
  2282. __be64 mlx4_get_admin_guid(struct mlx4_dev *dev, int entry, int port)
  2283. {
  2284. struct mlx4_priv *priv = mlx4_priv(dev);
  2285. return priv->mfunc.master.vf_admin[entry].vport[port].guid;
  2286. }
  2287. EXPORT_SYMBOL_GPL(mlx4_get_admin_guid);
  2288. void mlx4_set_random_admin_guid(struct mlx4_dev *dev, int entry, int port)
  2289. {
  2290. struct mlx4_priv *priv = mlx4_priv(dev);
  2291. __be64 guid;
  2292. /* hw GUID */
  2293. if (entry == 0)
  2294. return;
  2295. get_random_bytes((char *)&guid, sizeof(guid));
  2296. guid &= ~(cpu_to_be64(1ULL << 56));
  2297. guid |= cpu_to_be64(1ULL << 57);
  2298. priv->mfunc.master.vf_admin[entry].vport[port].guid = guid;
  2299. }
  2300. static int mlx4_setup_hca(struct mlx4_dev *dev)
  2301. {
  2302. struct mlx4_priv *priv = mlx4_priv(dev);
  2303. int err;
  2304. int port;
  2305. __be32 ib_port_default_caps;
  2306. err = mlx4_init_uar_table(dev);
  2307. if (err) {
  2308. mlx4_err(dev, "Failed to initialize user access region table, aborting\n");
  2309. return err;
  2310. }
  2311. err = mlx4_uar_alloc(dev, &priv->driver_uar);
  2312. if (err) {
  2313. mlx4_err(dev, "Failed to allocate driver access region, aborting\n");
  2314. goto err_uar_table_free;
  2315. }
  2316. priv->kar = ioremap((phys_addr_t) priv->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
  2317. if (!priv->kar) {
  2318. mlx4_err(dev, "Couldn't map kernel access region, aborting\n");
  2319. err = -ENOMEM;
  2320. goto err_uar_free;
  2321. }
  2322. err = mlx4_init_pd_table(dev);
  2323. if (err) {
  2324. mlx4_err(dev, "Failed to initialize protection domain table, aborting\n");
  2325. goto err_kar_unmap;
  2326. }
  2327. err = mlx4_init_xrcd_table(dev);
  2328. if (err) {
  2329. mlx4_err(dev, "Failed to initialize reliable connection domain table, aborting\n");
  2330. goto err_pd_table_free;
  2331. }
  2332. err = mlx4_init_mr_table(dev);
  2333. if (err) {
  2334. mlx4_err(dev, "Failed to initialize memory region table, aborting\n");
  2335. goto err_xrcd_table_free;
  2336. }
  2337. if (!mlx4_is_slave(dev)) {
  2338. err = mlx4_init_mcg_table(dev);
  2339. if (err) {
  2340. mlx4_err(dev, "Failed to initialize multicast group table, aborting\n");
  2341. goto err_mr_table_free;
  2342. }
  2343. err = mlx4_config_mad_demux(dev);
  2344. if (err) {
  2345. mlx4_err(dev, "Failed in config_mad_demux, aborting\n");
  2346. goto err_mcg_table_free;
  2347. }
  2348. }
  2349. err = mlx4_init_eq_table(dev);
  2350. if (err) {
  2351. mlx4_err(dev, "Failed to initialize event queue table, aborting\n");
  2352. goto err_mcg_table_free;
  2353. }
  2354. err = mlx4_cmd_use_events(dev);
  2355. if (err) {
  2356. mlx4_err(dev, "Failed to switch to event-driven firmware commands, aborting\n");
  2357. goto err_eq_table_free;
  2358. }
  2359. err = mlx4_NOP(dev);
  2360. if (err) {
  2361. if (dev->flags & MLX4_FLAG_MSI_X) {
  2362. mlx4_warn(dev, "NOP command failed to generate MSI-X interrupt IRQ %d)\n",
  2363. priv->eq_table.eq[MLX4_EQ_ASYNC].irq);
  2364. mlx4_warn(dev, "Trying again without MSI-X\n");
  2365. } else {
  2366. mlx4_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting\n",
  2367. priv->eq_table.eq[MLX4_EQ_ASYNC].irq);
  2368. mlx4_err(dev, "BIOS or ACPI interrupt routing problem?\n");
  2369. }
  2370. goto err_cmd_poll;
  2371. }
  2372. mlx4_dbg(dev, "NOP command IRQ test passed\n");
  2373. err = mlx4_init_cq_table(dev);
  2374. if (err) {
  2375. mlx4_err(dev, "Failed to initialize completion queue table, aborting\n");
  2376. goto err_cmd_poll;
  2377. }
  2378. err = mlx4_init_srq_table(dev);
  2379. if (err) {
  2380. mlx4_err(dev, "Failed to initialize shared receive queue table, aborting\n");
  2381. goto err_cq_table_free;
  2382. }
  2383. err = mlx4_init_qp_table(dev);
  2384. if (err) {
  2385. mlx4_err(dev, "Failed to initialize queue pair table, aborting\n");
  2386. goto err_srq_table_free;
  2387. }
  2388. if (!mlx4_is_slave(dev)) {
  2389. err = mlx4_init_counters_table(dev);
  2390. if (err && err != -ENOENT) {
  2391. mlx4_err(dev, "Failed to initialize counters table, aborting\n");
  2392. goto err_qp_table_free;
  2393. }
  2394. }
  2395. err = mlx4_allocate_default_counters(dev);
  2396. if (err) {
  2397. mlx4_err(dev, "Failed to allocate default counters, aborting\n");
  2398. goto err_counters_table_free;
  2399. }
  2400. if (!mlx4_is_slave(dev)) {
  2401. for (port = 1; port <= dev->caps.num_ports; port++) {
  2402. ib_port_default_caps = 0;
  2403. err = mlx4_get_port_ib_caps(dev, port,
  2404. &ib_port_default_caps);
  2405. if (err)
  2406. mlx4_warn(dev, "failed to get port %d default ib capabilities (%d). Continuing with caps = 0\n",
  2407. port, err);
  2408. dev->caps.ib_port_def_cap[port] = ib_port_default_caps;
  2409. /* initialize per-slave default ib port capabilities */
  2410. if (mlx4_is_master(dev)) {
  2411. int i;
  2412. for (i = 0; i < dev->num_slaves; i++) {
  2413. if (i == mlx4_master_func_num(dev))
  2414. continue;
  2415. priv->mfunc.master.slave_state[i].ib_cap_mask[port] =
  2416. ib_port_default_caps;
  2417. }
  2418. }
  2419. if (mlx4_is_mfunc(dev))
  2420. dev->caps.port_ib_mtu[port] = IB_MTU_2048;
  2421. else
  2422. dev->caps.port_ib_mtu[port] = IB_MTU_4096;
  2423. err = mlx4_SET_PORT(dev, port, mlx4_is_master(dev) ?
  2424. dev->caps.pkey_table_len[port] : -1);
  2425. if (err) {
  2426. mlx4_err(dev, "Failed to set port %d, aborting\n",
  2427. port);
  2428. goto err_default_countes_free;
  2429. }
  2430. }
  2431. }
  2432. return 0;
  2433. err_default_countes_free:
  2434. mlx4_cleanup_default_counters(dev);
  2435. err_counters_table_free:
  2436. if (!mlx4_is_slave(dev))
  2437. mlx4_cleanup_counters_table(dev);
  2438. err_qp_table_free:
  2439. mlx4_cleanup_qp_table(dev);
  2440. err_srq_table_free:
  2441. mlx4_cleanup_srq_table(dev);
  2442. err_cq_table_free:
  2443. mlx4_cleanup_cq_table(dev);
  2444. err_cmd_poll:
  2445. mlx4_cmd_use_polling(dev);
  2446. err_eq_table_free:
  2447. mlx4_cleanup_eq_table(dev);
  2448. err_mcg_table_free:
  2449. if (!mlx4_is_slave(dev))
  2450. mlx4_cleanup_mcg_table(dev);
  2451. err_mr_table_free:
  2452. mlx4_cleanup_mr_table(dev);
  2453. err_xrcd_table_free:
  2454. mlx4_cleanup_xrcd_table(dev);
  2455. err_pd_table_free:
  2456. mlx4_cleanup_pd_table(dev);
  2457. err_kar_unmap:
  2458. iounmap(priv->kar);
  2459. err_uar_free:
  2460. mlx4_uar_free(dev, &priv->driver_uar);
  2461. err_uar_table_free:
  2462. mlx4_cleanup_uar_table(dev);
  2463. return err;
  2464. }
  2465. static int mlx4_init_affinity_hint(struct mlx4_dev *dev, int port, int eqn)
  2466. {
  2467. int requested_cpu = 0;
  2468. struct mlx4_priv *priv = mlx4_priv(dev);
  2469. struct mlx4_eq *eq;
  2470. int off = 0;
  2471. int i;
  2472. if (eqn > dev->caps.num_comp_vectors)
  2473. return -EINVAL;
  2474. for (i = 1; i < port; i++)
  2475. off += mlx4_get_eqs_per_port(dev, i);
  2476. requested_cpu = eqn - off - !!(eqn > MLX4_EQ_ASYNC);
  2477. /* Meaning EQs are shared, and this call comes from the second port */
  2478. if (requested_cpu < 0)
  2479. return 0;
  2480. eq = &priv->eq_table.eq[eqn];
  2481. if (!zalloc_cpumask_var(&eq->affinity_mask, GFP_KERNEL))
  2482. return -ENOMEM;
  2483. cpumask_set_cpu(requested_cpu, eq->affinity_mask);
  2484. return 0;
  2485. }
  2486. static void mlx4_enable_msi_x(struct mlx4_dev *dev)
  2487. {
  2488. struct mlx4_priv *priv = mlx4_priv(dev);
  2489. struct msix_entry *entries;
  2490. int i;
  2491. int port = 0;
  2492. if (msi_x) {
  2493. int nreq = min3(dev->caps.num_ports *
  2494. (int)num_online_cpus() + 1,
  2495. dev->caps.num_eqs - dev->caps.reserved_eqs,
  2496. MAX_MSIX);
  2497. if (msi_x > 1)
  2498. nreq = min_t(int, nreq, msi_x);
  2499. entries = kcalloc(nreq, sizeof(*entries), GFP_KERNEL);
  2500. if (!entries)
  2501. goto no_msi;
  2502. for (i = 0; i < nreq; ++i)
  2503. entries[i].entry = i;
  2504. nreq = pci_enable_msix_range(dev->persist->pdev, entries, 2,
  2505. nreq);
  2506. if (nreq < 0 || nreq < MLX4_EQ_ASYNC) {
  2507. kfree(entries);
  2508. goto no_msi;
  2509. }
  2510. /* 1 is reserved for events (asyncrounous EQ) */
  2511. dev->caps.num_comp_vectors = nreq - 1;
  2512. priv->eq_table.eq[MLX4_EQ_ASYNC].irq = entries[0].vector;
  2513. bitmap_zero(priv->eq_table.eq[MLX4_EQ_ASYNC].actv_ports.ports,
  2514. dev->caps.num_ports);
  2515. for (i = 0; i < dev->caps.num_comp_vectors + 1; i++) {
  2516. if (i == MLX4_EQ_ASYNC)
  2517. continue;
  2518. priv->eq_table.eq[i].irq =
  2519. entries[i + 1 - !!(i > MLX4_EQ_ASYNC)].vector;
  2520. if (MLX4_IS_LEGACY_EQ_MODE(dev->caps)) {
  2521. bitmap_fill(priv->eq_table.eq[i].actv_ports.ports,
  2522. dev->caps.num_ports);
  2523. /* We don't set affinity hint when there
  2524. * aren't enough EQs
  2525. */
  2526. } else {
  2527. set_bit(port,
  2528. priv->eq_table.eq[i].actv_ports.ports);
  2529. if (mlx4_init_affinity_hint(dev, port + 1, i))
  2530. mlx4_warn(dev, "Couldn't init hint cpumask for EQ %d\n",
  2531. i);
  2532. }
  2533. /* We divide the Eqs evenly between the two ports.
  2534. * (dev->caps.num_comp_vectors / dev->caps.num_ports)
  2535. * refers to the number of Eqs per port
  2536. * (i.e eqs_per_port). Theoretically, we would like to
  2537. * write something like (i + 1) % eqs_per_port == 0.
  2538. * However, since there's an asynchronous Eq, we have
  2539. * to skip over it by comparing this condition to
  2540. * !!((i + 1) > MLX4_EQ_ASYNC).
  2541. */
  2542. if ((dev->caps.num_comp_vectors > dev->caps.num_ports) &&
  2543. ((i + 1) %
  2544. (dev->caps.num_comp_vectors / dev->caps.num_ports)) ==
  2545. !!((i + 1) > MLX4_EQ_ASYNC))
  2546. /* If dev->caps.num_comp_vectors < dev->caps.num_ports,
  2547. * everything is shared anyway.
  2548. */
  2549. port++;
  2550. }
  2551. dev->flags |= MLX4_FLAG_MSI_X;
  2552. kfree(entries);
  2553. return;
  2554. }
  2555. no_msi:
  2556. dev->caps.num_comp_vectors = 1;
  2557. BUG_ON(MLX4_EQ_ASYNC >= 2);
  2558. for (i = 0; i < 2; ++i) {
  2559. priv->eq_table.eq[i].irq = dev->persist->pdev->irq;
  2560. if (i != MLX4_EQ_ASYNC) {
  2561. bitmap_fill(priv->eq_table.eq[i].actv_ports.ports,
  2562. dev->caps.num_ports);
  2563. }
  2564. }
  2565. }
  2566. static int mlx4_devlink_port_type_set(struct devlink_port *devlink_port,
  2567. enum devlink_port_type port_type)
  2568. {
  2569. struct mlx4_port_info *info = container_of(devlink_port,
  2570. struct mlx4_port_info,
  2571. devlink_port);
  2572. enum mlx4_port_type mlx4_port_type;
  2573. switch (port_type) {
  2574. case DEVLINK_PORT_TYPE_AUTO:
  2575. mlx4_port_type = MLX4_PORT_TYPE_AUTO;
  2576. break;
  2577. case DEVLINK_PORT_TYPE_ETH:
  2578. mlx4_port_type = MLX4_PORT_TYPE_ETH;
  2579. break;
  2580. case DEVLINK_PORT_TYPE_IB:
  2581. mlx4_port_type = MLX4_PORT_TYPE_IB;
  2582. break;
  2583. default:
  2584. return -EOPNOTSUPP;
  2585. }
  2586. return __set_port_type(info, mlx4_port_type);
  2587. }
  2588. static const struct devlink_port_ops mlx4_devlink_port_ops = {
  2589. .port_type_set = mlx4_devlink_port_type_set,
  2590. };
  2591. static int mlx4_init_port_info(struct mlx4_dev *dev, int port)
  2592. {
  2593. struct devlink *devlink = priv_to_devlink(mlx4_priv(dev));
  2594. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  2595. int err;
  2596. err = devl_port_register_with_ops(devlink, &info->devlink_port, port,
  2597. &mlx4_devlink_port_ops);
  2598. if (err)
  2599. return err;
  2600. /* Ethernet and IB drivers will normally set the port type,
  2601. * but if they are not built set the type now to prevent
  2602. * devlink_port_type_warn() from firing.
  2603. */
  2604. if (!IS_ENABLED(CONFIG_MLX4_EN) &&
  2605. dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  2606. devlink_port_type_eth_set(&info->devlink_port);
  2607. else if (!IS_ENABLED(CONFIG_MLX4_INFINIBAND) &&
  2608. dev->caps.port_type[port] == MLX4_PORT_TYPE_IB)
  2609. devlink_port_type_ib_set(&info->devlink_port, NULL);
  2610. info->dev = dev;
  2611. info->port = port;
  2612. if (!mlx4_is_slave(dev)) {
  2613. mlx4_init_mac_table(dev, &info->mac_table);
  2614. mlx4_init_vlan_table(dev, &info->vlan_table);
  2615. mlx4_init_roce_gid_table(dev, &info->gid_table);
  2616. info->base_qpn = mlx4_get_base_qpn(dev, port);
  2617. }
  2618. sprintf(info->dev_name, "mlx4_port%d", port);
  2619. info->port_attr.attr.name = info->dev_name;
  2620. if (mlx4_is_mfunc(dev)) {
  2621. info->port_attr.attr.mode = 0444;
  2622. } else {
  2623. info->port_attr.attr.mode = 0644;
  2624. info->port_attr.store = set_port_type;
  2625. }
  2626. info->port_attr.show = show_port_type;
  2627. sysfs_attr_init(&info->port_attr.attr);
  2628. err = device_create_file(&dev->persist->pdev->dev, &info->port_attr);
  2629. if (err) {
  2630. mlx4_err(dev, "Failed to create file for port %d\n", port);
  2631. devlink_port_type_clear(&info->devlink_port);
  2632. devl_port_unregister(&info->devlink_port);
  2633. info->port = -1;
  2634. return err;
  2635. }
  2636. sprintf(info->dev_mtu_name, "mlx4_port%d_mtu", port);
  2637. info->port_mtu_attr.attr.name = info->dev_mtu_name;
  2638. if (mlx4_is_mfunc(dev)) {
  2639. info->port_mtu_attr.attr.mode = 0444;
  2640. } else {
  2641. info->port_mtu_attr.attr.mode = 0644;
  2642. info->port_mtu_attr.store = set_port_ib_mtu;
  2643. }
  2644. info->port_mtu_attr.show = show_port_ib_mtu;
  2645. sysfs_attr_init(&info->port_mtu_attr.attr);
  2646. err = device_create_file(&dev->persist->pdev->dev,
  2647. &info->port_mtu_attr);
  2648. if (err) {
  2649. mlx4_err(dev, "Failed to create mtu file for port %d\n", port);
  2650. device_remove_file(&info->dev->persist->pdev->dev,
  2651. &info->port_attr);
  2652. devlink_port_type_clear(&info->devlink_port);
  2653. devl_port_unregister(&info->devlink_port);
  2654. info->port = -1;
  2655. return err;
  2656. }
  2657. return 0;
  2658. }
  2659. static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
  2660. {
  2661. if (info->port < 0)
  2662. return;
  2663. device_remove_file(&info->dev->persist->pdev->dev, &info->port_attr);
  2664. device_remove_file(&info->dev->persist->pdev->dev,
  2665. &info->port_mtu_attr);
  2666. devlink_port_type_clear(&info->devlink_port);
  2667. devl_port_unregister(&info->devlink_port);
  2668. #ifdef CONFIG_RFS_ACCEL
  2669. free_irq_cpu_rmap(info->rmap);
  2670. info->rmap = NULL;
  2671. #endif
  2672. }
  2673. static int mlx4_init_steering(struct mlx4_dev *dev)
  2674. {
  2675. struct mlx4_priv *priv = mlx4_priv(dev);
  2676. int num_entries = dev->caps.num_ports;
  2677. int i, j;
  2678. priv->steer = kcalloc(num_entries, sizeof(struct mlx4_steer),
  2679. GFP_KERNEL);
  2680. if (!priv->steer)
  2681. return -ENOMEM;
  2682. for (i = 0; i < num_entries; i++)
  2683. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  2684. INIT_LIST_HEAD(&priv->steer[i].promisc_qps[j]);
  2685. INIT_LIST_HEAD(&priv->steer[i].steer_entries[j]);
  2686. }
  2687. return 0;
  2688. }
  2689. static void mlx4_clear_steering(struct mlx4_dev *dev)
  2690. {
  2691. struct mlx4_priv *priv = mlx4_priv(dev);
  2692. struct mlx4_steer_index *entry, *tmp_entry;
  2693. struct mlx4_promisc_qp *pqp, *tmp_pqp;
  2694. int num_entries = dev->caps.num_ports;
  2695. int i, j;
  2696. for (i = 0; i < num_entries; i++) {
  2697. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  2698. list_for_each_entry_safe(pqp, tmp_pqp,
  2699. &priv->steer[i].promisc_qps[j],
  2700. list) {
  2701. list_del(&pqp->list);
  2702. kfree(pqp);
  2703. }
  2704. list_for_each_entry_safe(entry, tmp_entry,
  2705. &priv->steer[i].steer_entries[j],
  2706. list) {
  2707. list_del(&entry->list);
  2708. list_for_each_entry_safe(pqp, tmp_pqp,
  2709. &entry->duplicates,
  2710. list) {
  2711. list_del(&pqp->list);
  2712. kfree(pqp);
  2713. }
  2714. kfree(entry);
  2715. }
  2716. }
  2717. }
  2718. kfree(priv->steer);
  2719. }
  2720. static int extended_func_num(struct pci_dev *pdev)
  2721. {
  2722. return PCI_SLOT(pdev->devfn) * 8 + PCI_FUNC(pdev->devfn);
  2723. }
  2724. #define MLX4_OWNER_BASE 0x8069c
  2725. #define MLX4_OWNER_SIZE 4
  2726. static int mlx4_get_ownership(struct mlx4_dev *dev)
  2727. {
  2728. void __iomem *owner;
  2729. u32 ret;
  2730. if (pci_channel_offline(dev->persist->pdev))
  2731. return -EIO;
  2732. owner = ioremap(pci_resource_start(dev->persist->pdev, 0) +
  2733. MLX4_OWNER_BASE,
  2734. MLX4_OWNER_SIZE);
  2735. if (!owner) {
  2736. mlx4_err(dev, "Failed to obtain ownership bit\n");
  2737. return -ENOMEM;
  2738. }
  2739. ret = readl(owner);
  2740. iounmap(owner);
  2741. return (int) !!ret;
  2742. }
  2743. static void mlx4_free_ownership(struct mlx4_dev *dev)
  2744. {
  2745. void __iomem *owner;
  2746. if (pci_channel_offline(dev->persist->pdev))
  2747. return;
  2748. owner = ioremap(pci_resource_start(dev->persist->pdev, 0) +
  2749. MLX4_OWNER_BASE,
  2750. MLX4_OWNER_SIZE);
  2751. if (!owner) {
  2752. mlx4_err(dev, "Failed to obtain ownership bit\n");
  2753. return;
  2754. }
  2755. writel(0, owner);
  2756. msleep(1000);
  2757. iounmap(owner);
  2758. }
  2759. #define SRIOV_VALID_STATE(flags) (!!((flags) & MLX4_FLAG_SRIOV) ==\
  2760. !!((flags) & MLX4_FLAG_MASTER))
  2761. static u64 mlx4_enable_sriov(struct mlx4_dev *dev, struct pci_dev *pdev,
  2762. u8 total_vfs, int existing_vfs, int reset_flow)
  2763. {
  2764. u64 dev_flags = dev->flags;
  2765. int err = 0;
  2766. int fw_enabled_sriov_vfs = min(pci_sriov_get_totalvfs(pdev),
  2767. MLX4_MAX_NUM_VF);
  2768. if (reset_flow) {
  2769. dev->dev_vfs = kcalloc(total_vfs, sizeof(*dev->dev_vfs),
  2770. GFP_KERNEL);
  2771. if (!dev->dev_vfs)
  2772. goto free_mem;
  2773. return dev_flags;
  2774. }
  2775. atomic_inc(&pf_loading);
  2776. if (dev->flags & MLX4_FLAG_SRIOV) {
  2777. if (existing_vfs != total_vfs) {
  2778. mlx4_err(dev, "SR-IOV was already enabled, but with num_vfs (%d) different than requested (%d)\n",
  2779. existing_vfs, total_vfs);
  2780. total_vfs = existing_vfs;
  2781. }
  2782. }
  2783. dev->dev_vfs = kcalloc(total_vfs, sizeof(*dev->dev_vfs), GFP_KERNEL);
  2784. if (NULL == dev->dev_vfs) {
  2785. mlx4_err(dev, "Failed to allocate memory for VFs\n");
  2786. goto disable_sriov;
  2787. }
  2788. if (!(dev->flags & MLX4_FLAG_SRIOV)) {
  2789. if (total_vfs > fw_enabled_sriov_vfs) {
  2790. mlx4_err(dev, "requested vfs (%d) > available vfs (%d). Continuing without SR_IOV\n",
  2791. total_vfs, fw_enabled_sriov_vfs);
  2792. err = -ENOMEM;
  2793. goto disable_sriov;
  2794. }
  2795. mlx4_warn(dev, "Enabling SR-IOV with %d VFs\n", total_vfs);
  2796. err = pci_enable_sriov(pdev, total_vfs);
  2797. }
  2798. if (err) {
  2799. mlx4_err(dev, "Failed to enable SR-IOV, continuing without SR-IOV (err = %d)\n",
  2800. err);
  2801. goto disable_sriov;
  2802. } else {
  2803. mlx4_warn(dev, "Running in master mode\n");
  2804. dev_flags |= MLX4_FLAG_SRIOV |
  2805. MLX4_FLAG_MASTER;
  2806. dev_flags &= ~MLX4_FLAG_SLAVE;
  2807. dev->persist->num_vfs = total_vfs;
  2808. }
  2809. return dev_flags;
  2810. disable_sriov:
  2811. atomic_dec(&pf_loading);
  2812. free_mem:
  2813. dev->persist->num_vfs = 0;
  2814. kfree(dev->dev_vfs);
  2815. dev->dev_vfs = NULL;
  2816. return dev_flags & ~MLX4_FLAG_MASTER;
  2817. }
  2818. enum {
  2819. MLX4_DEV_CAP_CHECK_NUM_VFS_ABOVE_64 = -1,
  2820. };
  2821. static int mlx4_check_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
  2822. int *nvfs)
  2823. {
  2824. int requested_vfs = nvfs[0] + nvfs[1] + nvfs[2];
  2825. /* Checking for 64 VFs as a limitation of CX2 */
  2826. if (!(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_80_VFS) &&
  2827. requested_vfs >= 64) {
  2828. mlx4_err(dev, "Requested %d VFs, but FW does not support more than 64\n",
  2829. requested_vfs);
  2830. return MLX4_DEV_CAP_CHECK_NUM_VFS_ABOVE_64;
  2831. }
  2832. return 0;
  2833. }
  2834. static int mlx4_pci_enable_device(struct mlx4_dev *dev)
  2835. {
  2836. struct pci_dev *pdev = dev->persist->pdev;
  2837. int err = 0;
  2838. mutex_lock(&dev->persist->pci_status_mutex);
  2839. if (dev->persist->pci_status == MLX4_PCI_STATUS_DISABLED) {
  2840. err = pci_enable_device(pdev);
  2841. if (!err)
  2842. dev->persist->pci_status = MLX4_PCI_STATUS_ENABLED;
  2843. }
  2844. mutex_unlock(&dev->persist->pci_status_mutex);
  2845. return err;
  2846. }
  2847. static void mlx4_pci_disable_device(struct mlx4_dev *dev)
  2848. {
  2849. struct pci_dev *pdev = dev->persist->pdev;
  2850. mutex_lock(&dev->persist->pci_status_mutex);
  2851. if (dev->persist->pci_status == MLX4_PCI_STATUS_ENABLED) {
  2852. pci_disable_device(pdev);
  2853. dev->persist->pci_status = MLX4_PCI_STATUS_DISABLED;
  2854. }
  2855. mutex_unlock(&dev->persist->pci_status_mutex);
  2856. }
  2857. static int mlx4_load_one(struct pci_dev *pdev, int pci_dev_data,
  2858. int total_vfs, int *nvfs, struct mlx4_priv *priv,
  2859. int reset_flow)
  2860. {
  2861. struct devlink *devlink = priv_to_devlink(priv);
  2862. struct mlx4_dev *dev;
  2863. unsigned sum = 0;
  2864. int err;
  2865. int port;
  2866. int i;
  2867. struct mlx4_dev_cap *dev_cap = NULL;
  2868. int existing_vfs = 0;
  2869. devl_assert_locked(devlink);
  2870. dev = &priv->dev;
  2871. err = mlx4_adev_init(dev);
  2872. if (err)
  2873. return err;
  2874. ATOMIC_INIT_NOTIFIER_HEAD(&priv->event_nh);
  2875. mutex_init(&priv->port_mutex);
  2876. mutex_init(&priv->bond_mutex);
  2877. INIT_LIST_HEAD(&priv->pgdir_list);
  2878. mutex_init(&priv->pgdir_mutex);
  2879. spin_lock_init(&priv->cmd.context_lock);
  2880. INIT_LIST_HEAD(&priv->bf_list);
  2881. mutex_init(&priv->bf_mutex);
  2882. dev->rev_id = pdev->revision;
  2883. dev->numa_node = dev_to_node(&pdev->dev);
  2884. /* Detect if this device is a virtual function */
  2885. if (pci_dev_data & MLX4_PCI_DEV_IS_VF) {
  2886. mlx4_warn(dev, "Detected virtual function - running in slave mode\n");
  2887. dev->flags |= MLX4_FLAG_SLAVE;
  2888. } else {
  2889. /* We reset the device and enable SRIOV only for physical
  2890. * devices. Try to claim ownership on the device;
  2891. * if already taken, skip -- do not allow multiple PFs */
  2892. err = mlx4_get_ownership(dev);
  2893. if (err) {
  2894. if (err < 0)
  2895. goto err_adev;
  2896. else {
  2897. mlx4_warn(dev, "Multiple PFs not yet supported - Skipping PF\n");
  2898. err = -EINVAL;
  2899. goto err_adev;
  2900. }
  2901. }
  2902. atomic_set(&priv->opreq_count, 0);
  2903. INIT_WORK(&priv->opreq_task, mlx4_opreq_action);
  2904. /*
  2905. * Now reset the HCA before we touch the PCI capabilities or
  2906. * attempt a firmware command, since a boot ROM may have left
  2907. * the HCA in an undefined state.
  2908. */
  2909. err = mlx4_reset(dev);
  2910. if (err) {
  2911. mlx4_err(dev, "Failed to reset HCA, aborting\n");
  2912. goto err_sriov;
  2913. }
  2914. if (total_vfs) {
  2915. dev->flags = MLX4_FLAG_MASTER;
  2916. existing_vfs = pci_num_vf(pdev);
  2917. if (existing_vfs)
  2918. dev->flags |= MLX4_FLAG_SRIOV;
  2919. dev->persist->num_vfs = total_vfs;
  2920. }
  2921. }
  2922. /* on load remove any previous indication of internal error,
  2923. * device is up.
  2924. */
  2925. dev->persist->state = MLX4_DEVICE_STATE_UP;
  2926. slave_start:
  2927. err = mlx4_cmd_init(dev);
  2928. if (err) {
  2929. mlx4_err(dev, "Failed to init command interface, aborting\n");
  2930. goto err_sriov;
  2931. }
  2932. /* In slave functions, the communication channel must be initialized
  2933. * before posting commands. Also, init num_slaves before calling
  2934. * mlx4_init_hca */
  2935. if (mlx4_is_mfunc(dev)) {
  2936. if (mlx4_is_master(dev)) {
  2937. dev->num_slaves = MLX4_MAX_NUM_SLAVES;
  2938. } else {
  2939. dev->num_slaves = 0;
  2940. err = mlx4_multi_func_init(dev);
  2941. if (err) {
  2942. mlx4_err(dev, "Failed to init slave mfunc interface, aborting\n");
  2943. goto err_cmd;
  2944. }
  2945. }
  2946. }
  2947. err = mlx4_init_fw(dev);
  2948. if (err) {
  2949. mlx4_err(dev, "Failed to init fw, aborting.\n");
  2950. goto err_mfunc;
  2951. }
  2952. if (mlx4_is_master(dev)) {
  2953. /* when we hit the goto slave_start below, dev_cap already initialized */
  2954. if (!dev_cap) {
  2955. dev_cap = kzalloc(sizeof(*dev_cap), GFP_KERNEL);
  2956. if (!dev_cap) {
  2957. err = -ENOMEM;
  2958. goto err_fw;
  2959. }
  2960. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  2961. if (err) {
  2962. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  2963. goto err_fw;
  2964. }
  2965. if (mlx4_check_dev_cap(dev, dev_cap, nvfs))
  2966. goto err_fw;
  2967. if (!(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS)) {
  2968. u64 dev_flags = mlx4_enable_sriov(dev, pdev,
  2969. total_vfs,
  2970. existing_vfs,
  2971. reset_flow);
  2972. mlx4_close_fw(dev);
  2973. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  2974. dev->flags = dev_flags;
  2975. if (!SRIOV_VALID_STATE(dev->flags)) {
  2976. mlx4_err(dev, "Invalid SRIOV state\n");
  2977. goto err_sriov;
  2978. }
  2979. err = mlx4_reset(dev);
  2980. if (err) {
  2981. mlx4_err(dev, "Failed to reset HCA, aborting.\n");
  2982. goto err_sriov;
  2983. }
  2984. goto slave_start;
  2985. }
  2986. } else {
  2987. /* Legacy mode FW requires SRIOV to be enabled before
  2988. * doing QUERY_DEV_CAP, since max_eq's value is different if
  2989. * SRIOV is enabled.
  2990. */
  2991. memset(dev_cap, 0, sizeof(*dev_cap));
  2992. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  2993. if (err) {
  2994. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  2995. goto err_fw;
  2996. }
  2997. if (mlx4_check_dev_cap(dev, dev_cap, nvfs))
  2998. goto err_fw;
  2999. }
  3000. }
  3001. err = mlx4_init_hca(dev);
  3002. if (err) {
  3003. if (err == -EACCES) {
  3004. /* Not primary Physical function
  3005. * Running in slave mode */
  3006. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  3007. /* We're not a PF */
  3008. if (dev->flags & MLX4_FLAG_SRIOV) {
  3009. if (!existing_vfs)
  3010. pci_disable_sriov(pdev);
  3011. if (mlx4_is_master(dev) && !reset_flow)
  3012. atomic_dec(&pf_loading);
  3013. dev->flags &= ~MLX4_FLAG_SRIOV;
  3014. }
  3015. if (!mlx4_is_slave(dev))
  3016. mlx4_free_ownership(dev);
  3017. dev->flags |= MLX4_FLAG_SLAVE;
  3018. dev->flags &= ~MLX4_FLAG_MASTER;
  3019. goto slave_start;
  3020. } else
  3021. goto err_fw;
  3022. }
  3023. if (mlx4_is_master(dev) && (dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS)) {
  3024. u64 dev_flags = mlx4_enable_sriov(dev, pdev, total_vfs,
  3025. existing_vfs, reset_flow);
  3026. if ((dev->flags ^ dev_flags) & (MLX4_FLAG_MASTER | MLX4_FLAG_SLAVE)) {
  3027. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_VHCR);
  3028. dev->flags = dev_flags;
  3029. err = mlx4_cmd_init(dev);
  3030. if (err) {
  3031. /* Only VHCR is cleaned up, so could still
  3032. * send FW commands
  3033. */
  3034. mlx4_err(dev, "Failed to init VHCR command interface, aborting\n");
  3035. goto err_close;
  3036. }
  3037. } else {
  3038. dev->flags = dev_flags;
  3039. }
  3040. if (!SRIOV_VALID_STATE(dev->flags)) {
  3041. mlx4_err(dev, "Invalid SRIOV state\n");
  3042. err = -EINVAL;
  3043. goto err_close;
  3044. }
  3045. }
  3046. /* check if the device is functioning at its maximum possible speed.
  3047. * No return code for this call, just warn the user in case of PCI
  3048. * express device capabilities are under-satisfied by the bus.
  3049. */
  3050. if (!mlx4_is_slave(dev))
  3051. pcie_print_link_status(dev->persist->pdev);
  3052. /* In master functions, the communication channel must be initialized
  3053. * after obtaining its address from fw */
  3054. if (mlx4_is_master(dev)) {
  3055. if (dev->caps.num_ports < 2 &&
  3056. num_vfs_argc > 1) {
  3057. err = -EINVAL;
  3058. mlx4_err(dev,
  3059. "Error: Trying to configure VFs on port 2, but HCA has only %d physical ports\n",
  3060. dev->caps.num_ports);
  3061. goto err_close;
  3062. }
  3063. memcpy(dev->persist->nvfs, nvfs, sizeof(dev->persist->nvfs));
  3064. for (i = 0;
  3065. i < sizeof(dev->persist->nvfs)/
  3066. sizeof(dev->persist->nvfs[0]); i++) {
  3067. unsigned j;
  3068. for (j = 0; j < dev->persist->nvfs[i]; ++sum, ++j) {
  3069. dev->dev_vfs[sum].min_port = i < 2 ? i + 1 : 1;
  3070. dev->dev_vfs[sum].n_ports = i < 2 ? 1 :
  3071. dev->caps.num_ports;
  3072. }
  3073. }
  3074. /* In master functions, the communication channel
  3075. * must be initialized after obtaining its address from fw
  3076. */
  3077. err = mlx4_multi_func_init(dev);
  3078. if (err) {
  3079. mlx4_err(dev, "Failed to init master mfunc interface, aborting.\n");
  3080. goto err_close;
  3081. }
  3082. }
  3083. err = mlx4_alloc_eq_table(dev);
  3084. if (err)
  3085. goto err_master_mfunc;
  3086. bitmap_zero(priv->msix_ctl.pool_bm, MAX_MSIX);
  3087. mutex_init(&priv->msix_ctl.pool_lock);
  3088. mlx4_enable_msi_x(dev);
  3089. if ((mlx4_is_mfunc(dev)) &&
  3090. !(dev->flags & MLX4_FLAG_MSI_X)) {
  3091. err = -EOPNOTSUPP;
  3092. mlx4_err(dev, "INTx is not supported in multi-function mode, aborting\n");
  3093. goto err_free_eq;
  3094. }
  3095. if (!mlx4_is_slave(dev)) {
  3096. err = mlx4_init_steering(dev);
  3097. if (err)
  3098. goto err_disable_msix;
  3099. }
  3100. mlx4_init_quotas(dev);
  3101. err = mlx4_setup_hca(dev);
  3102. if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X) &&
  3103. !mlx4_is_mfunc(dev)) {
  3104. dev->flags &= ~MLX4_FLAG_MSI_X;
  3105. dev->caps.num_comp_vectors = 1;
  3106. pci_disable_msix(pdev);
  3107. err = mlx4_setup_hca(dev);
  3108. }
  3109. if (err)
  3110. goto err_steer;
  3111. /* When PF resources are ready arm its comm channel to enable
  3112. * getting commands
  3113. */
  3114. if (mlx4_is_master(dev)) {
  3115. err = mlx4_ARM_COMM_CHANNEL(dev);
  3116. if (err) {
  3117. mlx4_err(dev, " Failed to arm comm channel eq: %x\n",
  3118. err);
  3119. goto err_steer;
  3120. }
  3121. }
  3122. for (port = 1; port <= dev->caps.num_ports; port++) {
  3123. err = mlx4_init_port_info(dev, port);
  3124. if (err)
  3125. goto err_port;
  3126. }
  3127. priv->v2p.port1 = 1;
  3128. priv->v2p.port2 = 2;
  3129. err = mlx4_register_device(dev);
  3130. if (err)
  3131. goto err_port;
  3132. mlx4_sense_init(dev);
  3133. mlx4_start_sense(dev);
  3134. priv->removed = 0;
  3135. if (mlx4_is_master(dev) && dev->persist->num_vfs && !reset_flow)
  3136. atomic_dec(&pf_loading);
  3137. kfree(dev_cap);
  3138. return 0;
  3139. err_port:
  3140. for (--port; port >= 1; --port)
  3141. mlx4_cleanup_port_info(&priv->port[port]);
  3142. mlx4_cleanup_default_counters(dev);
  3143. if (!mlx4_is_slave(dev))
  3144. mlx4_cleanup_counters_table(dev);
  3145. mlx4_cleanup_qp_table(dev);
  3146. mlx4_cleanup_srq_table(dev);
  3147. mlx4_cleanup_cq_table(dev);
  3148. mlx4_cmd_use_polling(dev);
  3149. mlx4_cleanup_eq_table(dev);
  3150. mlx4_cleanup_mcg_table(dev);
  3151. mlx4_cleanup_mr_table(dev);
  3152. mlx4_cleanup_xrcd_table(dev);
  3153. mlx4_cleanup_pd_table(dev);
  3154. mlx4_cleanup_uar_table(dev);
  3155. err_steer:
  3156. if (!mlx4_is_slave(dev))
  3157. mlx4_clear_steering(dev);
  3158. err_disable_msix:
  3159. if (dev->flags & MLX4_FLAG_MSI_X)
  3160. pci_disable_msix(pdev);
  3161. err_free_eq:
  3162. mlx4_free_eq_table(dev);
  3163. err_master_mfunc:
  3164. if (mlx4_is_master(dev)) {
  3165. mlx4_free_resource_tracker(dev, RES_TR_FREE_STRUCTS_ONLY);
  3166. mlx4_multi_func_cleanup(dev);
  3167. }
  3168. if (mlx4_is_slave(dev))
  3169. mlx4_slave_destroy_special_qp_cap(dev);
  3170. err_close:
  3171. mlx4_close_hca(dev);
  3172. err_fw:
  3173. mlx4_close_fw(dev);
  3174. err_mfunc:
  3175. if (mlx4_is_slave(dev))
  3176. mlx4_multi_func_cleanup(dev);
  3177. err_cmd:
  3178. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  3179. err_sriov:
  3180. if (dev->flags & MLX4_FLAG_SRIOV && !existing_vfs) {
  3181. pci_disable_sriov(pdev);
  3182. dev->flags &= ~MLX4_FLAG_SRIOV;
  3183. }
  3184. if (mlx4_is_master(dev) && dev->persist->num_vfs && !reset_flow)
  3185. atomic_dec(&pf_loading);
  3186. kfree(priv->dev.dev_vfs);
  3187. if (!mlx4_is_slave(dev))
  3188. mlx4_free_ownership(dev);
  3189. kfree(dev_cap);
  3190. err_adev:
  3191. mlx4_adev_cleanup(dev);
  3192. return err;
  3193. }
  3194. static int __mlx4_init_one(struct pci_dev *pdev, int pci_dev_data,
  3195. struct mlx4_priv *priv)
  3196. {
  3197. int err;
  3198. int nvfs[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  3199. int prb_vf[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  3200. const int param_map[MLX4_MAX_PORTS + 1][MLX4_MAX_PORTS + 1] = {
  3201. {2, 0, 0}, {0, 1, 2}, {0, 1, 2} };
  3202. unsigned total_vfs = 0;
  3203. unsigned int i;
  3204. pr_info(DRV_NAME ": Initializing %s\n", pci_name(pdev));
  3205. err = mlx4_pci_enable_device(&priv->dev);
  3206. if (err) {
  3207. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  3208. return err;
  3209. }
  3210. /* Due to requirement that all VFs and the PF are *guaranteed* 2 MACS
  3211. * per port, we must limit the number of VFs to 63 (since their are
  3212. * 128 MACs)
  3213. */
  3214. for (i = 0; i < ARRAY_SIZE(nvfs) && i < num_vfs_argc;
  3215. total_vfs += nvfs[param_map[num_vfs_argc - 1][i]], i++) {
  3216. nvfs[param_map[num_vfs_argc - 1][i]] = num_vfs[i];
  3217. if (nvfs[i] < 0) {
  3218. dev_err(&pdev->dev, "num_vfs module parameter cannot be negative\n");
  3219. err = -EINVAL;
  3220. goto err_disable_pdev;
  3221. }
  3222. }
  3223. for (i = 0; i < ARRAY_SIZE(prb_vf) && i < probe_vfs_argc;
  3224. i++) {
  3225. prb_vf[param_map[probe_vfs_argc - 1][i]] = probe_vf[i];
  3226. if (prb_vf[i] < 0 || prb_vf[i] > nvfs[i]) {
  3227. dev_err(&pdev->dev, "probe_vf module parameter cannot be negative or greater than num_vfs\n");
  3228. err = -EINVAL;
  3229. goto err_disable_pdev;
  3230. }
  3231. }
  3232. if (total_vfs > MLX4_MAX_NUM_VF) {
  3233. dev_err(&pdev->dev,
  3234. "Requested more VF's (%d) than allowed by hw (%d)\n",
  3235. total_vfs, MLX4_MAX_NUM_VF);
  3236. err = -EINVAL;
  3237. goto err_disable_pdev;
  3238. }
  3239. for (i = 0; i < MLX4_MAX_PORTS; i++) {
  3240. if (nvfs[i] + nvfs[2] > MLX4_MAX_NUM_VF_P_PORT) {
  3241. dev_err(&pdev->dev,
  3242. "Requested more VF's (%d) for port (%d) than allowed by driver (%d)\n",
  3243. nvfs[i] + nvfs[2], i + 1,
  3244. MLX4_MAX_NUM_VF_P_PORT);
  3245. err = -EINVAL;
  3246. goto err_disable_pdev;
  3247. }
  3248. }
  3249. /* Check for BARs. */
  3250. if (!(pci_dev_data & MLX4_PCI_DEV_IS_VF) &&
  3251. !(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  3252. dev_err(&pdev->dev, "Missing DCS, aborting (driver_data: 0x%x, pci_resource_flags(pdev, 0):0x%lx)\n",
  3253. pci_dev_data, pci_resource_flags(pdev, 0));
  3254. err = -ENODEV;
  3255. goto err_disable_pdev;
  3256. }
  3257. if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  3258. dev_err(&pdev->dev, "Missing UAR, aborting\n");
  3259. err = -ENODEV;
  3260. goto err_disable_pdev;
  3261. }
  3262. err = pci_request_regions(pdev, DRV_NAME);
  3263. if (err) {
  3264. dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
  3265. goto err_disable_pdev;
  3266. }
  3267. pci_set_master(pdev);
  3268. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  3269. if (err) {
  3270. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
  3271. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  3272. if (err) {
  3273. dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
  3274. goto err_release_regions;
  3275. }
  3276. }
  3277. /* Allow large DMA segments, up to the firmware limit of 1 GB */
  3278. dma_set_max_seg_size(&pdev->dev, 1024 * 1024 * 1024);
  3279. /* Detect if this device is a virtual function */
  3280. if (pci_dev_data & MLX4_PCI_DEV_IS_VF) {
  3281. /* When acting as pf, we normally skip vfs unless explicitly
  3282. * requested to probe them.
  3283. */
  3284. if (total_vfs) {
  3285. unsigned vfs_offset = 0;
  3286. for (i = 0; i < ARRAY_SIZE(nvfs) &&
  3287. vfs_offset + nvfs[i] < extended_func_num(pdev);
  3288. vfs_offset += nvfs[i], i++)
  3289. ;
  3290. if (i == ARRAY_SIZE(nvfs)) {
  3291. err = -ENODEV;
  3292. goto err_release_regions;
  3293. }
  3294. if ((extended_func_num(pdev) - vfs_offset)
  3295. > prb_vf[i]) {
  3296. dev_warn(&pdev->dev, "Skipping virtual function:%d\n",
  3297. extended_func_num(pdev));
  3298. err = -ENODEV;
  3299. goto err_release_regions;
  3300. }
  3301. }
  3302. }
  3303. err = mlx4_crdump_init(&priv->dev);
  3304. if (err)
  3305. goto err_release_regions;
  3306. err = mlx4_catas_init(&priv->dev);
  3307. if (err)
  3308. goto err_crdump;
  3309. err = mlx4_load_one(pdev, pci_dev_data, total_vfs, nvfs, priv, 0);
  3310. if (err)
  3311. goto err_catas;
  3312. return 0;
  3313. err_catas:
  3314. mlx4_catas_end(&priv->dev);
  3315. err_crdump:
  3316. mlx4_crdump_end(&priv->dev);
  3317. err_release_regions:
  3318. pci_release_regions(pdev);
  3319. err_disable_pdev:
  3320. mlx4_pci_disable_device(&priv->dev);
  3321. return err;
  3322. }
  3323. static void mlx4_devlink_param_load_driverinit_values(struct devlink *devlink)
  3324. {
  3325. struct mlx4_priv *priv = devlink_priv(devlink);
  3326. struct mlx4_dev *dev = &priv->dev;
  3327. struct mlx4_fw_crdump *crdump = &dev->persist->crdump;
  3328. union devlink_param_value saved_value;
  3329. int err;
  3330. err = devl_param_driverinit_value_get(devlink,
  3331. DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
  3332. &saved_value);
  3333. if (!err && mlx4_internal_err_reset != saved_value.vbool) {
  3334. mlx4_internal_err_reset = saved_value.vbool;
  3335. /* Notify on value changed on runtime configuration mode */
  3336. devl_param_value_changed(devlink,
  3337. DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET);
  3338. }
  3339. err = devl_param_driverinit_value_get(devlink,
  3340. DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
  3341. &saved_value);
  3342. if (!err)
  3343. log_num_mac = order_base_2(saved_value.vu32);
  3344. err = devl_param_driverinit_value_get(devlink,
  3345. MLX4_DEVLINK_PARAM_ID_ENABLE_64B_CQE_EQE,
  3346. &saved_value);
  3347. if (!err)
  3348. enable_64b_cqe_eqe = saved_value.vbool;
  3349. err = devl_param_driverinit_value_get(devlink,
  3350. MLX4_DEVLINK_PARAM_ID_ENABLE_4K_UAR,
  3351. &saved_value);
  3352. if (!err)
  3353. enable_4k_uar = saved_value.vbool;
  3354. err = devl_param_driverinit_value_get(devlink,
  3355. DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
  3356. &saved_value);
  3357. if (!err && crdump->snapshot_enable != saved_value.vbool) {
  3358. crdump->snapshot_enable = saved_value.vbool;
  3359. devl_param_value_changed(devlink,
  3360. DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT);
  3361. }
  3362. }
  3363. static void mlx4_restart_one_down(struct pci_dev *pdev);
  3364. static int mlx4_restart_one_up(struct pci_dev *pdev, bool reload,
  3365. struct devlink *devlink);
  3366. static int mlx4_devlink_reload_down(struct devlink *devlink, bool netns_change,
  3367. enum devlink_reload_action action,
  3368. enum devlink_reload_limit limit,
  3369. struct netlink_ext_ack *extack)
  3370. {
  3371. struct mlx4_priv *priv = devlink_priv(devlink);
  3372. struct mlx4_dev *dev = &priv->dev;
  3373. struct mlx4_dev_persistent *persist = dev->persist;
  3374. if (netns_change) {
  3375. NL_SET_ERR_MSG_MOD(extack, "Namespace change is not supported");
  3376. return -EOPNOTSUPP;
  3377. }
  3378. if (persist->num_vfs)
  3379. mlx4_warn(persist->dev, "Reload performed on PF, will cause reset on operating Virtual Functions\n");
  3380. mlx4_restart_one_down(persist->pdev);
  3381. return 0;
  3382. }
  3383. static int mlx4_devlink_reload_up(struct devlink *devlink, enum devlink_reload_action action,
  3384. enum devlink_reload_limit limit, u32 *actions_performed,
  3385. struct netlink_ext_ack *extack)
  3386. {
  3387. struct mlx4_priv *priv = devlink_priv(devlink);
  3388. struct mlx4_dev *dev = &priv->dev;
  3389. struct mlx4_dev_persistent *persist = dev->persist;
  3390. int err;
  3391. *actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
  3392. err = mlx4_restart_one_up(persist->pdev, true, devlink);
  3393. if (err)
  3394. mlx4_err(persist->dev, "mlx4_restart_one_up failed, ret=%d\n",
  3395. err);
  3396. return err;
  3397. }
  3398. static const struct devlink_ops mlx4_devlink_ops = {
  3399. .reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT),
  3400. .reload_down = mlx4_devlink_reload_down,
  3401. .reload_up = mlx4_devlink_reload_up,
  3402. };
  3403. static int mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3404. {
  3405. struct devlink *devlink;
  3406. struct mlx4_priv *priv;
  3407. struct mlx4_dev *dev;
  3408. int ret;
  3409. printk_once(KERN_INFO "%s", mlx4_version);
  3410. devlink = devlink_alloc(&mlx4_devlink_ops, sizeof(*priv), &pdev->dev);
  3411. if (!devlink)
  3412. return -ENOMEM;
  3413. devl_lock(devlink);
  3414. priv = devlink_priv(devlink);
  3415. dev = &priv->dev;
  3416. dev->persist = kzalloc(sizeof(*dev->persist), GFP_KERNEL);
  3417. if (!dev->persist) {
  3418. ret = -ENOMEM;
  3419. goto err_devlink_free;
  3420. }
  3421. dev->persist->pdev = pdev;
  3422. dev->persist->dev = dev;
  3423. pci_set_drvdata(pdev, dev->persist);
  3424. priv->pci_dev_data = id->driver_data;
  3425. mutex_init(&dev->persist->device_state_mutex);
  3426. mutex_init(&dev->persist->interface_state_mutex);
  3427. mutex_init(&dev->persist->pci_status_mutex);
  3428. ret = devl_params_register(devlink, mlx4_devlink_params,
  3429. ARRAY_SIZE(mlx4_devlink_params));
  3430. if (ret)
  3431. goto err_devlink_unregister;
  3432. mlx4_devlink_set_params_init_values(devlink);
  3433. ret = __mlx4_init_one(pdev, id->driver_data, priv);
  3434. if (ret)
  3435. goto err_params_unregister;
  3436. pci_save_state(pdev);
  3437. devl_unlock(devlink);
  3438. devlink_register(devlink);
  3439. return 0;
  3440. err_params_unregister:
  3441. devl_params_unregister(devlink, mlx4_devlink_params,
  3442. ARRAY_SIZE(mlx4_devlink_params));
  3443. err_devlink_unregister:
  3444. kfree(dev->persist);
  3445. err_devlink_free:
  3446. devl_unlock(devlink);
  3447. devlink_free(devlink);
  3448. return ret;
  3449. }
  3450. static void mlx4_clean_dev(struct mlx4_dev *dev)
  3451. {
  3452. struct mlx4_dev_persistent *persist = dev->persist;
  3453. struct mlx4_priv *priv = mlx4_priv(dev);
  3454. unsigned long flags = (dev->flags & RESET_PERSIST_MASK_FLAGS);
  3455. memset(priv, 0, sizeof(*priv));
  3456. priv->dev.persist = persist;
  3457. priv->dev.flags = flags;
  3458. }
  3459. static void mlx4_unload_one(struct pci_dev *pdev)
  3460. {
  3461. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3462. struct mlx4_dev *dev = persist->dev;
  3463. struct mlx4_priv *priv = mlx4_priv(dev);
  3464. int pci_dev_data;
  3465. struct devlink *devlink;
  3466. int p, i;
  3467. devlink = priv_to_devlink(priv);
  3468. devl_assert_locked(devlink);
  3469. if (priv->removed)
  3470. return;
  3471. /* saving current ports type for further use */
  3472. for (i = 0; i < dev->caps.num_ports; i++) {
  3473. dev->persist->curr_port_type[i] = dev->caps.port_type[i + 1];
  3474. dev->persist->curr_port_poss_type[i] = dev->caps.
  3475. possible_type[i + 1];
  3476. }
  3477. pci_dev_data = priv->pci_dev_data;
  3478. mlx4_stop_sense(dev);
  3479. mlx4_unregister_device(dev);
  3480. for (p = 1; p <= dev->caps.num_ports; p++) {
  3481. mlx4_cleanup_port_info(&priv->port[p]);
  3482. mlx4_CLOSE_PORT(dev, p);
  3483. }
  3484. if (mlx4_is_master(dev))
  3485. mlx4_free_resource_tracker(dev,
  3486. RES_TR_FREE_SLAVES_ONLY);
  3487. mlx4_cleanup_default_counters(dev);
  3488. if (!mlx4_is_slave(dev))
  3489. mlx4_cleanup_counters_table(dev);
  3490. mlx4_cleanup_qp_table(dev);
  3491. mlx4_cleanup_srq_table(dev);
  3492. mlx4_cleanup_cq_table(dev);
  3493. mlx4_cmd_use_polling(dev);
  3494. mlx4_cleanup_eq_table(dev);
  3495. mlx4_cleanup_mcg_table(dev);
  3496. mlx4_cleanup_mr_table(dev);
  3497. mlx4_cleanup_xrcd_table(dev);
  3498. mlx4_cleanup_pd_table(dev);
  3499. if (mlx4_is_master(dev))
  3500. mlx4_free_resource_tracker(dev,
  3501. RES_TR_FREE_STRUCTS_ONLY);
  3502. iounmap(priv->kar);
  3503. mlx4_uar_free(dev, &priv->driver_uar);
  3504. mlx4_cleanup_uar_table(dev);
  3505. if (!mlx4_is_slave(dev))
  3506. mlx4_clear_steering(dev);
  3507. mlx4_free_eq_table(dev);
  3508. if (mlx4_is_master(dev))
  3509. mlx4_multi_func_cleanup(dev);
  3510. mlx4_close_hca(dev);
  3511. mlx4_close_fw(dev);
  3512. if (mlx4_is_slave(dev))
  3513. mlx4_multi_func_cleanup(dev);
  3514. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  3515. if (dev->flags & MLX4_FLAG_MSI_X)
  3516. pci_disable_msix(pdev);
  3517. if (!mlx4_is_slave(dev))
  3518. mlx4_free_ownership(dev);
  3519. mlx4_slave_destroy_special_qp_cap(dev);
  3520. kfree(dev->dev_vfs);
  3521. mlx4_adev_cleanup(dev);
  3522. mlx4_clean_dev(dev);
  3523. priv->pci_dev_data = pci_dev_data;
  3524. priv->removed = 1;
  3525. }
  3526. static void mlx4_remove_one(struct pci_dev *pdev)
  3527. {
  3528. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3529. struct mlx4_dev *dev = persist->dev;
  3530. struct mlx4_priv *priv = mlx4_priv(dev);
  3531. struct devlink *devlink = priv_to_devlink(priv);
  3532. int active_vfs = 0;
  3533. devlink_unregister(devlink);
  3534. devl_lock(devlink);
  3535. if (mlx4_is_slave(dev))
  3536. persist->interface_state |= MLX4_INTERFACE_STATE_NOWAIT;
  3537. mutex_lock(&persist->interface_state_mutex);
  3538. persist->interface_state |= MLX4_INTERFACE_STATE_DELETION;
  3539. mutex_unlock(&persist->interface_state_mutex);
  3540. /* Disabling SR-IOV is not allowed while there are active vf's */
  3541. if (mlx4_is_master(dev) && dev->flags & MLX4_FLAG_SRIOV) {
  3542. active_vfs = mlx4_how_many_lives_vf(dev);
  3543. if (active_vfs) {
  3544. pr_warn("Removing PF when there are active VF's !!\n");
  3545. pr_warn("Will not disable SR-IOV.\n");
  3546. }
  3547. }
  3548. /* device marked to be under deletion running now without the lock
  3549. * letting other tasks to be terminated
  3550. */
  3551. if (persist->interface_state & MLX4_INTERFACE_STATE_UP)
  3552. mlx4_unload_one(pdev);
  3553. else
  3554. mlx4_info(dev, "%s: interface is down\n", __func__);
  3555. mlx4_catas_end(dev);
  3556. mlx4_crdump_end(dev);
  3557. if (dev->flags & MLX4_FLAG_SRIOV && !active_vfs) {
  3558. mlx4_warn(dev, "Disabling SR-IOV\n");
  3559. pci_disable_sriov(pdev);
  3560. }
  3561. pci_release_regions(pdev);
  3562. mlx4_pci_disable_device(dev);
  3563. devl_params_unregister(devlink, mlx4_devlink_params,
  3564. ARRAY_SIZE(mlx4_devlink_params));
  3565. kfree(dev->persist);
  3566. devl_unlock(devlink);
  3567. devlink_free(devlink);
  3568. }
  3569. static int restore_current_port_types(struct mlx4_dev *dev,
  3570. enum mlx4_port_type *types,
  3571. enum mlx4_port_type *poss_types)
  3572. {
  3573. struct mlx4_priv *priv = mlx4_priv(dev);
  3574. int err, i;
  3575. mlx4_stop_sense(dev);
  3576. mutex_lock(&priv->port_mutex);
  3577. for (i = 0; i < dev->caps.num_ports; i++)
  3578. dev->caps.possible_type[i + 1] = poss_types[i];
  3579. err = mlx4_change_port_types(dev, types);
  3580. mlx4_start_sense(dev);
  3581. mutex_unlock(&priv->port_mutex);
  3582. return err;
  3583. }
  3584. static void mlx4_restart_one_down(struct pci_dev *pdev)
  3585. {
  3586. mlx4_unload_one(pdev);
  3587. }
  3588. static int mlx4_restart_one_up(struct pci_dev *pdev, bool reload,
  3589. struct devlink *devlink)
  3590. {
  3591. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3592. struct mlx4_dev *dev = persist->dev;
  3593. struct mlx4_priv *priv = mlx4_priv(dev);
  3594. int nvfs[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  3595. int pci_dev_data, err, total_vfs;
  3596. pci_dev_data = priv->pci_dev_data;
  3597. total_vfs = dev->persist->num_vfs;
  3598. memcpy(nvfs, dev->persist->nvfs, sizeof(dev->persist->nvfs));
  3599. if (reload)
  3600. mlx4_devlink_param_load_driverinit_values(devlink);
  3601. err = mlx4_load_one(pdev, pci_dev_data, total_vfs, nvfs, priv, 1);
  3602. if (err) {
  3603. mlx4_err(dev, "%s: ERROR: mlx4_load_one failed, pci_name=%s, err=%d\n",
  3604. __func__, pci_name(pdev), err);
  3605. return err;
  3606. }
  3607. err = restore_current_port_types(dev, dev->persist->curr_port_type,
  3608. dev->persist->curr_port_poss_type);
  3609. if (err)
  3610. mlx4_err(dev, "could not restore original port types (%d)\n",
  3611. err);
  3612. return err;
  3613. }
  3614. int mlx4_restart_one(struct pci_dev *pdev)
  3615. {
  3616. mlx4_restart_one_down(pdev);
  3617. return mlx4_restart_one_up(pdev, false, NULL);
  3618. }
  3619. #define MLX_SP(id) { PCI_VDEVICE(MELLANOX, id), MLX4_PCI_DEV_FORCE_SENSE_PORT }
  3620. #define MLX_VF(id) { PCI_VDEVICE(MELLANOX, id), MLX4_PCI_DEV_IS_VF }
  3621. #define MLX_GN(id) { PCI_VDEVICE(MELLANOX, id), 0 }
  3622. static const struct pci_device_id mlx4_pci_table[] = {
  3623. #ifdef CONFIG_MLX4_CORE_GEN2
  3624. /* MT25408 "Hermon" */
  3625. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_SDR), /* SDR */
  3626. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_DDR), /* DDR */
  3627. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_QDR), /* QDR */
  3628. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_DDR_GEN2), /* DDR Gen2 */
  3629. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_QDR_GEN2), /* QDR Gen2 */
  3630. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_EN), /* EN 10GigE */
  3631. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_EN_GEN2), /* EN 10GigE Gen2 */
  3632. /* MT25458 ConnectX EN 10GBASE-T */
  3633. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX_EN),
  3634. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX_EN_T_GEN2), /* Gen2 */
  3635. /* MT26468 ConnectX EN 10GigE PCIe Gen2*/
  3636. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX_EN_GEN2),
  3637. /* MT26438 ConnectX EN 40GigE PCIe Gen2 5GT/s */
  3638. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX_EN_5_GEN2),
  3639. /* MT26478 ConnectX2 40GigE PCIe Gen2 */
  3640. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX2),
  3641. /* MT25400 Family [ConnectX-2] */
  3642. MLX_VF(0x1002), /* Virtual Function */
  3643. #endif /* CONFIG_MLX4_CORE_GEN2 */
  3644. /* MT27500 Family [ConnectX-3] */
  3645. MLX_GN(PCI_DEVICE_ID_MELLANOX_CONNECTX3),
  3646. MLX_VF(0x1004), /* Virtual Function */
  3647. MLX_GN(0x1005), /* MT27510 Family */
  3648. MLX_GN(0x1006), /* MT27511 Family */
  3649. MLX_GN(PCI_DEVICE_ID_MELLANOX_CONNECTX3_PRO), /* MT27520 Family */
  3650. MLX_GN(0x1008), /* MT27521 Family */
  3651. MLX_GN(0x1009), /* MT27530 Family */
  3652. MLX_GN(0x100a), /* MT27531 Family */
  3653. MLX_GN(0x100b), /* MT27540 Family */
  3654. MLX_GN(0x100c), /* MT27541 Family */
  3655. MLX_GN(0x100d), /* MT27550 Family */
  3656. MLX_GN(0x100e), /* MT27551 Family */
  3657. MLX_GN(0x100f), /* MT27560 Family */
  3658. MLX_GN(0x1010), /* MT27561 Family */
  3659. /*
  3660. * See the mellanox_check_broken_intx_masking() quirk when
  3661. * adding devices
  3662. */
  3663. { 0, }
  3664. };
  3665. MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
  3666. static pci_ers_result_t mlx4_pci_err_detected(struct pci_dev *pdev,
  3667. pci_channel_state_t state)
  3668. {
  3669. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3670. struct mlx4_dev *dev = persist->dev;
  3671. struct devlink *devlink;
  3672. mlx4_err(persist->dev, "mlx4_pci_err_detected was called\n");
  3673. mlx4_enter_error_state(persist);
  3674. devlink = priv_to_devlink(mlx4_priv(dev));
  3675. devl_lock(devlink);
  3676. mutex_lock(&persist->interface_state_mutex);
  3677. if (persist->interface_state & MLX4_INTERFACE_STATE_UP)
  3678. mlx4_unload_one(pdev);
  3679. mutex_unlock(&persist->interface_state_mutex);
  3680. devl_unlock(devlink);
  3681. if (state == pci_channel_io_perm_failure)
  3682. return PCI_ERS_RESULT_DISCONNECT;
  3683. mlx4_pci_disable_device(persist->dev);
  3684. return PCI_ERS_RESULT_NEED_RESET;
  3685. }
  3686. static pci_ers_result_t mlx4_pci_slot_reset(struct pci_dev *pdev)
  3687. {
  3688. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3689. struct mlx4_dev *dev = persist->dev;
  3690. int err;
  3691. mlx4_err(dev, "mlx4_pci_slot_reset was called\n");
  3692. err = mlx4_pci_enable_device(dev);
  3693. if (err) {
  3694. mlx4_err(dev, "Can not re-enable device, err=%d\n", err);
  3695. return PCI_ERS_RESULT_DISCONNECT;
  3696. }
  3697. pci_set_master(pdev);
  3698. pci_restore_state(pdev);
  3699. pci_save_state(pdev);
  3700. return PCI_ERS_RESULT_RECOVERED;
  3701. }
  3702. static void mlx4_pci_resume(struct pci_dev *pdev)
  3703. {
  3704. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3705. struct mlx4_dev *dev = persist->dev;
  3706. struct mlx4_priv *priv = mlx4_priv(dev);
  3707. int nvfs[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  3708. struct devlink *devlink;
  3709. int total_vfs;
  3710. int err;
  3711. mlx4_err(dev, "%s was called\n", __func__);
  3712. total_vfs = dev->persist->num_vfs;
  3713. memcpy(nvfs, dev->persist->nvfs, sizeof(dev->persist->nvfs));
  3714. devlink = priv_to_devlink(priv);
  3715. devl_lock(devlink);
  3716. mutex_lock(&persist->interface_state_mutex);
  3717. if (!(persist->interface_state & MLX4_INTERFACE_STATE_UP)) {
  3718. err = mlx4_load_one(pdev, priv->pci_dev_data, total_vfs, nvfs,
  3719. priv, 1);
  3720. if (err) {
  3721. mlx4_err(dev, "%s: mlx4_load_one failed, err=%d\n",
  3722. __func__, err);
  3723. goto end;
  3724. }
  3725. err = restore_current_port_types(dev, dev->persist->
  3726. curr_port_type, dev->persist->
  3727. curr_port_poss_type);
  3728. if (err)
  3729. mlx4_err(dev, "could not restore original port types (%d)\n", err);
  3730. }
  3731. end:
  3732. mutex_unlock(&persist->interface_state_mutex);
  3733. devl_unlock(devlink);
  3734. }
  3735. static void mlx4_shutdown(struct pci_dev *pdev)
  3736. {
  3737. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3738. struct mlx4_dev *dev = persist->dev;
  3739. struct devlink *devlink;
  3740. mlx4_info(persist->dev, "mlx4_shutdown was called\n");
  3741. devlink = priv_to_devlink(mlx4_priv(dev));
  3742. devl_lock(devlink);
  3743. mutex_lock(&persist->interface_state_mutex);
  3744. if (persist->interface_state & MLX4_INTERFACE_STATE_UP)
  3745. mlx4_unload_one(pdev);
  3746. mutex_unlock(&persist->interface_state_mutex);
  3747. devl_unlock(devlink);
  3748. mlx4_pci_disable_device(dev);
  3749. }
  3750. static const struct pci_error_handlers mlx4_err_handler = {
  3751. .error_detected = mlx4_pci_err_detected,
  3752. .slot_reset = mlx4_pci_slot_reset,
  3753. .resume = mlx4_pci_resume,
  3754. };
  3755. static int __maybe_unused mlx4_suspend(struct device *dev_d)
  3756. {
  3757. struct pci_dev *pdev = to_pci_dev(dev_d);
  3758. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3759. struct mlx4_dev *dev = persist->dev;
  3760. struct devlink *devlink;
  3761. mlx4_err(dev, "suspend was called\n");
  3762. devlink = priv_to_devlink(mlx4_priv(dev));
  3763. devl_lock(devlink);
  3764. mutex_lock(&persist->interface_state_mutex);
  3765. if (persist->interface_state & MLX4_INTERFACE_STATE_UP)
  3766. mlx4_unload_one(pdev);
  3767. mutex_unlock(&persist->interface_state_mutex);
  3768. devl_unlock(devlink);
  3769. return 0;
  3770. }
  3771. static int __maybe_unused mlx4_resume(struct device *dev_d)
  3772. {
  3773. struct pci_dev *pdev = to_pci_dev(dev_d);
  3774. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3775. struct mlx4_dev *dev = persist->dev;
  3776. struct mlx4_priv *priv = mlx4_priv(dev);
  3777. int nvfs[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  3778. struct devlink *devlink;
  3779. int total_vfs;
  3780. int ret = 0;
  3781. mlx4_err(dev, "resume was called\n");
  3782. total_vfs = dev->persist->num_vfs;
  3783. memcpy(nvfs, dev->persist->nvfs, sizeof(dev->persist->nvfs));
  3784. devlink = priv_to_devlink(priv);
  3785. devl_lock(devlink);
  3786. mutex_lock(&persist->interface_state_mutex);
  3787. if (!(persist->interface_state & MLX4_INTERFACE_STATE_UP)) {
  3788. ret = mlx4_load_one(pdev, priv->pci_dev_data, total_vfs,
  3789. nvfs, priv, 1);
  3790. if (!ret) {
  3791. ret = restore_current_port_types(dev,
  3792. dev->persist->curr_port_type,
  3793. dev->persist->curr_port_poss_type);
  3794. if (ret)
  3795. mlx4_err(dev, "resume: could not restore original port types (%d)\n", ret);
  3796. }
  3797. }
  3798. mutex_unlock(&persist->interface_state_mutex);
  3799. devl_unlock(devlink);
  3800. return ret;
  3801. }
  3802. static SIMPLE_DEV_PM_OPS(mlx4_pm_ops, mlx4_suspend, mlx4_resume);
  3803. static struct pci_driver mlx4_driver = {
  3804. .name = DRV_NAME,
  3805. .id_table = mlx4_pci_table,
  3806. .probe = mlx4_init_one,
  3807. .shutdown = mlx4_shutdown,
  3808. .remove = mlx4_remove_one,
  3809. .driver.pm = &mlx4_pm_ops,
  3810. .err_handler = &mlx4_err_handler,
  3811. };
  3812. static int __init mlx4_verify_params(void)
  3813. {
  3814. if (msi_x < 0) {
  3815. pr_warn("mlx4_core: bad msi_x: %d\n", msi_x);
  3816. return -1;
  3817. }
  3818. if ((log_num_mac < 0) || (log_num_mac > 7)) {
  3819. pr_warn("mlx4_core: bad num_mac: %d\n", log_num_mac);
  3820. return -1;
  3821. }
  3822. if (log_num_vlan != 0)
  3823. pr_warn("mlx4_core: log_num_vlan - obsolete module param, using %d\n",
  3824. MLX4_LOG_NUM_VLANS);
  3825. if (use_prio != 0)
  3826. pr_warn("mlx4_core: use_prio - obsolete module param, ignored\n");
  3827. if ((log_mtts_per_seg < 0) || (log_mtts_per_seg > 7)) {
  3828. pr_warn("mlx4_core: bad log_mtts_per_seg: %d\n",
  3829. log_mtts_per_seg);
  3830. return -1;
  3831. }
  3832. /* Check if module param for ports type has legal combination */
  3833. if (port_type_array[0] == false && port_type_array[1] == true) {
  3834. pr_warn("Module parameter configuration ETH/IB is not supported. Switching to default configuration IB/IB\n");
  3835. port_type_array[0] = true;
  3836. }
  3837. if (mlx4_log_num_mgm_entry_size < -7 ||
  3838. (mlx4_log_num_mgm_entry_size > 0 &&
  3839. (mlx4_log_num_mgm_entry_size < MLX4_MIN_MGM_LOG_ENTRY_SIZE ||
  3840. mlx4_log_num_mgm_entry_size > MLX4_MAX_MGM_LOG_ENTRY_SIZE))) {
  3841. pr_warn("mlx4_core: mlx4_log_num_mgm_entry_size (%d) not in legal range (-7..0 or %d..%d)\n",
  3842. mlx4_log_num_mgm_entry_size,
  3843. MLX4_MIN_MGM_LOG_ENTRY_SIZE,
  3844. MLX4_MAX_MGM_LOG_ENTRY_SIZE);
  3845. return -1;
  3846. }
  3847. return 0;
  3848. }
  3849. static int __init mlx4_init(void)
  3850. {
  3851. int ret;
  3852. WARN_ONCE(strcmp(MLX4_ADEV_NAME, KBUILD_MODNAME),
  3853. "mlx4_core name not in sync with kernel module name");
  3854. if (mlx4_verify_params())
  3855. return -EINVAL;
  3856. mlx4_wq = create_singlethread_workqueue("mlx4");
  3857. if (!mlx4_wq)
  3858. return -ENOMEM;
  3859. ret = pci_register_driver(&mlx4_driver);
  3860. if (ret < 0)
  3861. destroy_workqueue(mlx4_wq);
  3862. return ret < 0 ? ret : 0;
  3863. }
  3864. static void __exit mlx4_cleanup(void)
  3865. {
  3866. pci_unregister_driver(&mlx4_driver);
  3867. destroy_workqueue(mlx4_wq);
  3868. }
  3869. module_init(mlx4_init);
  3870. module_exit(mlx4_cleanup);