main.c 116 KB

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