main.c 123 KB

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