main.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267
  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 || !dev->persist->num_vfs)
  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 -ENOSYS;
  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 -ENOSYS;
  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 -ENOTSUPP;
  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. cycle_t mlx4_read_clock(struct mlx4_dev *dev)
  1567. {
  1568. u32 clockhi, clocklo, clockhi1;
  1569. cycle_t 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 -ENOTSUPP;
  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 || !dev->persist->num_vfs) {
  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 != -ENOTSUPP) {
  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. return err;
  2513. }
  2514. sprintf(info->dev_mtu_name, "mlx4_port%d_mtu", port);
  2515. info->port_mtu_attr.attr.name = info->dev_mtu_name;
  2516. if (mlx4_is_mfunc(dev))
  2517. info->port_mtu_attr.attr.mode = S_IRUGO;
  2518. else {
  2519. info->port_mtu_attr.attr.mode = S_IRUGO | S_IWUSR;
  2520. info->port_mtu_attr.store = set_port_ib_mtu;
  2521. }
  2522. info->port_mtu_attr.show = show_port_ib_mtu;
  2523. sysfs_attr_init(&info->port_mtu_attr.attr);
  2524. err = device_create_file(&dev->persist->pdev->dev,
  2525. &info->port_mtu_attr);
  2526. if (err) {
  2527. mlx4_err(dev, "Failed to create mtu file for port %d\n", port);
  2528. device_remove_file(&info->dev->persist->pdev->dev,
  2529. &info->port_attr);
  2530. devlink_port_unregister(&info->devlink_port);
  2531. info->port = -1;
  2532. return err;
  2533. }
  2534. return 0;
  2535. }
  2536. static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
  2537. {
  2538. if (info->port < 0)
  2539. return;
  2540. device_remove_file(&info->dev->persist->pdev->dev, &info->port_attr);
  2541. device_remove_file(&info->dev->persist->pdev->dev,
  2542. &info->port_mtu_attr);
  2543. devlink_port_unregister(&info->devlink_port);
  2544. #ifdef CONFIG_RFS_ACCEL
  2545. free_irq_cpu_rmap(info->rmap);
  2546. info->rmap = NULL;
  2547. #endif
  2548. }
  2549. static int mlx4_init_steering(struct mlx4_dev *dev)
  2550. {
  2551. struct mlx4_priv *priv = mlx4_priv(dev);
  2552. int num_entries = dev->caps.num_ports;
  2553. int i, j;
  2554. priv->steer = kzalloc(sizeof(struct mlx4_steer) * num_entries, GFP_KERNEL);
  2555. if (!priv->steer)
  2556. return -ENOMEM;
  2557. for (i = 0; i < num_entries; i++)
  2558. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  2559. INIT_LIST_HEAD(&priv->steer[i].promisc_qps[j]);
  2560. INIT_LIST_HEAD(&priv->steer[i].steer_entries[j]);
  2561. }
  2562. return 0;
  2563. }
  2564. static void mlx4_clear_steering(struct mlx4_dev *dev)
  2565. {
  2566. struct mlx4_priv *priv = mlx4_priv(dev);
  2567. struct mlx4_steer_index *entry, *tmp_entry;
  2568. struct mlx4_promisc_qp *pqp, *tmp_pqp;
  2569. int num_entries = dev->caps.num_ports;
  2570. int i, j;
  2571. for (i = 0; i < num_entries; i++) {
  2572. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  2573. list_for_each_entry_safe(pqp, tmp_pqp,
  2574. &priv->steer[i].promisc_qps[j],
  2575. list) {
  2576. list_del(&pqp->list);
  2577. kfree(pqp);
  2578. }
  2579. list_for_each_entry_safe(entry, tmp_entry,
  2580. &priv->steer[i].steer_entries[j],
  2581. list) {
  2582. list_del(&entry->list);
  2583. list_for_each_entry_safe(pqp, tmp_pqp,
  2584. &entry->duplicates,
  2585. list) {
  2586. list_del(&pqp->list);
  2587. kfree(pqp);
  2588. }
  2589. kfree(entry);
  2590. }
  2591. }
  2592. }
  2593. kfree(priv->steer);
  2594. }
  2595. static int extended_func_num(struct pci_dev *pdev)
  2596. {
  2597. return PCI_SLOT(pdev->devfn) * 8 + PCI_FUNC(pdev->devfn);
  2598. }
  2599. #define MLX4_OWNER_BASE 0x8069c
  2600. #define MLX4_OWNER_SIZE 4
  2601. static int mlx4_get_ownership(struct mlx4_dev *dev)
  2602. {
  2603. void __iomem *owner;
  2604. u32 ret;
  2605. if (pci_channel_offline(dev->persist->pdev))
  2606. return -EIO;
  2607. owner = ioremap(pci_resource_start(dev->persist->pdev, 0) +
  2608. MLX4_OWNER_BASE,
  2609. MLX4_OWNER_SIZE);
  2610. if (!owner) {
  2611. mlx4_err(dev, "Failed to obtain ownership bit\n");
  2612. return -ENOMEM;
  2613. }
  2614. ret = readl(owner);
  2615. iounmap(owner);
  2616. return (int) !!ret;
  2617. }
  2618. static void mlx4_free_ownership(struct mlx4_dev *dev)
  2619. {
  2620. void __iomem *owner;
  2621. if (pci_channel_offline(dev->persist->pdev))
  2622. return;
  2623. owner = ioremap(pci_resource_start(dev->persist->pdev, 0) +
  2624. MLX4_OWNER_BASE,
  2625. MLX4_OWNER_SIZE);
  2626. if (!owner) {
  2627. mlx4_err(dev, "Failed to obtain ownership bit\n");
  2628. return;
  2629. }
  2630. writel(0, owner);
  2631. msleep(1000);
  2632. iounmap(owner);
  2633. }
  2634. #define SRIOV_VALID_STATE(flags) (!!((flags) & MLX4_FLAG_SRIOV) ==\
  2635. !!((flags) & MLX4_FLAG_MASTER))
  2636. static u64 mlx4_enable_sriov(struct mlx4_dev *dev, struct pci_dev *pdev,
  2637. u8 total_vfs, int existing_vfs, int reset_flow)
  2638. {
  2639. u64 dev_flags = dev->flags;
  2640. int err = 0;
  2641. int fw_enabled_sriov_vfs = min(pci_sriov_get_totalvfs(pdev),
  2642. MLX4_MAX_NUM_VF);
  2643. if (reset_flow) {
  2644. dev->dev_vfs = kcalloc(total_vfs, sizeof(*dev->dev_vfs),
  2645. GFP_KERNEL);
  2646. if (!dev->dev_vfs)
  2647. goto free_mem;
  2648. return dev_flags;
  2649. }
  2650. atomic_inc(&pf_loading);
  2651. if (dev->flags & MLX4_FLAG_SRIOV) {
  2652. if (existing_vfs != total_vfs) {
  2653. mlx4_err(dev, "SR-IOV was already enabled, but with num_vfs (%d) different than requested (%d)\n",
  2654. existing_vfs, total_vfs);
  2655. total_vfs = existing_vfs;
  2656. }
  2657. }
  2658. dev->dev_vfs = kzalloc(total_vfs * sizeof(*dev->dev_vfs), GFP_KERNEL);
  2659. if (NULL == dev->dev_vfs) {
  2660. mlx4_err(dev, "Failed to allocate memory for VFs\n");
  2661. goto disable_sriov;
  2662. }
  2663. if (!(dev->flags & MLX4_FLAG_SRIOV)) {
  2664. if (total_vfs > fw_enabled_sriov_vfs) {
  2665. mlx4_err(dev, "requested vfs (%d) > available vfs (%d). Continuing without SR_IOV\n",
  2666. total_vfs, fw_enabled_sriov_vfs);
  2667. err = -ENOMEM;
  2668. goto disable_sriov;
  2669. }
  2670. mlx4_warn(dev, "Enabling SR-IOV with %d VFs\n", total_vfs);
  2671. err = pci_enable_sriov(pdev, total_vfs);
  2672. }
  2673. if (err) {
  2674. mlx4_err(dev, "Failed to enable SR-IOV, continuing without SR-IOV (err = %d)\n",
  2675. err);
  2676. goto disable_sriov;
  2677. } else {
  2678. mlx4_warn(dev, "Running in master mode\n");
  2679. dev_flags |= MLX4_FLAG_SRIOV |
  2680. MLX4_FLAG_MASTER;
  2681. dev_flags &= ~MLX4_FLAG_SLAVE;
  2682. dev->persist->num_vfs = total_vfs;
  2683. }
  2684. return dev_flags;
  2685. disable_sriov:
  2686. atomic_dec(&pf_loading);
  2687. free_mem:
  2688. dev->persist->num_vfs = 0;
  2689. kfree(dev->dev_vfs);
  2690. dev->dev_vfs = NULL;
  2691. return dev_flags & ~MLX4_FLAG_MASTER;
  2692. }
  2693. enum {
  2694. MLX4_DEV_CAP_CHECK_NUM_VFS_ABOVE_64 = -1,
  2695. };
  2696. static int mlx4_check_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
  2697. int *nvfs)
  2698. {
  2699. int requested_vfs = nvfs[0] + nvfs[1] + nvfs[2];
  2700. /* Checking for 64 VFs as a limitation of CX2 */
  2701. if (!(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_80_VFS) &&
  2702. requested_vfs >= 64) {
  2703. mlx4_err(dev, "Requested %d VFs, but FW does not support more than 64\n",
  2704. requested_vfs);
  2705. return MLX4_DEV_CAP_CHECK_NUM_VFS_ABOVE_64;
  2706. }
  2707. return 0;
  2708. }
  2709. static int mlx4_pci_enable_device(struct mlx4_dev *dev)
  2710. {
  2711. struct pci_dev *pdev = dev->persist->pdev;
  2712. int err = 0;
  2713. mutex_lock(&dev->persist->pci_status_mutex);
  2714. if (dev->persist->pci_status == MLX4_PCI_STATUS_DISABLED) {
  2715. err = pci_enable_device(pdev);
  2716. if (!err)
  2717. dev->persist->pci_status = MLX4_PCI_STATUS_ENABLED;
  2718. }
  2719. mutex_unlock(&dev->persist->pci_status_mutex);
  2720. return err;
  2721. }
  2722. static void mlx4_pci_disable_device(struct mlx4_dev *dev)
  2723. {
  2724. struct pci_dev *pdev = dev->persist->pdev;
  2725. mutex_lock(&dev->persist->pci_status_mutex);
  2726. if (dev->persist->pci_status == MLX4_PCI_STATUS_ENABLED) {
  2727. pci_disable_device(pdev);
  2728. dev->persist->pci_status = MLX4_PCI_STATUS_DISABLED;
  2729. }
  2730. mutex_unlock(&dev->persist->pci_status_mutex);
  2731. }
  2732. static int mlx4_load_one(struct pci_dev *pdev, int pci_dev_data,
  2733. int total_vfs, int *nvfs, struct mlx4_priv *priv,
  2734. int reset_flow)
  2735. {
  2736. struct mlx4_dev *dev;
  2737. unsigned sum = 0;
  2738. int err;
  2739. int port;
  2740. int i;
  2741. struct mlx4_dev_cap *dev_cap = NULL;
  2742. int existing_vfs = 0;
  2743. dev = &priv->dev;
  2744. INIT_LIST_HEAD(&priv->ctx_list);
  2745. spin_lock_init(&priv->ctx_lock);
  2746. mutex_init(&priv->port_mutex);
  2747. mutex_init(&priv->bond_mutex);
  2748. INIT_LIST_HEAD(&priv->pgdir_list);
  2749. mutex_init(&priv->pgdir_mutex);
  2750. spin_lock_init(&priv->cmd.context_lock);
  2751. INIT_LIST_HEAD(&priv->bf_list);
  2752. mutex_init(&priv->bf_mutex);
  2753. dev->rev_id = pdev->revision;
  2754. dev->numa_node = dev_to_node(&pdev->dev);
  2755. /* Detect if this device is a virtual function */
  2756. if (pci_dev_data & MLX4_PCI_DEV_IS_VF) {
  2757. mlx4_warn(dev, "Detected virtual function - running in slave mode\n");
  2758. dev->flags |= MLX4_FLAG_SLAVE;
  2759. } else {
  2760. /* We reset the device and enable SRIOV only for physical
  2761. * devices. Try to claim ownership on the device;
  2762. * if already taken, skip -- do not allow multiple PFs */
  2763. err = mlx4_get_ownership(dev);
  2764. if (err) {
  2765. if (err < 0)
  2766. return err;
  2767. else {
  2768. mlx4_warn(dev, "Multiple PFs not yet supported - Skipping PF\n");
  2769. return -EINVAL;
  2770. }
  2771. }
  2772. atomic_set(&priv->opreq_count, 0);
  2773. INIT_WORK(&priv->opreq_task, mlx4_opreq_action);
  2774. /*
  2775. * Now reset the HCA before we touch the PCI capabilities or
  2776. * attempt a firmware command, since a boot ROM may have left
  2777. * the HCA in an undefined state.
  2778. */
  2779. err = mlx4_reset(dev);
  2780. if (err) {
  2781. mlx4_err(dev, "Failed to reset HCA, aborting\n");
  2782. goto err_sriov;
  2783. }
  2784. if (total_vfs) {
  2785. dev->flags = MLX4_FLAG_MASTER;
  2786. existing_vfs = pci_num_vf(pdev);
  2787. if (existing_vfs)
  2788. dev->flags |= MLX4_FLAG_SRIOV;
  2789. dev->persist->num_vfs = total_vfs;
  2790. }
  2791. }
  2792. /* on load remove any previous indication of internal error,
  2793. * device is up.
  2794. */
  2795. dev->persist->state = MLX4_DEVICE_STATE_UP;
  2796. slave_start:
  2797. err = mlx4_cmd_init(dev);
  2798. if (err) {
  2799. mlx4_err(dev, "Failed to init command interface, aborting\n");
  2800. goto err_sriov;
  2801. }
  2802. /* In slave functions, the communication channel must be initialized
  2803. * before posting commands. Also, init num_slaves before calling
  2804. * mlx4_init_hca */
  2805. if (mlx4_is_mfunc(dev)) {
  2806. if (mlx4_is_master(dev)) {
  2807. dev->num_slaves = MLX4_MAX_NUM_SLAVES;
  2808. } else {
  2809. dev->num_slaves = 0;
  2810. err = mlx4_multi_func_init(dev);
  2811. if (err) {
  2812. mlx4_err(dev, "Failed to init slave mfunc interface, aborting\n");
  2813. goto err_cmd;
  2814. }
  2815. }
  2816. }
  2817. err = mlx4_init_fw(dev);
  2818. if (err) {
  2819. mlx4_err(dev, "Failed to init fw, aborting.\n");
  2820. goto err_mfunc;
  2821. }
  2822. if (mlx4_is_master(dev)) {
  2823. /* when we hit the goto slave_start below, dev_cap already initialized */
  2824. if (!dev_cap) {
  2825. dev_cap = kzalloc(sizeof(*dev_cap), GFP_KERNEL);
  2826. if (!dev_cap) {
  2827. err = -ENOMEM;
  2828. goto err_fw;
  2829. }
  2830. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  2831. if (err) {
  2832. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  2833. goto err_fw;
  2834. }
  2835. if (mlx4_check_dev_cap(dev, dev_cap, nvfs))
  2836. goto err_fw;
  2837. if (!(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS)) {
  2838. u64 dev_flags = mlx4_enable_sriov(dev, pdev,
  2839. total_vfs,
  2840. existing_vfs,
  2841. reset_flow);
  2842. mlx4_close_fw(dev);
  2843. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  2844. dev->flags = dev_flags;
  2845. if (!SRIOV_VALID_STATE(dev->flags)) {
  2846. mlx4_err(dev, "Invalid SRIOV state\n");
  2847. goto err_sriov;
  2848. }
  2849. err = mlx4_reset(dev);
  2850. if (err) {
  2851. mlx4_err(dev, "Failed to reset HCA, aborting.\n");
  2852. goto err_sriov;
  2853. }
  2854. goto slave_start;
  2855. }
  2856. } else {
  2857. /* Legacy mode FW requires SRIOV to be enabled before
  2858. * doing QUERY_DEV_CAP, since max_eq's value is different if
  2859. * SRIOV is enabled.
  2860. */
  2861. memset(dev_cap, 0, sizeof(*dev_cap));
  2862. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  2863. if (err) {
  2864. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  2865. goto err_fw;
  2866. }
  2867. if (mlx4_check_dev_cap(dev, dev_cap, nvfs))
  2868. goto err_fw;
  2869. }
  2870. }
  2871. err = mlx4_init_hca(dev);
  2872. if (err) {
  2873. if (err == -EACCES) {
  2874. /* Not primary Physical function
  2875. * Running in slave mode */
  2876. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  2877. /* We're not a PF */
  2878. if (dev->flags & MLX4_FLAG_SRIOV) {
  2879. if (!existing_vfs)
  2880. pci_disable_sriov(pdev);
  2881. if (mlx4_is_master(dev) && !reset_flow)
  2882. atomic_dec(&pf_loading);
  2883. dev->flags &= ~MLX4_FLAG_SRIOV;
  2884. }
  2885. if (!mlx4_is_slave(dev))
  2886. mlx4_free_ownership(dev);
  2887. dev->flags |= MLX4_FLAG_SLAVE;
  2888. dev->flags &= ~MLX4_FLAG_MASTER;
  2889. goto slave_start;
  2890. } else
  2891. goto err_fw;
  2892. }
  2893. if (mlx4_is_master(dev) && (dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS)) {
  2894. u64 dev_flags = mlx4_enable_sriov(dev, pdev, total_vfs,
  2895. existing_vfs, reset_flow);
  2896. if ((dev->flags ^ dev_flags) & (MLX4_FLAG_MASTER | MLX4_FLAG_SLAVE)) {
  2897. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_VHCR);
  2898. dev->flags = dev_flags;
  2899. err = mlx4_cmd_init(dev);
  2900. if (err) {
  2901. /* Only VHCR is cleaned up, so could still
  2902. * send FW commands
  2903. */
  2904. mlx4_err(dev, "Failed to init VHCR command interface, aborting\n");
  2905. goto err_close;
  2906. }
  2907. } else {
  2908. dev->flags = dev_flags;
  2909. }
  2910. if (!SRIOV_VALID_STATE(dev->flags)) {
  2911. mlx4_err(dev, "Invalid SRIOV state\n");
  2912. goto err_close;
  2913. }
  2914. }
  2915. /* check if the device is functioning at its maximum possible speed.
  2916. * No return code for this call, just warn the user in case of PCI
  2917. * express device capabilities are under-satisfied by the bus.
  2918. */
  2919. if (!mlx4_is_slave(dev))
  2920. mlx4_check_pcie_caps(dev);
  2921. /* In master functions, the communication channel must be initialized
  2922. * after obtaining its address from fw */
  2923. if (mlx4_is_master(dev)) {
  2924. if (dev->caps.num_ports < 2 &&
  2925. num_vfs_argc > 1) {
  2926. err = -EINVAL;
  2927. mlx4_err(dev,
  2928. "Error: Trying to configure VFs on port 2, but HCA has only %d physical ports\n",
  2929. dev->caps.num_ports);
  2930. goto err_close;
  2931. }
  2932. memcpy(dev->persist->nvfs, nvfs, sizeof(dev->persist->nvfs));
  2933. for (i = 0;
  2934. i < sizeof(dev->persist->nvfs)/
  2935. sizeof(dev->persist->nvfs[0]); i++) {
  2936. unsigned j;
  2937. for (j = 0; j < dev->persist->nvfs[i]; ++sum, ++j) {
  2938. dev->dev_vfs[sum].min_port = i < 2 ? i + 1 : 1;
  2939. dev->dev_vfs[sum].n_ports = i < 2 ? 1 :
  2940. dev->caps.num_ports;
  2941. }
  2942. }
  2943. /* In master functions, the communication channel
  2944. * must be initialized after obtaining its address from fw
  2945. */
  2946. err = mlx4_multi_func_init(dev);
  2947. if (err) {
  2948. mlx4_err(dev, "Failed to init master mfunc interface, aborting.\n");
  2949. goto err_close;
  2950. }
  2951. }
  2952. err = mlx4_alloc_eq_table(dev);
  2953. if (err)
  2954. goto err_master_mfunc;
  2955. bitmap_zero(priv->msix_ctl.pool_bm, MAX_MSIX);
  2956. mutex_init(&priv->msix_ctl.pool_lock);
  2957. mlx4_enable_msi_x(dev);
  2958. if ((mlx4_is_mfunc(dev)) &&
  2959. !(dev->flags & MLX4_FLAG_MSI_X)) {
  2960. err = -ENOSYS;
  2961. mlx4_err(dev, "INTx is not supported in multi-function mode, aborting\n");
  2962. goto err_free_eq;
  2963. }
  2964. if (!mlx4_is_slave(dev)) {
  2965. err = mlx4_init_steering(dev);
  2966. if (err)
  2967. goto err_disable_msix;
  2968. }
  2969. err = mlx4_setup_hca(dev);
  2970. if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X) &&
  2971. !mlx4_is_mfunc(dev)) {
  2972. dev->flags &= ~MLX4_FLAG_MSI_X;
  2973. dev->caps.num_comp_vectors = 1;
  2974. pci_disable_msix(pdev);
  2975. err = mlx4_setup_hca(dev);
  2976. }
  2977. if (err)
  2978. goto err_steer;
  2979. mlx4_init_quotas(dev);
  2980. /* When PF resources are ready arm its comm channel to enable
  2981. * getting commands
  2982. */
  2983. if (mlx4_is_master(dev)) {
  2984. err = mlx4_ARM_COMM_CHANNEL(dev);
  2985. if (err) {
  2986. mlx4_err(dev, " Failed to arm comm channel eq: %x\n",
  2987. err);
  2988. goto err_steer;
  2989. }
  2990. }
  2991. for (port = 1; port <= dev->caps.num_ports; port++) {
  2992. err = mlx4_init_port_info(dev, port);
  2993. if (err)
  2994. goto err_port;
  2995. }
  2996. priv->v2p.port1 = 1;
  2997. priv->v2p.port2 = 2;
  2998. err = mlx4_register_device(dev);
  2999. if (err)
  3000. goto err_port;
  3001. mlx4_request_modules(dev);
  3002. mlx4_sense_init(dev);
  3003. mlx4_start_sense(dev);
  3004. priv->removed = 0;
  3005. if (mlx4_is_master(dev) && dev->persist->num_vfs && !reset_flow)
  3006. atomic_dec(&pf_loading);
  3007. kfree(dev_cap);
  3008. return 0;
  3009. err_port:
  3010. for (--port; port >= 1; --port)
  3011. mlx4_cleanup_port_info(&priv->port[port]);
  3012. mlx4_cleanup_default_counters(dev);
  3013. if (!mlx4_is_slave(dev))
  3014. mlx4_cleanup_counters_table(dev);
  3015. mlx4_cleanup_qp_table(dev);
  3016. mlx4_cleanup_srq_table(dev);
  3017. mlx4_cleanup_cq_table(dev);
  3018. mlx4_cmd_use_polling(dev);
  3019. mlx4_cleanup_eq_table(dev);
  3020. mlx4_cleanup_mcg_table(dev);
  3021. mlx4_cleanup_mr_table(dev);
  3022. mlx4_cleanup_xrcd_table(dev);
  3023. mlx4_cleanup_pd_table(dev);
  3024. mlx4_cleanup_uar_table(dev);
  3025. err_steer:
  3026. if (!mlx4_is_slave(dev))
  3027. mlx4_clear_steering(dev);
  3028. err_disable_msix:
  3029. if (dev->flags & MLX4_FLAG_MSI_X)
  3030. pci_disable_msix(pdev);
  3031. err_free_eq:
  3032. mlx4_free_eq_table(dev);
  3033. err_master_mfunc:
  3034. if (mlx4_is_master(dev)) {
  3035. mlx4_free_resource_tracker(dev, RES_TR_FREE_STRUCTS_ONLY);
  3036. mlx4_multi_func_cleanup(dev);
  3037. }
  3038. if (mlx4_is_slave(dev)) {
  3039. kfree(dev->caps.qp0_qkey);
  3040. kfree(dev->caps.qp0_tunnel);
  3041. kfree(dev->caps.qp0_proxy);
  3042. kfree(dev->caps.qp1_tunnel);
  3043. kfree(dev->caps.qp1_proxy);
  3044. }
  3045. err_close:
  3046. mlx4_close_hca(dev);
  3047. err_fw:
  3048. mlx4_close_fw(dev);
  3049. err_mfunc:
  3050. if (mlx4_is_slave(dev))
  3051. mlx4_multi_func_cleanup(dev);
  3052. err_cmd:
  3053. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  3054. err_sriov:
  3055. if (dev->flags & MLX4_FLAG_SRIOV && !existing_vfs) {
  3056. pci_disable_sriov(pdev);
  3057. dev->flags &= ~MLX4_FLAG_SRIOV;
  3058. }
  3059. if (mlx4_is_master(dev) && dev->persist->num_vfs && !reset_flow)
  3060. atomic_dec(&pf_loading);
  3061. kfree(priv->dev.dev_vfs);
  3062. if (!mlx4_is_slave(dev))
  3063. mlx4_free_ownership(dev);
  3064. kfree(dev_cap);
  3065. return err;
  3066. }
  3067. static int __mlx4_init_one(struct pci_dev *pdev, int pci_dev_data,
  3068. struct mlx4_priv *priv)
  3069. {
  3070. int err;
  3071. int nvfs[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  3072. int prb_vf[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  3073. const int param_map[MLX4_MAX_PORTS + 1][MLX4_MAX_PORTS + 1] = {
  3074. {2, 0, 0}, {0, 1, 2}, {0, 1, 2} };
  3075. unsigned total_vfs = 0;
  3076. unsigned int i;
  3077. pr_info(DRV_NAME ": Initializing %s\n", pci_name(pdev));
  3078. err = mlx4_pci_enable_device(&priv->dev);
  3079. if (err) {
  3080. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  3081. return err;
  3082. }
  3083. /* Due to requirement that all VFs and the PF are *guaranteed* 2 MACS
  3084. * per port, we must limit the number of VFs to 63 (since their are
  3085. * 128 MACs)
  3086. */
  3087. for (i = 0; i < sizeof(nvfs)/sizeof(nvfs[0]) && i < num_vfs_argc;
  3088. total_vfs += nvfs[param_map[num_vfs_argc - 1][i]], i++) {
  3089. nvfs[param_map[num_vfs_argc - 1][i]] = num_vfs[i];
  3090. if (nvfs[i] < 0) {
  3091. dev_err(&pdev->dev, "num_vfs module parameter cannot be negative\n");
  3092. err = -EINVAL;
  3093. goto err_disable_pdev;
  3094. }
  3095. }
  3096. for (i = 0; i < sizeof(prb_vf)/sizeof(prb_vf[0]) && i < probe_vfs_argc;
  3097. i++) {
  3098. prb_vf[param_map[probe_vfs_argc - 1][i]] = probe_vf[i];
  3099. if (prb_vf[i] < 0 || prb_vf[i] > nvfs[i]) {
  3100. dev_err(&pdev->dev, "probe_vf module parameter cannot be negative or greater than num_vfs\n");
  3101. err = -EINVAL;
  3102. goto err_disable_pdev;
  3103. }
  3104. }
  3105. if (total_vfs > MLX4_MAX_NUM_VF) {
  3106. dev_err(&pdev->dev,
  3107. "Requested more VF's (%d) than allowed by hw (%d)\n",
  3108. total_vfs, MLX4_MAX_NUM_VF);
  3109. err = -EINVAL;
  3110. goto err_disable_pdev;
  3111. }
  3112. for (i = 0; i < MLX4_MAX_PORTS; i++) {
  3113. if (nvfs[i] + nvfs[2] > MLX4_MAX_NUM_VF_P_PORT) {
  3114. dev_err(&pdev->dev,
  3115. "Requested more VF's (%d) for port (%d) than allowed by driver (%d)\n",
  3116. nvfs[i] + nvfs[2], i + 1,
  3117. MLX4_MAX_NUM_VF_P_PORT);
  3118. err = -EINVAL;
  3119. goto err_disable_pdev;
  3120. }
  3121. }
  3122. /* Check for BARs. */
  3123. if (!(pci_dev_data & MLX4_PCI_DEV_IS_VF) &&
  3124. !(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  3125. dev_err(&pdev->dev, "Missing DCS, aborting (driver_data: 0x%x, pci_resource_flags(pdev, 0):0x%lx)\n",
  3126. pci_dev_data, pci_resource_flags(pdev, 0));
  3127. err = -ENODEV;
  3128. goto err_disable_pdev;
  3129. }
  3130. if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  3131. dev_err(&pdev->dev, "Missing UAR, aborting\n");
  3132. err = -ENODEV;
  3133. goto err_disable_pdev;
  3134. }
  3135. err = pci_request_regions(pdev, DRV_NAME);
  3136. if (err) {
  3137. dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
  3138. goto err_disable_pdev;
  3139. }
  3140. pci_set_master(pdev);
  3141. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  3142. if (err) {
  3143. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
  3144. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3145. if (err) {
  3146. dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
  3147. goto err_release_regions;
  3148. }
  3149. }
  3150. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3151. if (err) {
  3152. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
  3153. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3154. if (err) {
  3155. dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, aborting\n");
  3156. goto err_release_regions;
  3157. }
  3158. }
  3159. /* Allow large DMA segments, up to the firmware limit of 1 GB */
  3160. dma_set_max_seg_size(&pdev->dev, 1024 * 1024 * 1024);
  3161. /* Detect if this device is a virtual function */
  3162. if (pci_dev_data & MLX4_PCI_DEV_IS_VF) {
  3163. /* When acting as pf, we normally skip vfs unless explicitly
  3164. * requested to probe them.
  3165. */
  3166. if (total_vfs) {
  3167. unsigned vfs_offset = 0;
  3168. for (i = 0; i < sizeof(nvfs)/sizeof(nvfs[0]) &&
  3169. vfs_offset + nvfs[i] < extended_func_num(pdev);
  3170. vfs_offset += nvfs[i], i++)
  3171. ;
  3172. if (i == sizeof(nvfs)/sizeof(nvfs[0])) {
  3173. err = -ENODEV;
  3174. goto err_release_regions;
  3175. }
  3176. if ((extended_func_num(pdev) - vfs_offset)
  3177. > prb_vf[i]) {
  3178. dev_warn(&pdev->dev, "Skipping virtual function:%d\n",
  3179. extended_func_num(pdev));
  3180. err = -ENODEV;
  3181. goto err_release_regions;
  3182. }
  3183. }
  3184. }
  3185. err = mlx4_catas_init(&priv->dev);
  3186. if (err)
  3187. goto err_release_regions;
  3188. err = mlx4_load_one(pdev, pci_dev_data, total_vfs, nvfs, priv, 0);
  3189. if (err)
  3190. goto err_catas;
  3191. return 0;
  3192. err_catas:
  3193. mlx4_catas_end(&priv->dev);
  3194. err_release_regions:
  3195. pci_release_regions(pdev);
  3196. err_disable_pdev:
  3197. mlx4_pci_disable_device(&priv->dev);
  3198. pci_set_drvdata(pdev, NULL);
  3199. return err;
  3200. }
  3201. static int mlx4_devlink_port_type_set(struct devlink_port *devlink_port,
  3202. enum devlink_port_type port_type)
  3203. {
  3204. struct mlx4_port_info *info = container_of(devlink_port,
  3205. struct mlx4_port_info,
  3206. devlink_port);
  3207. enum mlx4_port_type mlx4_port_type;
  3208. switch (port_type) {
  3209. case DEVLINK_PORT_TYPE_AUTO:
  3210. mlx4_port_type = MLX4_PORT_TYPE_AUTO;
  3211. break;
  3212. case DEVLINK_PORT_TYPE_ETH:
  3213. mlx4_port_type = MLX4_PORT_TYPE_ETH;
  3214. break;
  3215. case DEVLINK_PORT_TYPE_IB:
  3216. mlx4_port_type = MLX4_PORT_TYPE_IB;
  3217. break;
  3218. default:
  3219. return -EOPNOTSUPP;
  3220. }
  3221. return __set_port_type(info, mlx4_port_type);
  3222. }
  3223. static const struct devlink_ops mlx4_devlink_ops = {
  3224. .port_type_set = mlx4_devlink_port_type_set,
  3225. };
  3226. static int mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3227. {
  3228. struct devlink *devlink;
  3229. struct mlx4_priv *priv;
  3230. struct mlx4_dev *dev;
  3231. int ret;
  3232. printk_once(KERN_INFO "%s", mlx4_version);
  3233. devlink = devlink_alloc(&mlx4_devlink_ops, sizeof(*priv));
  3234. if (!devlink)
  3235. return -ENOMEM;
  3236. priv = devlink_priv(devlink);
  3237. dev = &priv->dev;
  3238. dev->persist = kzalloc(sizeof(*dev->persist), GFP_KERNEL);
  3239. if (!dev->persist) {
  3240. ret = -ENOMEM;
  3241. goto err_devlink_free;
  3242. }
  3243. dev->persist->pdev = pdev;
  3244. dev->persist->dev = dev;
  3245. pci_set_drvdata(pdev, dev->persist);
  3246. priv->pci_dev_data = id->driver_data;
  3247. mutex_init(&dev->persist->device_state_mutex);
  3248. mutex_init(&dev->persist->interface_state_mutex);
  3249. mutex_init(&dev->persist->pci_status_mutex);
  3250. ret = devlink_register(devlink, &pdev->dev);
  3251. if (ret)
  3252. goto err_persist_free;
  3253. ret = __mlx4_init_one(pdev, id->driver_data, priv);
  3254. if (ret)
  3255. goto err_devlink_unregister;
  3256. pci_save_state(pdev);
  3257. return 0;
  3258. err_devlink_unregister:
  3259. devlink_unregister(devlink);
  3260. err_persist_free:
  3261. kfree(dev->persist);
  3262. err_devlink_free:
  3263. devlink_free(devlink);
  3264. return ret;
  3265. }
  3266. static void mlx4_clean_dev(struct mlx4_dev *dev)
  3267. {
  3268. struct mlx4_dev_persistent *persist = dev->persist;
  3269. struct mlx4_priv *priv = mlx4_priv(dev);
  3270. unsigned long flags = (dev->flags & RESET_PERSIST_MASK_FLAGS);
  3271. memset(priv, 0, sizeof(*priv));
  3272. priv->dev.persist = persist;
  3273. priv->dev.flags = flags;
  3274. }
  3275. static void mlx4_unload_one(struct pci_dev *pdev)
  3276. {
  3277. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3278. struct mlx4_dev *dev = persist->dev;
  3279. struct mlx4_priv *priv = mlx4_priv(dev);
  3280. int pci_dev_data;
  3281. int p, i;
  3282. if (priv->removed)
  3283. return;
  3284. /* saving current ports type for further use */
  3285. for (i = 0; i < dev->caps.num_ports; i++) {
  3286. dev->persist->curr_port_type[i] = dev->caps.port_type[i + 1];
  3287. dev->persist->curr_port_poss_type[i] = dev->caps.
  3288. possible_type[i + 1];
  3289. }
  3290. pci_dev_data = priv->pci_dev_data;
  3291. mlx4_stop_sense(dev);
  3292. mlx4_unregister_device(dev);
  3293. for (p = 1; p <= dev->caps.num_ports; p++) {
  3294. mlx4_cleanup_port_info(&priv->port[p]);
  3295. mlx4_CLOSE_PORT(dev, p);
  3296. }
  3297. if (mlx4_is_master(dev))
  3298. mlx4_free_resource_tracker(dev,
  3299. RES_TR_FREE_SLAVES_ONLY);
  3300. mlx4_cleanup_default_counters(dev);
  3301. if (!mlx4_is_slave(dev))
  3302. mlx4_cleanup_counters_table(dev);
  3303. mlx4_cleanup_qp_table(dev);
  3304. mlx4_cleanup_srq_table(dev);
  3305. mlx4_cleanup_cq_table(dev);
  3306. mlx4_cmd_use_polling(dev);
  3307. mlx4_cleanup_eq_table(dev);
  3308. mlx4_cleanup_mcg_table(dev);
  3309. mlx4_cleanup_mr_table(dev);
  3310. mlx4_cleanup_xrcd_table(dev);
  3311. mlx4_cleanup_pd_table(dev);
  3312. if (mlx4_is_master(dev))
  3313. mlx4_free_resource_tracker(dev,
  3314. RES_TR_FREE_STRUCTS_ONLY);
  3315. iounmap(priv->kar);
  3316. mlx4_uar_free(dev, &priv->driver_uar);
  3317. mlx4_cleanup_uar_table(dev);
  3318. if (!mlx4_is_slave(dev))
  3319. mlx4_clear_steering(dev);
  3320. mlx4_free_eq_table(dev);
  3321. if (mlx4_is_master(dev))
  3322. mlx4_multi_func_cleanup(dev);
  3323. mlx4_close_hca(dev);
  3324. mlx4_close_fw(dev);
  3325. if (mlx4_is_slave(dev))
  3326. mlx4_multi_func_cleanup(dev);
  3327. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  3328. if (dev->flags & MLX4_FLAG_MSI_X)
  3329. pci_disable_msix(pdev);
  3330. if (!mlx4_is_slave(dev))
  3331. mlx4_free_ownership(dev);
  3332. kfree(dev->caps.qp0_qkey);
  3333. kfree(dev->caps.qp0_tunnel);
  3334. kfree(dev->caps.qp0_proxy);
  3335. kfree(dev->caps.qp1_tunnel);
  3336. kfree(dev->caps.qp1_proxy);
  3337. kfree(dev->dev_vfs);
  3338. mlx4_clean_dev(dev);
  3339. priv->pci_dev_data = pci_dev_data;
  3340. priv->removed = 1;
  3341. }
  3342. static void mlx4_remove_one(struct pci_dev *pdev)
  3343. {
  3344. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3345. struct mlx4_dev *dev = persist->dev;
  3346. struct mlx4_priv *priv = mlx4_priv(dev);
  3347. struct devlink *devlink = priv_to_devlink(priv);
  3348. int active_vfs = 0;
  3349. if (mlx4_is_slave(dev))
  3350. persist->interface_state |= MLX4_INTERFACE_STATE_NOWAIT;
  3351. mutex_lock(&persist->interface_state_mutex);
  3352. persist->interface_state |= MLX4_INTERFACE_STATE_DELETION;
  3353. mutex_unlock(&persist->interface_state_mutex);
  3354. /* Disabling SR-IOV is not allowed while there are active vf's */
  3355. if (mlx4_is_master(dev) && dev->flags & MLX4_FLAG_SRIOV) {
  3356. active_vfs = mlx4_how_many_lives_vf(dev);
  3357. if (active_vfs) {
  3358. pr_warn("Removing PF when there are active VF's !!\n");
  3359. pr_warn("Will not disable SR-IOV.\n");
  3360. }
  3361. }
  3362. /* device marked to be under deletion running now without the lock
  3363. * letting other tasks to be terminated
  3364. */
  3365. if (persist->interface_state & MLX4_INTERFACE_STATE_UP)
  3366. mlx4_unload_one(pdev);
  3367. else
  3368. mlx4_info(dev, "%s: interface is down\n", __func__);
  3369. mlx4_catas_end(dev);
  3370. if (dev->flags & MLX4_FLAG_SRIOV && !active_vfs) {
  3371. mlx4_warn(dev, "Disabling SR-IOV\n");
  3372. pci_disable_sriov(pdev);
  3373. }
  3374. pci_release_regions(pdev);
  3375. mlx4_pci_disable_device(dev);
  3376. devlink_unregister(devlink);
  3377. kfree(dev->persist);
  3378. devlink_free(devlink);
  3379. pci_set_drvdata(pdev, NULL);
  3380. }
  3381. static int restore_current_port_types(struct mlx4_dev *dev,
  3382. enum mlx4_port_type *types,
  3383. enum mlx4_port_type *poss_types)
  3384. {
  3385. struct mlx4_priv *priv = mlx4_priv(dev);
  3386. int err, i;
  3387. mlx4_stop_sense(dev);
  3388. mutex_lock(&priv->port_mutex);
  3389. for (i = 0; i < dev->caps.num_ports; i++)
  3390. dev->caps.possible_type[i + 1] = poss_types[i];
  3391. err = mlx4_change_port_types(dev, types);
  3392. mlx4_start_sense(dev);
  3393. mutex_unlock(&priv->port_mutex);
  3394. return err;
  3395. }
  3396. int mlx4_restart_one(struct pci_dev *pdev)
  3397. {
  3398. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3399. struct mlx4_dev *dev = persist->dev;
  3400. struct mlx4_priv *priv = mlx4_priv(dev);
  3401. int nvfs[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  3402. int pci_dev_data, err, total_vfs;
  3403. pci_dev_data = priv->pci_dev_data;
  3404. total_vfs = dev->persist->num_vfs;
  3405. memcpy(nvfs, dev->persist->nvfs, sizeof(dev->persist->nvfs));
  3406. mlx4_unload_one(pdev);
  3407. err = mlx4_load_one(pdev, pci_dev_data, total_vfs, nvfs, priv, 1);
  3408. if (err) {
  3409. mlx4_err(dev, "%s: ERROR: mlx4_load_one failed, pci_name=%s, err=%d\n",
  3410. __func__, pci_name(pdev), err);
  3411. return err;
  3412. }
  3413. err = restore_current_port_types(dev, dev->persist->curr_port_type,
  3414. dev->persist->curr_port_poss_type);
  3415. if (err)
  3416. mlx4_err(dev, "could not restore original port types (%d)\n",
  3417. err);
  3418. return err;
  3419. }
  3420. #define MLX_SP(id) { PCI_VDEVICE(MELLANOX, id), MLX4_PCI_DEV_FORCE_SENSE_PORT }
  3421. #define MLX_VF(id) { PCI_VDEVICE(MELLANOX, id), MLX4_PCI_DEV_IS_VF }
  3422. #define MLX_GN(id) { PCI_VDEVICE(MELLANOX, id), 0 }
  3423. static const struct pci_device_id mlx4_pci_table[] = {
  3424. /* MT25408 "Hermon" */
  3425. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_SDR), /* SDR */
  3426. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_DDR), /* DDR */
  3427. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_QDR), /* QDR */
  3428. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_DDR_GEN2), /* DDR Gen2 */
  3429. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_QDR_GEN2), /* QDR Gen2 */
  3430. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_EN), /* EN 10GigE */
  3431. MLX_SP(PCI_DEVICE_ID_MELLANOX_HERMON_EN_GEN2), /* EN 10GigE Gen2 */
  3432. /* MT25458 ConnectX EN 10GBASE-T */
  3433. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX_EN),
  3434. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX_EN_T_GEN2), /* Gen2 */
  3435. /* MT26468 ConnectX EN 10GigE PCIe Gen2*/
  3436. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX_EN_GEN2),
  3437. /* MT26438 ConnectX EN 40GigE PCIe Gen2 5GT/s */
  3438. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX_EN_5_GEN2),
  3439. /* MT26478 ConnectX2 40GigE PCIe Gen2 */
  3440. MLX_SP(PCI_DEVICE_ID_MELLANOX_CONNECTX2),
  3441. /* MT25400 Family [ConnectX-2] */
  3442. MLX_VF(0x1002), /* Virtual Function */
  3443. /* MT27500 Family [ConnectX-3] */
  3444. MLX_GN(PCI_DEVICE_ID_MELLANOX_CONNECTX3),
  3445. MLX_VF(0x1004), /* Virtual Function */
  3446. MLX_GN(0x1005), /* MT27510 Family */
  3447. MLX_GN(0x1006), /* MT27511 Family */
  3448. MLX_GN(PCI_DEVICE_ID_MELLANOX_CONNECTX3_PRO), /* MT27520 Family */
  3449. MLX_GN(0x1008), /* MT27521 Family */
  3450. MLX_GN(0x1009), /* MT27530 Family */
  3451. MLX_GN(0x100a), /* MT27531 Family */
  3452. MLX_GN(0x100b), /* MT27540 Family */
  3453. MLX_GN(0x100c), /* MT27541 Family */
  3454. MLX_GN(0x100d), /* MT27550 Family */
  3455. MLX_GN(0x100e), /* MT27551 Family */
  3456. MLX_GN(0x100f), /* MT27560 Family */
  3457. MLX_GN(0x1010), /* MT27561 Family */
  3458. /*
  3459. * See the mellanox_check_broken_intx_masking() quirk when
  3460. * adding devices
  3461. */
  3462. { 0, }
  3463. };
  3464. MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
  3465. static pci_ers_result_t mlx4_pci_err_detected(struct pci_dev *pdev,
  3466. pci_channel_state_t state)
  3467. {
  3468. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3469. mlx4_err(persist->dev, "mlx4_pci_err_detected was called\n");
  3470. mlx4_enter_error_state(persist);
  3471. mutex_lock(&persist->interface_state_mutex);
  3472. if (persist->interface_state & MLX4_INTERFACE_STATE_UP)
  3473. mlx4_unload_one(pdev);
  3474. mutex_unlock(&persist->interface_state_mutex);
  3475. if (state == pci_channel_io_perm_failure)
  3476. return PCI_ERS_RESULT_DISCONNECT;
  3477. mlx4_pci_disable_device(persist->dev);
  3478. return PCI_ERS_RESULT_NEED_RESET;
  3479. }
  3480. static pci_ers_result_t mlx4_pci_slot_reset(struct pci_dev *pdev)
  3481. {
  3482. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3483. struct mlx4_dev *dev = persist->dev;
  3484. int err;
  3485. mlx4_err(dev, "mlx4_pci_slot_reset was called\n");
  3486. err = mlx4_pci_enable_device(dev);
  3487. if (err) {
  3488. mlx4_err(dev, "Can not re-enable device, err=%d\n", err);
  3489. return PCI_ERS_RESULT_DISCONNECT;
  3490. }
  3491. pci_set_master(pdev);
  3492. pci_restore_state(pdev);
  3493. pci_save_state(pdev);
  3494. return PCI_ERS_RESULT_RECOVERED;
  3495. }
  3496. static void mlx4_pci_resume(struct pci_dev *pdev)
  3497. {
  3498. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3499. struct mlx4_dev *dev = persist->dev;
  3500. struct mlx4_priv *priv = mlx4_priv(dev);
  3501. int nvfs[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  3502. int total_vfs;
  3503. int err;
  3504. mlx4_err(dev, "%s was called\n", __func__);
  3505. total_vfs = dev->persist->num_vfs;
  3506. memcpy(nvfs, dev->persist->nvfs, sizeof(dev->persist->nvfs));
  3507. mutex_lock(&persist->interface_state_mutex);
  3508. if (!(persist->interface_state & MLX4_INTERFACE_STATE_UP)) {
  3509. err = mlx4_load_one(pdev, priv->pci_dev_data, total_vfs, nvfs,
  3510. priv, 1);
  3511. if (err) {
  3512. mlx4_err(dev, "%s: mlx4_load_one failed, err=%d\n",
  3513. __func__, err);
  3514. goto end;
  3515. }
  3516. err = restore_current_port_types(dev, dev->persist->
  3517. curr_port_type, dev->persist->
  3518. curr_port_poss_type);
  3519. if (err)
  3520. mlx4_err(dev, "could not restore original port types (%d)\n", err);
  3521. }
  3522. end:
  3523. mutex_unlock(&persist->interface_state_mutex);
  3524. }
  3525. static void mlx4_shutdown(struct pci_dev *pdev)
  3526. {
  3527. struct mlx4_dev_persistent *persist = pci_get_drvdata(pdev);
  3528. mlx4_info(persist->dev, "mlx4_shutdown was called\n");
  3529. mutex_lock(&persist->interface_state_mutex);
  3530. if (persist->interface_state & MLX4_INTERFACE_STATE_UP)
  3531. mlx4_unload_one(pdev);
  3532. mutex_unlock(&persist->interface_state_mutex);
  3533. }
  3534. static const struct pci_error_handlers mlx4_err_handler = {
  3535. .error_detected = mlx4_pci_err_detected,
  3536. .slot_reset = mlx4_pci_slot_reset,
  3537. .resume = mlx4_pci_resume,
  3538. };
  3539. static struct pci_driver mlx4_driver = {
  3540. .name = DRV_NAME,
  3541. .id_table = mlx4_pci_table,
  3542. .probe = mlx4_init_one,
  3543. .shutdown = mlx4_shutdown,
  3544. .remove = mlx4_remove_one,
  3545. .err_handler = &mlx4_err_handler,
  3546. };
  3547. static int __init mlx4_verify_params(void)
  3548. {
  3549. if ((log_num_mac < 0) || (log_num_mac > 7)) {
  3550. pr_warn("mlx4_core: bad num_mac: %d\n", log_num_mac);
  3551. return -1;
  3552. }
  3553. if (log_num_vlan != 0)
  3554. pr_warn("mlx4_core: log_num_vlan - obsolete module param, using %d\n",
  3555. MLX4_LOG_NUM_VLANS);
  3556. if (use_prio != 0)
  3557. pr_warn("mlx4_core: use_prio - obsolete module param, ignored\n");
  3558. if ((log_mtts_per_seg < 1) || (log_mtts_per_seg > 7)) {
  3559. pr_warn("mlx4_core: bad log_mtts_per_seg: %d\n",
  3560. log_mtts_per_seg);
  3561. return -1;
  3562. }
  3563. /* Check if module param for ports type has legal combination */
  3564. if (port_type_array[0] == false && port_type_array[1] == true) {
  3565. pr_warn("Module parameter configuration ETH/IB is not supported. Switching to default configuration IB/IB\n");
  3566. port_type_array[0] = true;
  3567. }
  3568. if (mlx4_log_num_mgm_entry_size < -7 ||
  3569. (mlx4_log_num_mgm_entry_size > 0 &&
  3570. (mlx4_log_num_mgm_entry_size < MLX4_MIN_MGM_LOG_ENTRY_SIZE ||
  3571. mlx4_log_num_mgm_entry_size > MLX4_MAX_MGM_LOG_ENTRY_SIZE))) {
  3572. pr_warn("mlx4_core: mlx4_log_num_mgm_entry_size (%d) not in legal range (-7..0 or %d..%d)\n",
  3573. mlx4_log_num_mgm_entry_size,
  3574. MLX4_MIN_MGM_LOG_ENTRY_SIZE,
  3575. MLX4_MAX_MGM_LOG_ENTRY_SIZE);
  3576. return -1;
  3577. }
  3578. return 0;
  3579. }
  3580. static int __init mlx4_init(void)
  3581. {
  3582. int ret;
  3583. if (mlx4_verify_params())
  3584. return -EINVAL;
  3585. mlx4_wq = create_singlethread_workqueue("mlx4");
  3586. if (!mlx4_wq)
  3587. return -ENOMEM;
  3588. ret = pci_register_driver(&mlx4_driver);
  3589. if (ret < 0)
  3590. destroy_workqueue(mlx4_wq);
  3591. return ret < 0 ? ret : 0;
  3592. }
  3593. static void __exit mlx4_cleanup(void)
  3594. {
  3595. pci_unregister_driver(&mlx4_driver);
  3596. destroy_workqueue(mlx4_wq);
  3597. }
  3598. module_init(mlx4_init);
  3599. module_exit(mlx4_cleanup);