main.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357
  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/mlx4/device.h>
  45. #include <linux/mlx4/doorbell.h>
  46. #include "mlx4.h"
  47. #include "fw.h"
  48. #include "icm.h"
  49. MODULE_AUTHOR("Roland Dreier");
  50. MODULE_DESCRIPTION("Mellanox ConnectX HCA low-level driver");
  51. MODULE_LICENSE("Dual BSD/GPL");
  52. MODULE_VERSION(DRV_VERSION);
  53. struct workqueue_struct *mlx4_wq;
  54. #ifdef CONFIG_MLX4_DEBUG
  55. int mlx4_debug_level = 0;
  56. module_param_named(debug_level, mlx4_debug_level, int, 0644);
  57. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  58. #endif /* CONFIG_MLX4_DEBUG */
  59. #ifdef CONFIG_PCI_MSI
  60. static int msi_x = 1;
  61. module_param(msi_x, int, 0444);
  62. MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
  63. #else /* CONFIG_PCI_MSI */
  64. #define msi_x (0)
  65. #endif /* CONFIG_PCI_MSI */
  66. static uint8_t num_vfs[3] = {0, 0, 0};
  67. static int num_vfs_argc;
  68. module_param_array(num_vfs, byte , &num_vfs_argc, 0444);
  69. MODULE_PARM_DESC(num_vfs, "enable #num_vfs functions if num_vfs > 0\n"
  70. "num_vfs=port1,port2,port1+2");
  71. static uint8_t probe_vf[3] = {0, 0, 0};
  72. static int probe_vfs_argc;
  73. module_param_array(probe_vf, byte, &probe_vfs_argc, 0444);
  74. MODULE_PARM_DESC(probe_vf, "number of vfs to probe by pf driver (num_vfs > 0)\n"
  75. "probe_vf=port1,port2,port1+2");
  76. int mlx4_log_num_mgm_entry_size = MLX4_DEFAULT_MGM_LOG_ENTRY_SIZE;
  77. module_param_named(log_num_mgm_entry_size,
  78. mlx4_log_num_mgm_entry_size, int, 0444);
  79. MODULE_PARM_DESC(log_num_mgm_entry_size, "log mgm size, that defines the num"
  80. " of qp per mcg, for example:"
  81. " 10 gives 248.range: 7 <="
  82. " log_num_mgm_entry_size <= 12."
  83. " To activate device managed"
  84. " flow steering when available, set to -1");
  85. static bool enable_64b_cqe_eqe = true;
  86. module_param(enable_64b_cqe_eqe, bool, 0444);
  87. MODULE_PARM_DESC(enable_64b_cqe_eqe,
  88. "Enable 64 byte CQEs/EQEs when the FW supports this (default: True)");
  89. #define PF_CONTEXT_BEHAVIOUR_MASK (MLX4_FUNC_CAP_64B_EQE_CQE | \
  90. MLX4_FUNC_CAP_EQE_CQE_STRIDE | \
  91. MLX4_FUNC_CAP_DMFS_A0_STATIC)
  92. static char mlx4_version[] =
  93. DRV_NAME ": Mellanox ConnectX core driver v"
  94. DRV_VERSION " (" DRV_RELDATE ")\n";
  95. static struct mlx4_profile default_profile = {
  96. .num_qp = 1 << 18,
  97. .num_srq = 1 << 16,
  98. .rdmarc_per_qp = 1 << 4,
  99. .num_cq = 1 << 16,
  100. .num_mcg = 1 << 13,
  101. .num_mpt = 1 << 19,
  102. .num_mtt = 1 << 20, /* It is really num mtt segements */
  103. };
  104. static struct mlx4_profile low_mem_profile = {
  105. .num_qp = 1 << 17,
  106. .num_srq = 1 << 6,
  107. .rdmarc_per_qp = 1 << 4,
  108. .num_cq = 1 << 8,
  109. .num_mcg = 1 << 8,
  110. .num_mpt = 1 << 9,
  111. .num_mtt = 1 << 7,
  112. };
  113. static int log_num_mac = 7;
  114. module_param_named(log_num_mac, log_num_mac, int, 0444);
  115. MODULE_PARM_DESC(log_num_mac, "Log2 max number of MACs per ETH port (1-7)");
  116. static int log_num_vlan;
  117. module_param_named(log_num_vlan, log_num_vlan, int, 0444);
  118. MODULE_PARM_DESC(log_num_vlan, "Log2 max number of VLANs per ETH port (0-7)");
  119. /* Log2 max number of VLANs per ETH port (0-7) */
  120. #define MLX4_LOG_NUM_VLANS 7
  121. #define MLX4_MIN_LOG_NUM_VLANS 0
  122. #define MLX4_MIN_LOG_NUM_MAC 1
  123. static bool use_prio;
  124. module_param_named(use_prio, use_prio, bool, 0444);
  125. MODULE_PARM_DESC(use_prio, "Enable steering by VLAN priority on ETH ports (deprecated)");
  126. int log_mtts_per_seg = ilog2(MLX4_MTT_ENTRY_PER_SEG);
  127. module_param_named(log_mtts_per_seg, log_mtts_per_seg, int, 0444);
  128. MODULE_PARM_DESC(log_mtts_per_seg, "Log2 number of MTT entries per segment (1-7)");
  129. static int port_type_array[2] = {MLX4_PORT_TYPE_NONE, MLX4_PORT_TYPE_NONE};
  130. static int arr_argc = 2;
  131. module_param_array(port_type_array, int, &arr_argc, 0444);
  132. MODULE_PARM_DESC(port_type_array, "Array of port types: HW_DEFAULT (0) is default "
  133. "1 for IB, 2 for Ethernet");
  134. struct mlx4_port_config {
  135. struct list_head list;
  136. enum mlx4_port_type port_type[MLX4_MAX_PORTS + 1];
  137. struct pci_dev *pdev;
  138. };
  139. static atomic_t pf_loading = ATOMIC_INIT(0);
  140. int mlx4_check_port_params(struct mlx4_dev *dev,
  141. enum mlx4_port_type *port_type)
  142. {
  143. int i;
  144. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
  145. for (i = 0; i < dev->caps.num_ports - 1; i++) {
  146. if (port_type[i] != port_type[i + 1]) {
  147. mlx4_err(dev, "Only same port types supported on this HCA, aborting\n");
  148. return -EINVAL;
  149. }
  150. }
  151. }
  152. for (i = 0; i < dev->caps.num_ports; i++) {
  153. if (!(port_type[i] & dev->caps.supported_type[i+1])) {
  154. mlx4_err(dev, "Requested port type for port %d is not supported on this HCA\n",
  155. i + 1);
  156. return -EINVAL;
  157. }
  158. }
  159. return 0;
  160. }
  161. static void mlx4_set_port_mask(struct mlx4_dev *dev)
  162. {
  163. int i;
  164. for (i = 1; i <= dev->caps.num_ports; ++i)
  165. dev->caps.port_mask[i] = dev->caps.port_type[i];
  166. }
  167. enum {
  168. MLX4_QUERY_FUNC_NUM_SYS_EQS = 1 << 0,
  169. };
  170. static int mlx4_query_func(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
  171. {
  172. int err = 0;
  173. struct mlx4_func func;
  174. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS) {
  175. err = mlx4_QUERY_FUNC(dev, &func, 0);
  176. if (err) {
  177. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  178. return err;
  179. }
  180. dev_cap->max_eqs = func.max_eq;
  181. dev_cap->reserved_eqs = func.rsvd_eqs;
  182. dev_cap->reserved_uars = func.rsvd_uars;
  183. err |= MLX4_QUERY_FUNC_NUM_SYS_EQS;
  184. }
  185. return err;
  186. }
  187. static void mlx4_enable_cqe_eqe_stride(struct mlx4_dev *dev)
  188. {
  189. struct mlx4_caps *dev_cap = &dev->caps;
  190. /* FW not supporting or cancelled by user */
  191. if (!(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_EQE_STRIDE) ||
  192. !(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_CQE_STRIDE))
  193. return;
  194. /* Must have 64B CQE_EQE enabled by FW to use bigger stride
  195. * When FW has NCSI it may decide not to report 64B CQE/EQEs
  196. */
  197. if (!(dev_cap->flags & MLX4_DEV_CAP_FLAG_64B_EQE) ||
  198. !(dev_cap->flags & MLX4_DEV_CAP_FLAG_64B_CQE)) {
  199. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_CQE_STRIDE;
  200. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_EQE_STRIDE;
  201. return;
  202. }
  203. if (cache_line_size() == 128 || cache_line_size() == 256) {
  204. mlx4_dbg(dev, "Enabling CQE stride cacheLine supported\n");
  205. /* Changing the real data inside CQE size to 32B */
  206. dev_cap->flags &= ~MLX4_DEV_CAP_FLAG_64B_CQE;
  207. dev_cap->flags &= ~MLX4_DEV_CAP_FLAG_64B_EQE;
  208. if (mlx4_is_master(dev))
  209. dev_cap->function_caps |= MLX4_FUNC_CAP_EQE_CQE_STRIDE;
  210. } else {
  211. mlx4_dbg(dev, "Disabling CQE stride cacheLine unsupported\n");
  212. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_CQE_STRIDE;
  213. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_EQE_STRIDE;
  214. }
  215. }
  216. static int _mlx4_dev_port(struct mlx4_dev *dev, int port,
  217. struct mlx4_port_cap *port_cap)
  218. {
  219. dev->caps.vl_cap[port] = port_cap->max_vl;
  220. dev->caps.ib_mtu_cap[port] = port_cap->ib_mtu;
  221. dev->phys_caps.gid_phys_table_len[port] = port_cap->max_gids;
  222. dev->phys_caps.pkey_phys_table_len[port] = port_cap->max_pkeys;
  223. /* set gid and pkey table operating lengths by default
  224. * to non-sriov values
  225. */
  226. dev->caps.gid_table_len[port] = port_cap->max_gids;
  227. dev->caps.pkey_table_len[port] = port_cap->max_pkeys;
  228. dev->caps.port_width_cap[port] = port_cap->max_port_width;
  229. dev->caps.eth_mtu_cap[port] = port_cap->eth_mtu;
  230. dev->caps.def_mac[port] = port_cap->def_mac;
  231. dev->caps.supported_type[port] = port_cap->supported_port_types;
  232. dev->caps.suggested_type[port] = port_cap->suggested_type;
  233. dev->caps.default_sense[port] = port_cap->default_sense;
  234. dev->caps.trans_type[port] = port_cap->trans_type;
  235. dev->caps.vendor_oui[port] = port_cap->vendor_oui;
  236. dev->caps.wavelength[port] = port_cap->wavelength;
  237. dev->caps.trans_code[port] = port_cap->trans_code;
  238. return 0;
  239. }
  240. static int mlx4_dev_port(struct mlx4_dev *dev, int port,
  241. struct mlx4_port_cap *port_cap)
  242. {
  243. int err = 0;
  244. err = mlx4_QUERY_PORT(dev, port, port_cap);
  245. if (err)
  246. mlx4_err(dev, "QUERY_PORT command failed.\n");
  247. return err;
  248. }
  249. #define MLX4_A0_STEERING_TABLE_SIZE 256
  250. static int mlx4_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
  251. {
  252. int err;
  253. int i;
  254. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  255. if (err) {
  256. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting\n");
  257. return err;
  258. }
  259. mlx4_dev_cap_dump(dev, dev_cap);
  260. if (dev_cap->min_page_sz > PAGE_SIZE) {
  261. mlx4_err(dev, "HCA minimum page size of %d bigger than kernel PAGE_SIZE of %ld, aborting\n",
  262. dev_cap->min_page_sz, PAGE_SIZE);
  263. return -ENODEV;
  264. }
  265. if (dev_cap->num_ports > MLX4_MAX_PORTS) {
  266. mlx4_err(dev, "HCA has %d ports, but we only support %d, aborting\n",
  267. dev_cap->num_ports, MLX4_MAX_PORTS);
  268. return -ENODEV;
  269. }
  270. if (dev_cap->uar_size > pci_resource_len(dev->pdev, 2)) {
  271. mlx4_err(dev, "HCA reported UAR size of 0x%x bigger than PCI resource 2 size of 0x%llx, aborting\n",
  272. dev_cap->uar_size,
  273. (unsigned long long) pci_resource_len(dev->pdev, 2));
  274. return -ENODEV;
  275. }
  276. dev->caps.num_ports = dev_cap->num_ports;
  277. dev->caps.num_sys_eqs = dev_cap->num_sys_eqs;
  278. dev->phys_caps.num_phys_eqs = dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS ?
  279. dev->caps.num_sys_eqs :
  280. MLX4_MAX_EQ_NUM;
  281. for (i = 1; i <= dev->caps.num_ports; ++i) {
  282. err = _mlx4_dev_port(dev, i, dev_cap->port_cap + i);
  283. if (err) {
  284. mlx4_err(dev, "QUERY_PORT command failed, aborting\n");
  285. return err;
  286. }
  287. }
  288. dev->caps.uar_page_size = PAGE_SIZE;
  289. dev->caps.num_uars = dev_cap->uar_size / PAGE_SIZE;
  290. dev->caps.local_ca_ack_delay = dev_cap->local_ca_ack_delay;
  291. dev->caps.bf_reg_size = dev_cap->bf_reg_size;
  292. dev->caps.bf_regs_per_page = dev_cap->bf_regs_per_page;
  293. dev->caps.max_sq_sg = dev_cap->max_sq_sg;
  294. dev->caps.max_rq_sg = dev_cap->max_rq_sg;
  295. dev->caps.max_wqes = dev_cap->max_qp_sz;
  296. dev->caps.max_qp_init_rdma = dev_cap->max_requester_per_qp;
  297. dev->caps.max_srq_wqes = dev_cap->max_srq_sz;
  298. dev->caps.max_srq_sge = dev_cap->max_rq_sg - 1;
  299. dev->caps.reserved_srqs = dev_cap->reserved_srqs;
  300. dev->caps.max_sq_desc_sz = dev_cap->max_sq_desc_sz;
  301. dev->caps.max_rq_desc_sz = dev_cap->max_rq_desc_sz;
  302. /*
  303. * Subtract 1 from the limit because we need to allocate a
  304. * spare CQE so the HCA HW can tell the difference between an
  305. * empty CQ and a full CQ.
  306. */
  307. dev->caps.max_cqes = dev_cap->max_cq_sz - 1;
  308. dev->caps.reserved_cqs = dev_cap->reserved_cqs;
  309. dev->caps.reserved_eqs = dev_cap->reserved_eqs;
  310. dev->caps.reserved_mtts = dev_cap->reserved_mtts;
  311. dev->caps.reserved_mrws = dev_cap->reserved_mrws;
  312. /* The first 128 UARs are used for EQ doorbells */
  313. dev->caps.reserved_uars = max_t(int, 128, dev_cap->reserved_uars);
  314. dev->caps.reserved_pds = dev_cap->reserved_pds;
  315. dev->caps.reserved_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
  316. dev_cap->reserved_xrcds : 0;
  317. dev->caps.max_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
  318. dev_cap->max_xrcds : 0;
  319. dev->caps.mtt_entry_sz = dev_cap->mtt_entry_sz;
  320. dev->caps.max_msg_sz = dev_cap->max_msg_sz;
  321. dev->caps.page_size_cap = ~(u32) (dev_cap->min_page_sz - 1);
  322. dev->caps.flags = dev_cap->flags;
  323. dev->caps.flags2 = dev_cap->flags2;
  324. dev->caps.bmme_flags = dev_cap->bmme_flags;
  325. dev->caps.reserved_lkey = dev_cap->reserved_lkey;
  326. dev->caps.stat_rate_support = dev_cap->stat_rate_support;
  327. dev->caps.max_gso_sz = dev_cap->max_gso_sz;
  328. dev->caps.max_rss_tbl_sz = dev_cap->max_rss_tbl_sz;
  329. /* Sense port always allowed on supported devices for ConnectX-1 and -2 */
  330. if (mlx4_priv(dev)->pci_dev_data & MLX4_PCI_DEV_FORCE_SENSE_PORT)
  331. dev->caps.flags |= MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
  332. /* Don't do sense port on multifunction devices (for now at least) */
  333. if (mlx4_is_mfunc(dev))
  334. dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
  335. if (mlx4_low_memory_profile()) {
  336. dev->caps.log_num_macs = MLX4_MIN_LOG_NUM_MAC;
  337. dev->caps.log_num_vlans = MLX4_MIN_LOG_NUM_VLANS;
  338. } else {
  339. dev->caps.log_num_macs = log_num_mac;
  340. dev->caps.log_num_vlans = MLX4_LOG_NUM_VLANS;
  341. }
  342. for (i = 1; i <= dev->caps.num_ports; ++i) {
  343. dev->caps.port_type[i] = MLX4_PORT_TYPE_NONE;
  344. if (dev->caps.supported_type[i]) {
  345. /* if only ETH is supported - assign ETH */
  346. if (dev->caps.supported_type[i] == MLX4_PORT_TYPE_ETH)
  347. dev->caps.port_type[i] = MLX4_PORT_TYPE_ETH;
  348. /* if only IB is supported, assign IB */
  349. else if (dev->caps.supported_type[i] ==
  350. MLX4_PORT_TYPE_IB)
  351. dev->caps.port_type[i] = MLX4_PORT_TYPE_IB;
  352. else {
  353. /* if IB and ETH are supported, we set the port
  354. * type according to user selection of port type;
  355. * if user selected none, take the FW hint */
  356. if (port_type_array[i - 1] == MLX4_PORT_TYPE_NONE)
  357. dev->caps.port_type[i] = dev->caps.suggested_type[i] ?
  358. MLX4_PORT_TYPE_ETH : MLX4_PORT_TYPE_IB;
  359. else
  360. dev->caps.port_type[i] = port_type_array[i - 1];
  361. }
  362. }
  363. /*
  364. * Link sensing is allowed on the port if 3 conditions are true:
  365. * 1. Both protocols are supported on the port.
  366. * 2. Different types are supported on the port
  367. * 3. FW declared that it supports link sensing
  368. */
  369. mlx4_priv(dev)->sense.sense_allowed[i] =
  370. ((dev->caps.supported_type[i] == MLX4_PORT_TYPE_AUTO) &&
  371. (dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
  372. (dev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT));
  373. /*
  374. * If "default_sense" bit is set, we move the port to "AUTO" mode
  375. * and perform sense_port FW command to try and set the correct
  376. * port type from beginning
  377. */
  378. if (mlx4_priv(dev)->sense.sense_allowed[i] && dev->caps.default_sense[i]) {
  379. enum mlx4_port_type sensed_port = MLX4_PORT_TYPE_NONE;
  380. dev->caps.possible_type[i] = MLX4_PORT_TYPE_AUTO;
  381. mlx4_SENSE_PORT(dev, i, &sensed_port);
  382. if (sensed_port != MLX4_PORT_TYPE_NONE)
  383. dev->caps.port_type[i] = sensed_port;
  384. } else {
  385. dev->caps.possible_type[i] = dev->caps.port_type[i];
  386. }
  387. if (dev->caps.log_num_macs > dev_cap->port_cap[i].log_max_macs) {
  388. dev->caps.log_num_macs = dev_cap->port_cap[i].log_max_macs;
  389. mlx4_warn(dev, "Requested number of MACs is too much for port %d, reducing to %d\n",
  390. i, 1 << dev->caps.log_num_macs);
  391. }
  392. if (dev->caps.log_num_vlans > dev_cap->port_cap[i].log_max_vlans) {
  393. dev->caps.log_num_vlans = dev_cap->port_cap[i].log_max_vlans;
  394. mlx4_warn(dev, "Requested number of VLANs is too much for port %d, reducing to %d\n",
  395. i, 1 << dev->caps.log_num_vlans);
  396. }
  397. }
  398. dev->caps.max_counters = 1 << ilog2(dev_cap->max_counters);
  399. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] = dev_cap->reserved_qps;
  400. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] =
  401. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] =
  402. (1 << dev->caps.log_num_macs) *
  403. (1 << dev->caps.log_num_vlans) *
  404. dev->caps.num_ports;
  405. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH] = MLX4_NUM_FEXCH;
  406. if (dev_cap->dmfs_high_rate_qpn_base > 0 &&
  407. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN)
  408. dev->caps.dmfs_high_rate_qpn_base = dev_cap->dmfs_high_rate_qpn_base;
  409. else
  410. dev->caps.dmfs_high_rate_qpn_base =
  411. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
  412. if (dev_cap->dmfs_high_rate_qpn_range > 0 &&
  413. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN) {
  414. dev->caps.dmfs_high_rate_qpn_range = dev_cap->dmfs_high_rate_qpn_range;
  415. dev->caps.dmfs_high_steer_mode = MLX4_STEERING_DMFS_A0_DEFAULT;
  416. dev->caps.flags2 |= MLX4_DEV_CAP_FLAG2_FS_A0;
  417. } else {
  418. dev->caps.dmfs_high_steer_mode = MLX4_STEERING_DMFS_A0_NOT_SUPPORTED;
  419. dev->caps.dmfs_high_rate_qpn_base =
  420. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
  421. dev->caps.dmfs_high_rate_qpn_range = MLX4_A0_STEERING_TABLE_SIZE;
  422. }
  423. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_RSS_RAW_ETH] =
  424. dev->caps.dmfs_high_rate_qpn_range;
  425. dev->caps.reserved_qps = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] +
  426. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] +
  427. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] +
  428. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH];
  429. dev->caps.sqp_demux = (mlx4_is_master(dev)) ? MLX4_MAX_NUM_SLAVES : 0;
  430. if (!enable_64b_cqe_eqe && !mlx4_is_slave(dev)) {
  431. if (dev_cap->flags &
  432. (MLX4_DEV_CAP_FLAG_64B_CQE | MLX4_DEV_CAP_FLAG_64B_EQE)) {
  433. mlx4_warn(dev, "64B EQEs/CQEs supported by the device but not enabled\n");
  434. dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_64B_CQE;
  435. dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_64B_EQE;
  436. }
  437. if (dev_cap->flags2 &
  438. (MLX4_DEV_CAP_FLAG2_CQE_STRIDE |
  439. MLX4_DEV_CAP_FLAG2_EQE_STRIDE)) {
  440. mlx4_warn(dev, "Disabling EQE/CQE stride per user request\n");
  441. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_CQE_STRIDE;
  442. dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_EQE_STRIDE;
  443. }
  444. }
  445. if ((dev->caps.flags &
  446. (MLX4_DEV_CAP_FLAG_64B_CQE | MLX4_DEV_CAP_FLAG_64B_EQE)) &&
  447. mlx4_is_master(dev))
  448. dev->caps.function_caps |= MLX4_FUNC_CAP_64B_EQE_CQE;
  449. if (!mlx4_is_slave(dev)) {
  450. mlx4_enable_cqe_eqe_stride(dev);
  451. dev->caps.alloc_res_qp_mask =
  452. (dev->caps.bf_reg_size ? MLX4_RESERVE_ETH_BF_QP : 0) |
  453. MLX4_RESERVE_A0_QP;
  454. } else {
  455. dev->caps.alloc_res_qp_mask = 0;
  456. }
  457. return 0;
  458. }
  459. static int mlx4_get_pcie_dev_link_caps(struct mlx4_dev *dev,
  460. enum pci_bus_speed *speed,
  461. enum pcie_link_width *width)
  462. {
  463. u32 lnkcap1, lnkcap2;
  464. int err1, err2;
  465. #define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
  466. *speed = PCI_SPEED_UNKNOWN;
  467. *width = PCIE_LNK_WIDTH_UNKNOWN;
  468. err1 = pcie_capability_read_dword(dev->pdev, PCI_EXP_LNKCAP, &lnkcap1);
  469. err2 = pcie_capability_read_dword(dev->pdev, PCI_EXP_LNKCAP2, &lnkcap2);
  470. if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
  471. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
  472. *speed = PCIE_SPEED_8_0GT;
  473. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
  474. *speed = PCIE_SPEED_5_0GT;
  475. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
  476. *speed = PCIE_SPEED_2_5GT;
  477. }
  478. if (!err1) {
  479. *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
  480. if (!lnkcap2) { /* pre-r3.0 */
  481. if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
  482. *speed = PCIE_SPEED_5_0GT;
  483. else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
  484. *speed = PCIE_SPEED_2_5GT;
  485. }
  486. }
  487. if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN) {
  488. return err1 ? err1 :
  489. err2 ? err2 : -EINVAL;
  490. }
  491. return 0;
  492. }
  493. static void mlx4_check_pcie_caps(struct mlx4_dev *dev)
  494. {
  495. enum pcie_link_width width, width_cap;
  496. enum pci_bus_speed speed, speed_cap;
  497. int err;
  498. #define PCIE_SPEED_STR(speed) \
  499. (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
  500. speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
  501. speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
  502. "Unknown")
  503. err = mlx4_get_pcie_dev_link_caps(dev, &speed_cap, &width_cap);
  504. if (err) {
  505. mlx4_warn(dev,
  506. "Unable to determine PCIe device BW capabilities\n");
  507. return;
  508. }
  509. err = pcie_get_minimum_link(dev->pdev, &speed, &width);
  510. if (err || speed == PCI_SPEED_UNKNOWN ||
  511. width == PCIE_LNK_WIDTH_UNKNOWN) {
  512. mlx4_warn(dev,
  513. "Unable to determine PCI device chain minimum BW\n");
  514. return;
  515. }
  516. if (width != width_cap || speed != speed_cap)
  517. mlx4_warn(dev,
  518. "PCIe BW is different than device's capability\n");
  519. mlx4_info(dev, "PCIe link speed is %s, device supports %s\n",
  520. PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
  521. mlx4_info(dev, "PCIe link width is x%d, device supports x%d\n",
  522. width, width_cap);
  523. return;
  524. }
  525. /*The function checks if there are live vf, return the num of them*/
  526. static int mlx4_how_many_lives_vf(struct mlx4_dev *dev)
  527. {
  528. struct mlx4_priv *priv = mlx4_priv(dev);
  529. struct mlx4_slave_state *s_state;
  530. int i;
  531. int ret = 0;
  532. for (i = 1/*the ppf is 0*/; i < dev->num_slaves; ++i) {
  533. s_state = &priv->mfunc.master.slave_state[i];
  534. if (s_state->active && s_state->last_cmd !=
  535. MLX4_COMM_CMD_RESET) {
  536. mlx4_warn(dev, "%s: slave: %d is still active\n",
  537. __func__, i);
  538. ret++;
  539. }
  540. }
  541. return ret;
  542. }
  543. int mlx4_get_parav_qkey(struct mlx4_dev *dev, u32 qpn, u32 *qkey)
  544. {
  545. u32 qk = MLX4_RESERVED_QKEY_BASE;
  546. if (qpn >= dev->phys_caps.base_tunnel_sqpn + 8 * MLX4_MFUNC_MAX ||
  547. qpn < dev->phys_caps.base_proxy_sqpn)
  548. return -EINVAL;
  549. if (qpn >= dev->phys_caps.base_tunnel_sqpn)
  550. /* tunnel qp */
  551. qk += qpn - dev->phys_caps.base_tunnel_sqpn;
  552. else
  553. qk += qpn - dev->phys_caps.base_proxy_sqpn;
  554. *qkey = qk;
  555. return 0;
  556. }
  557. EXPORT_SYMBOL(mlx4_get_parav_qkey);
  558. void mlx4_sync_pkey_table(struct mlx4_dev *dev, int slave, int port, int i, int val)
  559. {
  560. struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
  561. if (!mlx4_is_master(dev))
  562. return;
  563. priv->virt2phys_pkey[slave][port - 1][i] = val;
  564. }
  565. EXPORT_SYMBOL(mlx4_sync_pkey_table);
  566. void mlx4_put_slave_node_guid(struct mlx4_dev *dev, int slave, __be64 guid)
  567. {
  568. struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
  569. if (!mlx4_is_master(dev))
  570. return;
  571. priv->slave_node_guids[slave] = guid;
  572. }
  573. EXPORT_SYMBOL(mlx4_put_slave_node_guid);
  574. __be64 mlx4_get_slave_node_guid(struct mlx4_dev *dev, int slave)
  575. {
  576. struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
  577. if (!mlx4_is_master(dev))
  578. return 0;
  579. return priv->slave_node_guids[slave];
  580. }
  581. EXPORT_SYMBOL(mlx4_get_slave_node_guid);
  582. int mlx4_is_slave_active(struct mlx4_dev *dev, int slave)
  583. {
  584. struct mlx4_priv *priv = mlx4_priv(dev);
  585. struct mlx4_slave_state *s_slave;
  586. if (!mlx4_is_master(dev))
  587. return 0;
  588. s_slave = &priv->mfunc.master.slave_state[slave];
  589. return !!s_slave->active;
  590. }
  591. EXPORT_SYMBOL(mlx4_is_slave_active);
  592. static void slave_adjust_steering_mode(struct mlx4_dev *dev,
  593. struct mlx4_dev_cap *dev_cap,
  594. struct mlx4_init_hca_param *hca_param)
  595. {
  596. dev->caps.steering_mode = hca_param->steering_mode;
  597. if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED) {
  598. dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
  599. dev->caps.fs_log_max_ucast_qp_range_size =
  600. dev_cap->fs_log_max_ucast_qp_range_size;
  601. } else
  602. dev->caps.num_qp_per_mgm =
  603. 4 * ((1 << hca_param->log_mc_entry_sz)/16 - 2);
  604. mlx4_dbg(dev, "Steering mode is: %s\n",
  605. mlx4_steering_mode_str(dev->caps.steering_mode));
  606. }
  607. static int mlx4_slave_cap(struct mlx4_dev *dev)
  608. {
  609. int err;
  610. u32 page_size;
  611. struct mlx4_dev_cap dev_cap;
  612. struct mlx4_func_cap func_cap;
  613. struct mlx4_init_hca_param hca_param;
  614. u8 i;
  615. memset(&hca_param, 0, sizeof(hca_param));
  616. err = mlx4_QUERY_HCA(dev, &hca_param);
  617. if (err) {
  618. mlx4_err(dev, "QUERY_HCA command failed, aborting\n");
  619. return err;
  620. }
  621. /* fail if the hca has an unknown global capability
  622. * at this time global_caps should be always zeroed
  623. */
  624. if (hca_param.global_caps) {
  625. mlx4_err(dev, "Unknown hca global capabilities\n");
  626. return -ENOSYS;
  627. }
  628. mlx4_log_num_mgm_entry_size = hca_param.log_mc_entry_sz;
  629. dev->caps.hca_core_clock = hca_param.hca_core_clock;
  630. memset(&dev_cap, 0, sizeof(dev_cap));
  631. dev->caps.max_qp_dest_rdma = 1 << hca_param.log_rd_per_qp;
  632. err = mlx4_dev_cap(dev, &dev_cap);
  633. if (err) {
  634. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting\n");
  635. return err;
  636. }
  637. err = mlx4_QUERY_FW(dev);
  638. if (err)
  639. mlx4_err(dev, "QUERY_FW command failed: could not get FW version\n");
  640. page_size = ~dev->caps.page_size_cap + 1;
  641. mlx4_warn(dev, "HCA minimum page size:%d\n", page_size);
  642. if (page_size > PAGE_SIZE) {
  643. mlx4_err(dev, "HCA minimum page size of %d bigger than kernel PAGE_SIZE of %ld, aborting\n",
  644. page_size, PAGE_SIZE);
  645. return -ENODEV;
  646. }
  647. /* slave gets uar page size from QUERY_HCA fw command */
  648. dev->caps.uar_page_size = 1 << (hca_param.uar_page_sz + 12);
  649. /* TODO: relax this assumption */
  650. if (dev->caps.uar_page_size != PAGE_SIZE) {
  651. mlx4_err(dev, "UAR size:%d != kernel PAGE_SIZE of %ld\n",
  652. dev->caps.uar_page_size, PAGE_SIZE);
  653. return -ENODEV;
  654. }
  655. memset(&func_cap, 0, sizeof(func_cap));
  656. err = mlx4_QUERY_FUNC_CAP(dev, 0, &func_cap);
  657. if (err) {
  658. mlx4_err(dev, "QUERY_FUNC_CAP general command failed, aborting (%d)\n",
  659. err);
  660. return err;
  661. }
  662. if ((func_cap.pf_context_behaviour | PF_CONTEXT_BEHAVIOUR_MASK) !=
  663. PF_CONTEXT_BEHAVIOUR_MASK) {
  664. mlx4_err(dev, "Unknown pf context behaviour %x known flags %x\n",
  665. func_cap.pf_context_behaviour, PF_CONTEXT_BEHAVIOUR_MASK);
  666. return -ENOSYS;
  667. }
  668. dev->caps.num_ports = func_cap.num_ports;
  669. dev->quotas.qp = func_cap.qp_quota;
  670. dev->quotas.srq = func_cap.srq_quota;
  671. dev->quotas.cq = func_cap.cq_quota;
  672. dev->quotas.mpt = func_cap.mpt_quota;
  673. dev->quotas.mtt = func_cap.mtt_quota;
  674. dev->caps.num_qps = 1 << hca_param.log_num_qps;
  675. dev->caps.num_srqs = 1 << hca_param.log_num_srqs;
  676. dev->caps.num_cqs = 1 << hca_param.log_num_cqs;
  677. dev->caps.num_mpts = 1 << hca_param.log_mpt_sz;
  678. dev->caps.num_eqs = func_cap.max_eq;
  679. dev->caps.reserved_eqs = func_cap.reserved_eq;
  680. dev->caps.num_pds = MLX4_NUM_PDS;
  681. dev->caps.num_mgms = 0;
  682. dev->caps.num_amgms = 0;
  683. if (dev->caps.num_ports > MLX4_MAX_PORTS) {
  684. mlx4_err(dev, "HCA has %d ports, but we only support %d, aborting\n",
  685. dev->caps.num_ports, MLX4_MAX_PORTS);
  686. return -ENODEV;
  687. }
  688. dev->caps.qp0_qkey = kcalloc(dev->caps.num_ports, sizeof(u32), GFP_KERNEL);
  689. dev->caps.qp0_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  690. dev->caps.qp0_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  691. dev->caps.qp1_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  692. dev->caps.qp1_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  693. if (!dev->caps.qp0_tunnel || !dev->caps.qp0_proxy ||
  694. !dev->caps.qp1_tunnel || !dev->caps.qp1_proxy ||
  695. !dev->caps.qp0_qkey) {
  696. err = -ENOMEM;
  697. goto err_mem;
  698. }
  699. for (i = 1; i <= dev->caps.num_ports; ++i) {
  700. err = mlx4_QUERY_FUNC_CAP(dev, i, &func_cap);
  701. if (err) {
  702. mlx4_err(dev, "QUERY_FUNC_CAP port command failed for port %d, aborting (%d)\n",
  703. i, err);
  704. goto err_mem;
  705. }
  706. dev->caps.qp0_qkey[i - 1] = func_cap.qp0_qkey;
  707. dev->caps.qp0_tunnel[i - 1] = func_cap.qp0_tunnel_qpn;
  708. dev->caps.qp0_proxy[i - 1] = func_cap.qp0_proxy_qpn;
  709. dev->caps.qp1_tunnel[i - 1] = func_cap.qp1_tunnel_qpn;
  710. dev->caps.qp1_proxy[i - 1] = func_cap.qp1_proxy_qpn;
  711. dev->caps.port_mask[i] = dev->caps.port_type[i];
  712. dev->caps.phys_port_id[i] = func_cap.phys_port_id;
  713. if (mlx4_get_slave_pkey_gid_tbl_len(dev, i,
  714. &dev->caps.gid_table_len[i],
  715. &dev->caps.pkey_table_len[i]))
  716. goto err_mem;
  717. }
  718. if (dev->caps.uar_page_size * (dev->caps.num_uars -
  719. dev->caps.reserved_uars) >
  720. pci_resource_len(dev->pdev, 2)) {
  721. mlx4_err(dev, "HCA reported UAR region size of 0x%x bigger than PCI resource 2 size of 0x%llx, aborting\n",
  722. dev->caps.uar_page_size * dev->caps.num_uars,
  723. (unsigned long long) pci_resource_len(dev->pdev, 2));
  724. goto err_mem;
  725. }
  726. if (hca_param.dev_cap_enabled & MLX4_DEV_CAP_64B_EQE_ENABLED) {
  727. dev->caps.eqe_size = 64;
  728. dev->caps.eqe_factor = 1;
  729. } else {
  730. dev->caps.eqe_size = 32;
  731. dev->caps.eqe_factor = 0;
  732. }
  733. if (hca_param.dev_cap_enabled & MLX4_DEV_CAP_64B_CQE_ENABLED) {
  734. dev->caps.cqe_size = 64;
  735. dev->caps.userspace_caps |= MLX4_USER_DEV_CAP_LARGE_CQE;
  736. } else {
  737. dev->caps.cqe_size = 32;
  738. }
  739. if (hca_param.dev_cap_enabled & MLX4_DEV_CAP_EQE_STRIDE_ENABLED) {
  740. dev->caps.eqe_size = hca_param.eqe_size;
  741. dev->caps.eqe_factor = 0;
  742. }
  743. if (hca_param.dev_cap_enabled & MLX4_DEV_CAP_CQE_STRIDE_ENABLED) {
  744. dev->caps.cqe_size = hca_param.cqe_size;
  745. /* User still need to know when CQE > 32B */
  746. dev->caps.userspace_caps |= MLX4_USER_DEV_CAP_LARGE_CQE;
  747. }
  748. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
  749. mlx4_warn(dev, "Timestamping is not supported in slave mode\n");
  750. slave_adjust_steering_mode(dev, &dev_cap, &hca_param);
  751. if (func_cap.extra_flags & MLX4_QUERY_FUNC_FLAGS_BF_RES_QP &&
  752. dev->caps.bf_reg_size)
  753. dev->caps.alloc_res_qp_mask |= MLX4_RESERVE_ETH_BF_QP;
  754. if (func_cap.extra_flags & MLX4_QUERY_FUNC_FLAGS_A0_RES_QP)
  755. dev->caps.alloc_res_qp_mask |= MLX4_RESERVE_A0_QP;
  756. return 0;
  757. err_mem:
  758. kfree(dev->caps.qp0_qkey);
  759. kfree(dev->caps.qp0_tunnel);
  760. kfree(dev->caps.qp0_proxy);
  761. kfree(dev->caps.qp1_tunnel);
  762. kfree(dev->caps.qp1_proxy);
  763. dev->caps.qp0_qkey = NULL;
  764. dev->caps.qp0_tunnel = NULL;
  765. dev->caps.qp0_proxy = NULL;
  766. dev->caps.qp1_tunnel = NULL;
  767. dev->caps.qp1_proxy = NULL;
  768. return err;
  769. }
  770. static void mlx4_request_modules(struct mlx4_dev *dev)
  771. {
  772. int port;
  773. int has_ib_port = false;
  774. int has_eth_port = false;
  775. #define EN_DRV_NAME "mlx4_en"
  776. #define IB_DRV_NAME "mlx4_ib"
  777. for (port = 1; port <= dev->caps.num_ports; port++) {
  778. if (dev->caps.port_type[port] == MLX4_PORT_TYPE_IB)
  779. has_ib_port = true;
  780. else if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
  781. has_eth_port = true;
  782. }
  783. if (has_eth_port)
  784. request_module_nowait(EN_DRV_NAME);
  785. if (has_ib_port || (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE))
  786. request_module_nowait(IB_DRV_NAME);
  787. }
  788. /*
  789. * Change the port configuration of the device.
  790. * Every user of this function must hold the port mutex.
  791. */
  792. int mlx4_change_port_types(struct mlx4_dev *dev,
  793. enum mlx4_port_type *port_types)
  794. {
  795. int err = 0;
  796. int change = 0;
  797. int port;
  798. for (port = 0; port < dev->caps.num_ports; port++) {
  799. /* Change the port type only if the new type is different
  800. * from the current, and not set to Auto */
  801. if (port_types[port] != dev->caps.port_type[port + 1])
  802. change = 1;
  803. }
  804. if (change) {
  805. mlx4_unregister_device(dev);
  806. for (port = 1; port <= dev->caps.num_ports; port++) {
  807. mlx4_CLOSE_PORT(dev, port);
  808. dev->caps.port_type[port] = port_types[port - 1];
  809. err = mlx4_SET_PORT(dev, port, -1);
  810. if (err) {
  811. mlx4_err(dev, "Failed to set port %d, aborting\n",
  812. port);
  813. goto out;
  814. }
  815. }
  816. mlx4_set_port_mask(dev);
  817. err = mlx4_register_device(dev);
  818. if (err) {
  819. mlx4_err(dev, "Failed to register device\n");
  820. goto out;
  821. }
  822. mlx4_request_modules(dev);
  823. }
  824. out:
  825. return err;
  826. }
  827. static ssize_t show_port_type(struct device *dev,
  828. struct device_attribute *attr,
  829. char *buf)
  830. {
  831. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  832. port_attr);
  833. struct mlx4_dev *mdev = info->dev;
  834. char type[8];
  835. sprintf(type, "%s",
  836. (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_IB) ?
  837. "ib" : "eth");
  838. if (mdev->caps.possible_type[info->port] == MLX4_PORT_TYPE_AUTO)
  839. sprintf(buf, "auto (%s)\n", type);
  840. else
  841. sprintf(buf, "%s\n", type);
  842. return strlen(buf);
  843. }
  844. static ssize_t set_port_type(struct device *dev,
  845. struct device_attribute *attr,
  846. const char *buf, size_t count)
  847. {
  848. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  849. port_attr);
  850. struct mlx4_dev *mdev = info->dev;
  851. struct mlx4_priv *priv = mlx4_priv(mdev);
  852. enum mlx4_port_type types[MLX4_MAX_PORTS];
  853. enum mlx4_port_type new_types[MLX4_MAX_PORTS];
  854. static DEFINE_MUTEX(set_port_type_mutex);
  855. int i;
  856. int err = 0;
  857. mutex_lock(&set_port_type_mutex);
  858. if (!strcmp(buf, "ib\n"))
  859. info->tmp_type = MLX4_PORT_TYPE_IB;
  860. else if (!strcmp(buf, "eth\n"))
  861. info->tmp_type = MLX4_PORT_TYPE_ETH;
  862. else if (!strcmp(buf, "auto\n"))
  863. info->tmp_type = MLX4_PORT_TYPE_AUTO;
  864. else {
  865. mlx4_err(mdev, "%s is not supported port type\n", buf);
  866. err = -EINVAL;
  867. goto err_out;
  868. }
  869. mlx4_stop_sense(mdev);
  870. mutex_lock(&priv->port_mutex);
  871. /* Possible type is always the one that was delivered */
  872. mdev->caps.possible_type[info->port] = info->tmp_type;
  873. for (i = 0; i < mdev->caps.num_ports; i++) {
  874. types[i] = priv->port[i+1].tmp_type ? priv->port[i+1].tmp_type :
  875. mdev->caps.possible_type[i+1];
  876. if (types[i] == MLX4_PORT_TYPE_AUTO)
  877. types[i] = mdev->caps.port_type[i+1];
  878. }
  879. if (!(mdev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
  880. !(mdev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT)) {
  881. for (i = 1; i <= mdev->caps.num_ports; i++) {
  882. if (mdev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
  883. mdev->caps.possible_type[i] = mdev->caps.port_type[i];
  884. err = -EINVAL;
  885. }
  886. }
  887. }
  888. if (err) {
  889. mlx4_err(mdev, "Auto sensing is not supported on this HCA. Set only 'eth' or 'ib' for both ports (should be the same)\n");
  890. goto out;
  891. }
  892. mlx4_do_sense_ports(mdev, new_types, types);
  893. err = mlx4_check_port_params(mdev, new_types);
  894. if (err)
  895. goto out;
  896. /* We are about to apply the changes after the configuration
  897. * was verified, no need to remember the temporary types
  898. * any more */
  899. for (i = 0; i < mdev->caps.num_ports; i++)
  900. priv->port[i + 1].tmp_type = 0;
  901. err = mlx4_change_port_types(mdev, new_types);
  902. out:
  903. mlx4_start_sense(mdev);
  904. mutex_unlock(&priv->port_mutex);
  905. err_out:
  906. mutex_unlock(&set_port_type_mutex);
  907. return err ? err : count;
  908. }
  909. enum ibta_mtu {
  910. IB_MTU_256 = 1,
  911. IB_MTU_512 = 2,
  912. IB_MTU_1024 = 3,
  913. IB_MTU_2048 = 4,
  914. IB_MTU_4096 = 5
  915. };
  916. static inline int int_to_ibta_mtu(int mtu)
  917. {
  918. switch (mtu) {
  919. case 256: return IB_MTU_256;
  920. case 512: return IB_MTU_512;
  921. case 1024: return IB_MTU_1024;
  922. case 2048: return IB_MTU_2048;
  923. case 4096: return IB_MTU_4096;
  924. default: return -1;
  925. }
  926. }
  927. static inline int ibta_mtu_to_int(enum ibta_mtu mtu)
  928. {
  929. switch (mtu) {
  930. case IB_MTU_256: return 256;
  931. case IB_MTU_512: return 512;
  932. case IB_MTU_1024: return 1024;
  933. case IB_MTU_2048: return 2048;
  934. case IB_MTU_4096: return 4096;
  935. default: return -1;
  936. }
  937. }
  938. static ssize_t show_port_ib_mtu(struct device *dev,
  939. struct device_attribute *attr,
  940. char *buf)
  941. {
  942. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  943. port_mtu_attr);
  944. struct mlx4_dev *mdev = info->dev;
  945. if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH)
  946. mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
  947. sprintf(buf, "%d\n",
  948. ibta_mtu_to_int(mdev->caps.port_ib_mtu[info->port]));
  949. return strlen(buf);
  950. }
  951. static ssize_t set_port_ib_mtu(struct device *dev,
  952. struct device_attribute *attr,
  953. const char *buf, size_t count)
  954. {
  955. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  956. port_mtu_attr);
  957. struct mlx4_dev *mdev = info->dev;
  958. struct mlx4_priv *priv = mlx4_priv(mdev);
  959. int err, port, mtu, ibta_mtu = -1;
  960. if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH) {
  961. mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
  962. return -EINVAL;
  963. }
  964. err = kstrtoint(buf, 0, &mtu);
  965. if (!err)
  966. ibta_mtu = int_to_ibta_mtu(mtu);
  967. if (err || ibta_mtu < 0) {
  968. mlx4_err(mdev, "%s is invalid IBTA mtu\n", buf);
  969. return -EINVAL;
  970. }
  971. mdev->caps.port_ib_mtu[info->port] = ibta_mtu;
  972. mlx4_stop_sense(mdev);
  973. mutex_lock(&priv->port_mutex);
  974. mlx4_unregister_device(mdev);
  975. for (port = 1; port <= mdev->caps.num_ports; port++) {
  976. mlx4_CLOSE_PORT(mdev, port);
  977. err = mlx4_SET_PORT(mdev, port, -1);
  978. if (err) {
  979. mlx4_err(mdev, "Failed to set port %d, aborting\n",
  980. port);
  981. goto err_set_port;
  982. }
  983. }
  984. err = mlx4_register_device(mdev);
  985. err_set_port:
  986. mutex_unlock(&priv->port_mutex);
  987. mlx4_start_sense(mdev);
  988. return err ? err : count;
  989. }
  990. static int mlx4_load_fw(struct mlx4_dev *dev)
  991. {
  992. struct mlx4_priv *priv = mlx4_priv(dev);
  993. int err;
  994. priv->fw.fw_icm = mlx4_alloc_icm(dev, priv->fw.fw_pages,
  995. GFP_HIGHUSER | __GFP_NOWARN, 0);
  996. if (!priv->fw.fw_icm) {
  997. mlx4_err(dev, "Couldn't allocate FW area, aborting\n");
  998. return -ENOMEM;
  999. }
  1000. err = mlx4_MAP_FA(dev, priv->fw.fw_icm);
  1001. if (err) {
  1002. mlx4_err(dev, "MAP_FA command failed, aborting\n");
  1003. goto err_free;
  1004. }
  1005. err = mlx4_RUN_FW(dev);
  1006. if (err) {
  1007. mlx4_err(dev, "RUN_FW command failed, aborting\n");
  1008. goto err_unmap_fa;
  1009. }
  1010. return 0;
  1011. err_unmap_fa:
  1012. mlx4_UNMAP_FA(dev);
  1013. err_free:
  1014. mlx4_free_icm(dev, priv->fw.fw_icm, 0);
  1015. return err;
  1016. }
  1017. static int mlx4_init_cmpt_table(struct mlx4_dev *dev, u64 cmpt_base,
  1018. int cmpt_entry_sz)
  1019. {
  1020. struct mlx4_priv *priv = mlx4_priv(dev);
  1021. int err;
  1022. int num_eqs;
  1023. err = mlx4_init_icm_table(dev, &priv->qp_table.cmpt_table,
  1024. cmpt_base +
  1025. ((u64) (MLX4_CMPT_TYPE_QP *
  1026. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  1027. cmpt_entry_sz, dev->caps.num_qps,
  1028. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1029. 0, 0);
  1030. if (err)
  1031. goto err;
  1032. err = mlx4_init_icm_table(dev, &priv->srq_table.cmpt_table,
  1033. cmpt_base +
  1034. ((u64) (MLX4_CMPT_TYPE_SRQ *
  1035. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  1036. cmpt_entry_sz, dev->caps.num_srqs,
  1037. dev->caps.reserved_srqs, 0, 0);
  1038. if (err)
  1039. goto err_qp;
  1040. err = mlx4_init_icm_table(dev, &priv->cq_table.cmpt_table,
  1041. cmpt_base +
  1042. ((u64) (MLX4_CMPT_TYPE_CQ *
  1043. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  1044. cmpt_entry_sz, dev->caps.num_cqs,
  1045. dev->caps.reserved_cqs, 0, 0);
  1046. if (err)
  1047. goto err_srq;
  1048. num_eqs = dev->phys_caps.num_phys_eqs;
  1049. err = mlx4_init_icm_table(dev, &priv->eq_table.cmpt_table,
  1050. cmpt_base +
  1051. ((u64) (MLX4_CMPT_TYPE_EQ *
  1052. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  1053. cmpt_entry_sz, num_eqs, num_eqs, 0, 0);
  1054. if (err)
  1055. goto err_cq;
  1056. return 0;
  1057. err_cq:
  1058. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  1059. err_srq:
  1060. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  1061. err_qp:
  1062. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  1063. err:
  1064. return err;
  1065. }
  1066. static int mlx4_init_icm(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
  1067. struct mlx4_init_hca_param *init_hca, u64 icm_size)
  1068. {
  1069. struct mlx4_priv *priv = mlx4_priv(dev);
  1070. u64 aux_pages;
  1071. int num_eqs;
  1072. int err;
  1073. err = mlx4_SET_ICM_SIZE(dev, icm_size, &aux_pages);
  1074. if (err) {
  1075. mlx4_err(dev, "SET_ICM_SIZE command failed, aborting\n");
  1076. return err;
  1077. }
  1078. mlx4_dbg(dev, "%lld KB of HCA context requires %lld KB aux memory\n",
  1079. (unsigned long long) icm_size >> 10,
  1080. (unsigned long long) aux_pages << 2);
  1081. priv->fw.aux_icm = mlx4_alloc_icm(dev, aux_pages,
  1082. GFP_HIGHUSER | __GFP_NOWARN, 0);
  1083. if (!priv->fw.aux_icm) {
  1084. mlx4_err(dev, "Couldn't allocate aux memory, aborting\n");
  1085. return -ENOMEM;
  1086. }
  1087. err = mlx4_MAP_ICM_AUX(dev, priv->fw.aux_icm);
  1088. if (err) {
  1089. mlx4_err(dev, "MAP_ICM_AUX command failed, aborting\n");
  1090. goto err_free_aux;
  1091. }
  1092. err = mlx4_init_cmpt_table(dev, init_hca->cmpt_base, dev_cap->cmpt_entry_sz);
  1093. if (err) {
  1094. mlx4_err(dev, "Failed to map cMPT context memory, aborting\n");
  1095. goto err_unmap_aux;
  1096. }
  1097. num_eqs = dev->phys_caps.num_phys_eqs;
  1098. err = mlx4_init_icm_table(dev, &priv->eq_table.table,
  1099. init_hca->eqc_base, dev_cap->eqc_entry_sz,
  1100. num_eqs, num_eqs, 0, 0);
  1101. if (err) {
  1102. mlx4_err(dev, "Failed to map EQ context memory, aborting\n");
  1103. goto err_unmap_cmpt;
  1104. }
  1105. /*
  1106. * Reserved MTT entries must be aligned up to a cacheline
  1107. * boundary, since the FW will write to them, while the driver
  1108. * writes to all other MTT entries. (The variable
  1109. * dev->caps.mtt_entry_sz below is really the MTT segment
  1110. * size, not the raw entry size)
  1111. */
  1112. dev->caps.reserved_mtts =
  1113. ALIGN(dev->caps.reserved_mtts * dev->caps.mtt_entry_sz,
  1114. dma_get_cache_alignment()) / dev->caps.mtt_entry_sz;
  1115. err = mlx4_init_icm_table(dev, &priv->mr_table.mtt_table,
  1116. init_hca->mtt_base,
  1117. dev->caps.mtt_entry_sz,
  1118. dev->caps.num_mtts,
  1119. dev->caps.reserved_mtts, 1, 0);
  1120. if (err) {
  1121. mlx4_err(dev, "Failed to map MTT context memory, aborting\n");
  1122. goto err_unmap_eq;
  1123. }
  1124. err = mlx4_init_icm_table(dev, &priv->mr_table.dmpt_table,
  1125. init_hca->dmpt_base,
  1126. dev_cap->dmpt_entry_sz,
  1127. dev->caps.num_mpts,
  1128. dev->caps.reserved_mrws, 1, 1);
  1129. if (err) {
  1130. mlx4_err(dev, "Failed to map dMPT context memory, aborting\n");
  1131. goto err_unmap_mtt;
  1132. }
  1133. err = mlx4_init_icm_table(dev, &priv->qp_table.qp_table,
  1134. init_hca->qpc_base,
  1135. dev_cap->qpc_entry_sz,
  1136. dev->caps.num_qps,
  1137. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1138. 0, 0);
  1139. if (err) {
  1140. mlx4_err(dev, "Failed to map QP context memory, aborting\n");
  1141. goto err_unmap_dmpt;
  1142. }
  1143. err = mlx4_init_icm_table(dev, &priv->qp_table.auxc_table,
  1144. init_hca->auxc_base,
  1145. dev_cap->aux_entry_sz,
  1146. dev->caps.num_qps,
  1147. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1148. 0, 0);
  1149. if (err) {
  1150. mlx4_err(dev, "Failed to map AUXC context memory, aborting\n");
  1151. goto err_unmap_qp;
  1152. }
  1153. err = mlx4_init_icm_table(dev, &priv->qp_table.altc_table,
  1154. init_hca->altc_base,
  1155. dev_cap->altc_entry_sz,
  1156. dev->caps.num_qps,
  1157. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1158. 0, 0);
  1159. if (err) {
  1160. mlx4_err(dev, "Failed to map ALTC context memory, aborting\n");
  1161. goto err_unmap_auxc;
  1162. }
  1163. err = mlx4_init_icm_table(dev, &priv->qp_table.rdmarc_table,
  1164. init_hca->rdmarc_base,
  1165. dev_cap->rdmarc_entry_sz << priv->qp_table.rdmarc_shift,
  1166. dev->caps.num_qps,
  1167. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  1168. 0, 0);
  1169. if (err) {
  1170. mlx4_err(dev, "Failed to map RDMARC context memory, aborting\n");
  1171. goto err_unmap_altc;
  1172. }
  1173. err = mlx4_init_icm_table(dev, &priv->cq_table.table,
  1174. init_hca->cqc_base,
  1175. dev_cap->cqc_entry_sz,
  1176. dev->caps.num_cqs,
  1177. dev->caps.reserved_cqs, 0, 0);
  1178. if (err) {
  1179. mlx4_err(dev, "Failed to map CQ context memory, aborting\n");
  1180. goto err_unmap_rdmarc;
  1181. }
  1182. err = mlx4_init_icm_table(dev, &priv->srq_table.table,
  1183. init_hca->srqc_base,
  1184. dev_cap->srq_entry_sz,
  1185. dev->caps.num_srqs,
  1186. dev->caps.reserved_srqs, 0, 0);
  1187. if (err) {
  1188. mlx4_err(dev, "Failed to map SRQ context memory, aborting\n");
  1189. goto err_unmap_cq;
  1190. }
  1191. /*
  1192. * For flow steering device managed mode it is required to use
  1193. * mlx4_init_icm_table. For B0 steering mode it's not strictly
  1194. * required, but for simplicity just map the whole multicast
  1195. * group table now. The table isn't very big and it's a lot
  1196. * easier than trying to track ref counts.
  1197. */
  1198. err = mlx4_init_icm_table(dev, &priv->mcg_table.table,
  1199. init_hca->mc_base,
  1200. mlx4_get_mgm_entry_size(dev),
  1201. dev->caps.num_mgms + dev->caps.num_amgms,
  1202. dev->caps.num_mgms + dev->caps.num_amgms,
  1203. 0, 0);
  1204. if (err) {
  1205. mlx4_err(dev, "Failed to map MCG context memory, aborting\n");
  1206. goto err_unmap_srq;
  1207. }
  1208. return 0;
  1209. err_unmap_srq:
  1210. mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
  1211. err_unmap_cq:
  1212. mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
  1213. err_unmap_rdmarc:
  1214. mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
  1215. err_unmap_altc:
  1216. mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
  1217. err_unmap_auxc:
  1218. mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
  1219. err_unmap_qp:
  1220. mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
  1221. err_unmap_dmpt:
  1222. mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
  1223. err_unmap_mtt:
  1224. mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
  1225. err_unmap_eq:
  1226. mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
  1227. err_unmap_cmpt:
  1228. mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
  1229. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  1230. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  1231. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  1232. err_unmap_aux:
  1233. mlx4_UNMAP_ICM_AUX(dev);
  1234. err_free_aux:
  1235. mlx4_free_icm(dev, priv->fw.aux_icm, 0);
  1236. return err;
  1237. }
  1238. static void mlx4_free_icms(struct mlx4_dev *dev)
  1239. {
  1240. struct mlx4_priv *priv = mlx4_priv(dev);
  1241. mlx4_cleanup_icm_table(dev, &priv->mcg_table.table);
  1242. mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
  1243. mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
  1244. mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
  1245. mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
  1246. mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
  1247. mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
  1248. mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
  1249. mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
  1250. mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
  1251. mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
  1252. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  1253. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  1254. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  1255. mlx4_UNMAP_ICM_AUX(dev);
  1256. mlx4_free_icm(dev, priv->fw.aux_icm, 0);
  1257. }
  1258. static void mlx4_slave_exit(struct mlx4_dev *dev)
  1259. {
  1260. struct mlx4_priv *priv = mlx4_priv(dev);
  1261. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1262. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, MLX4_COMM_TIME))
  1263. mlx4_warn(dev, "Failed to close slave function\n");
  1264. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1265. }
  1266. static int map_bf_area(struct mlx4_dev *dev)
  1267. {
  1268. struct mlx4_priv *priv = mlx4_priv(dev);
  1269. resource_size_t bf_start;
  1270. resource_size_t bf_len;
  1271. int err = 0;
  1272. if (!dev->caps.bf_reg_size)
  1273. return -ENXIO;
  1274. bf_start = pci_resource_start(dev->pdev, 2) +
  1275. (dev->caps.num_uars << PAGE_SHIFT);
  1276. bf_len = pci_resource_len(dev->pdev, 2) -
  1277. (dev->caps.num_uars << PAGE_SHIFT);
  1278. priv->bf_mapping = io_mapping_create_wc(bf_start, bf_len);
  1279. if (!priv->bf_mapping)
  1280. err = -ENOMEM;
  1281. return err;
  1282. }
  1283. static void unmap_bf_area(struct mlx4_dev *dev)
  1284. {
  1285. if (mlx4_priv(dev)->bf_mapping)
  1286. io_mapping_free(mlx4_priv(dev)->bf_mapping);
  1287. }
  1288. cycle_t mlx4_read_clock(struct mlx4_dev *dev)
  1289. {
  1290. u32 clockhi, clocklo, clockhi1;
  1291. cycle_t cycles;
  1292. int i;
  1293. struct mlx4_priv *priv = mlx4_priv(dev);
  1294. for (i = 0; i < 10; i++) {
  1295. clockhi = swab32(readl(priv->clock_mapping));
  1296. clocklo = swab32(readl(priv->clock_mapping + 4));
  1297. clockhi1 = swab32(readl(priv->clock_mapping));
  1298. if (clockhi == clockhi1)
  1299. break;
  1300. }
  1301. cycles = (u64) clockhi << 32 | (u64) clocklo;
  1302. return cycles;
  1303. }
  1304. EXPORT_SYMBOL_GPL(mlx4_read_clock);
  1305. static int map_internal_clock(struct mlx4_dev *dev)
  1306. {
  1307. struct mlx4_priv *priv = mlx4_priv(dev);
  1308. priv->clock_mapping =
  1309. ioremap(pci_resource_start(dev->pdev, priv->fw.clock_bar) +
  1310. priv->fw.clock_offset, MLX4_CLOCK_SIZE);
  1311. if (!priv->clock_mapping)
  1312. return -ENOMEM;
  1313. return 0;
  1314. }
  1315. static void unmap_internal_clock(struct mlx4_dev *dev)
  1316. {
  1317. struct mlx4_priv *priv = mlx4_priv(dev);
  1318. if (priv->clock_mapping)
  1319. iounmap(priv->clock_mapping);
  1320. }
  1321. static void mlx4_close_hca(struct mlx4_dev *dev)
  1322. {
  1323. unmap_internal_clock(dev);
  1324. unmap_bf_area(dev);
  1325. if (mlx4_is_slave(dev))
  1326. mlx4_slave_exit(dev);
  1327. else {
  1328. mlx4_CLOSE_HCA(dev, 0);
  1329. mlx4_free_icms(dev);
  1330. }
  1331. }
  1332. static void mlx4_close_fw(struct mlx4_dev *dev)
  1333. {
  1334. if (!mlx4_is_slave(dev)) {
  1335. mlx4_UNMAP_FA(dev);
  1336. mlx4_free_icm(dev, mlx4_priv(dev)->fw.fw_icm, 0);
  1337. }
  1338. }
  1339. static int mlx4_init_slave(struct mlx4_dev *dev)
  1340. {
  1341. struct mlx4_priv *priv = mlx4_priv(dev);
  1342. u64 dma = (u64) priv->mfunc.vhcr_dma;
  1343. int ret_from_reset = 0;
  1344. u32 slave_read;
  1345. u32 cmd_channel_ver;
  1346. if (atomic_read(&pf_loading)) {
  1347. mlx4_warn(dev, "PF is not ready - Deferring probe\n");
  1348. return -EPROBE_DEFER;
  1349. }
  1350. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1351. priv->cmd.max_cmds = 1;
  1352. mlx4_warn(dev, "Sending reset\n");
  1353. ret_from_reset = mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0,
  1354. MLX4_COMM_TIME);
  1355. /* if we are in the middle of flr the slave will try
  1356. * NUM_OF_RESET_RETRIES times before leaving.*/
  1357. if (ret_from_reset) {
  1358. if (MLX4_DELAY_RESET_SLAVE == ret_from_reset) {
  1359. mlx4_warn(dev, "slave is currently in the middle of FLR - Deferring probe\n");
  1360. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1361. return -EPROBE_DEFER;
  1362. } else
  1363. goto err;
  1364. }
  1365. /* check the driver version - the slave I/F revision
  1366. * must match the master's */
  1367. slave_read = swab32(readl(&priv->mfunc.comm->slave_read));
  1368. cmd_channel_ver = mlx4_comm_get_version();
  1369. if (MLX4_COMM_GET_IF_REV(cmd_channel_ver) !=
  1370. MLX4_COMM_GET_IF_REV(slave_read)) {
  1371. mlx4_err(dev, "slave driver version is not supported by the master\n");
  1372. goto err;
  1373. }
  1374. mlx4_warn(dev, "Sending vhcr0\n");
  1375. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR0, dma >> 48,
  1376. MLX4_COMM_TIME))
  1377. goto err;
  1378. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR1, dma >> 32,
  1379. MLX4_COMM_TIME))
  1380. goto err;
  1381. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR2, dma >> 16,
  1382. MLX4_COMM_TIME))
  1383. goto err;
  1384. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_EN, dma, MLX4_COMM_TIME))
  1385. goto err;
  1386. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1387. return 0;
  1388. err:
  1389. mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, 0);
  1390. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1391. return -EIO;
  1392. }
  1393. static void mlx4_parav_master_pf_caps(struct mlx4_dev *dev)
  1394. {
  1395. int i;
  1396. for (i = 1; i <= dev->caps.num_ports; i++) {
  1397. if (dev->caps.port_type[i] == MLX4_PORT_TYPE_ETH)
  1398. dev->caps.gid_table_len[i] =
  1399. mlx4_get_slave_num_gids(dev, 0, i);
  1400. else
  1401. dev->caps.gid_table_len[i] = 1;
  1402. dev->caps.pkey_table_len[i] =
  1403. dev->phys_caps.pkey_phys_table_len[i] - 1;
  1404. }
  1405. }
  1406. static int choose_log_fs_mgm_entry_size(int qp_per_entry)
  1407. {
  1408. int i = MLX4_MIN_MGM_LOG_ENTRY_SIZE;
  1409. for (i = MLX4_MIN_MGM_LOG_ENTRY_SIZE; i <= MLX4_MAX_MGM_LOG_ENTRY_SIZE;
  1410. i++) {
  1411. if (qp_per_entry <= 4 * ((1 << i) / 16 - 2))
  1412. break;
  1413. }
  1414. return (i <= MLX4_MAX_MGM_LOG_ENTRY_SIZE) ? i : -1;
  1415. }
  1416. static const char *dmfs_high_rate_steering_mode_str(int dmfs_high_steer_mode)
  1417. {
  1418. switch (dmfs_high_steer_mode) {
  1419. case MLX4_STEERING_DMFS_A0_DEFAULT:
  1420. return "default performance";
  1421. case MLX4_STEERING_DMFS_A0_DYNAMIC:
  1422. return "dynamic hybrid mode";
  1423. case MLX4_STEERING_DMFS_A0_STATIC:
  1424. return "performance optimized for limited rule configuration (static)";
  1425. case MLX4_STEERING_DMFS_A0_DISABLE:
  1426. return "disabled performance optimized steering";
  1427. case MLX4_STEERING_DMFS_A0_NOT_SUPPORTED:
  1428. return "performance optimized steering not supported";
  1429. default:
  1430. return "Unrecognized mode";
  1431. }
  1432. }
  1433. #define MLX4_DMFS_A0_STEERING (1UL << 2)
  1434. static void choose_steering_mode(struct mlx4_dev *dev,
  1435. struct mlx4_dev_cap *dev_cap)
  1436. {
  1437. if (mlx4_log_num_mgm_entry_size <= 0) {
  1438. if ((-mlx4_log_num_mgm_entry_size) & MLX4_DMFS_A0_STEERING) {
  1439. if (dev->caps.dmfs_high_steer_mode ==
  1440. MLX4_STEERING_DMFS_A0_NOT_SUPPORTED)
  1441. mlx4_err(dev, "DMFS high rate mode not supported\n");
  1442. else
  1443. dev->caps.dmfs_high_steer_mode =
  1444. MLX4_STEERING_DMFS_A0_STATIC;
  1445. }
  1446. }
  1447. if (mlx4_log_num_mgm_entry_size <= 0 &&
  1448. dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_FS_EN &&
  1449. (!mlx4_is_mfunc(dev) ||
  1450. (dev_cap->fs_max_num_qp_per_entry >= (dev->num_vfs + 1))) &&
  1451. choose_log_fs_mgm_entry_size(dev_cap->fs_max_num_qp_per_entry) >=
  1452. MLX4_MIN_MGM_LOG_ENTRY_SIZE) {
  1453. dev->oper_log_mgm_entry_size =
  1454. choose_log_fs_mgm_entry_size(dev_cap->fs_max_num_qp_per_entry);
  1455. dev->caps.steering_mode = MLX4_STEERING_MODE_DEVICE_MANAGED;
  1456. dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
  1457. dev->caps.fs_log_max_ucast_qp_range_size =
  1458. dev_cap->fs_log_max_ucast_qp_range_size;
  1459. } else {
  1460. if (dev->caps.dmfs_high_steer_mode !=
  1461. MLX4_STEERING_DMFS_A0_NOT_SUPPORTED)
  1462. dev->caps.dmfs_high_steer_mode = MLX4_STEERING_DMFS_A0_DISABLE;
  1463. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER &&
  1464. dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
  1465. dev->caps.steering_mode = MLX4_STEERING_MODE_B0;
  1466. else {
  1467. dev->caps.steering_mode = MLX4_STEERING_MODE_A0;
  1468. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER ||
  1469. dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
  1470. mlx4_warn(dev, "Must have both UC_STEER and MC_STEER flags set to use B0 steering - falling back to A0 steering mode\n");
  1471. }
  1472. dev->oper_log_mgm_entry_size =
  1473. mlx4_log_num_mgm_entry_size > 0 ?
  1474. mlx4_log_num_mgm_entry_size :
  1475. MLX4_DEFAULT_MGM_LOG_ENTRY_SIZE;
  1476. dev->caps.num_qp_per_mgm = mlx4_get_qp_per_mgm(dev);
  1477. }
  1478. mlx4_dbg(dev, "Steering mode is: %s, oper_log_mgm_entry_size = %d, modparam log_num_mgm_entry_size = %d\n",
  1479. mlx4_steering_mode_str(dev->caps.steering_mode),
  1480. dev->oper_log_mgm_entry_size,
  1481. mlx4_log_num_mgm_entry_size);
  1482. }
  1483. static void choose_tunnel_offload_mode(struct mlx4_dev *dev,
  1484. struct mlx4_dev_cap *dev_cap)
  1485. {
  1486. if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED &&
  1487. dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_VXLAN_OFFLOADS)
  1488. dev->caps.tunnel_offload_mode = MLX4_TUNNEL_OFFLOAD_MODE_VXLAN;
  1489. else
  1490. dev->caps.tunnel_offload_mode = MLX4_TUNNEL_OFFLOAD_MODE_NONE;
  1491. mlx4_dbg(dev, "Tunneling offload mode is: %s\n", (dev->caps.tunnel_offload_mode
  1492. == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) ? "vxlan" : "none");
  1493. }
  1494. static int mlx4_validate_optimized_steering(struct mlx4_dev *dev)
  1495. {
  1496. int i;
  1497. struct mlx4_port_cap port_cap;
  1498. if (dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_NOT_SUPPORTED)
  1499. return -EINVAL;
  1500. for (i = 1; i <= dev->caps.num_ports; i++) {
  1501. if (mlx4_dev_port(dev, i, &port_cap)) {
  1502. mlx4_err(dev,
  1503. "QUERY_DEV_CAP command failed, can't veify DMFS high rate steering.\n");
  1504. } else if ((dev->caps.dmfs_high_steer_mode !=
  1505. MLX4_STEERING_DMFS_A0_DEFAULT) &&
  1506. (port_cap.dmfs_optimized_state ==
  1507. !!(dev->caps.dmfs_high_steer_mode ==
  1508. MLX4_STEERING_DMFS_A0_DISABLE))) {
  1509. mlx4_err(dev,
  1510. "DMFS high rate steer mode differ, driver requested %s but %s in FW.\n",
  1511. dmfs_high_rate_steering_mode_str(
  1512. dev->caps.dmfs_high_steer_mode),
  1513. (port_cap.dmfs_optimized_state ?
  1514. "enabled" : "disabled"));
  1515. }
  1516. }
  1517. return 0;
  1518. }
  1519. static int mlx4_init_fw(struct mlx4_dev *dev)
  1520. {
  1521. struct mlx4_mod_stat_cfg mlx4_cfg;
  1522. int err = 0;
  1523. if (!mlx4_is_slave(dev)) {
  1524. err = mlx4_QUERY_FW(dev);
  1525. if (err) {
  1526. if (err == -EACCES)
  1527. mlx4_info(dev, "non-primary physical function, skipping\n");
  1528. else
  1529. mlx4_err(dev, "QUERY_FW command failed, aborting\n");
  1530. return err;
  1531. }
  1532. err = mlx4_load_fw(dev);
  1533. if (err) {
  1534. mlx4_err(dev, "Failed to start FW, aborting\n");
  1535. return err;
  1536. }
  1537. mlx4_cfg.log_pg_sz_m = 1;
  1538. mlx4_cfg.log_pg_sz = 0;
  1539. err = mlx4_MOD_STAT_CFG(dev, &mlx4_cfg);
  1540. if (err)
  1541. mlx4_warn(dev, "Failed to override log_pg_sz parameter\n");
  1542. }
  1543. return err;
  1544. }
  1545. static int mlx4_init_hca(struct mlx4_dev *dev)
  1546. {
  1547. struct mlx4_priv *priv = mlx4_priv(dev);
  1548. struct mlx4_adapter adapter;
  1549. struct mlx4_dev_cap dev_cap;
  1550. struct mlx4_profile profile;
  1551. struct mlx4_init_hca_param init_hca;
  1552. u64 icm_size;
  1553. struct mlx4_config_dev_params params;
  1554. int err;
  1555. if (!mlx4_is_slave(dev)) {
  1556. err = mlx4_dev_cap(dev, &dev_cap);
  1557. if (err) {
  1558. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting\n");
  1559. return err;
  1560. }
  1561. choose_steering_mode(dev, &dev_cap);
  1562. choose_tunnel_offload_mode(dev, &dev_cap);
  1563. if (dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC &&
  1564. mlx4_is_master(dev))
  1565. dev->caps.function_caps |= MLX4_FUNC_CAP_DMFS_A0_STATIC;
  1566. err = mlx4_get_phys_port_id(dev);
  1567. if (err)
  1568. mlx4_err(dev, "Fail to get physical port id\n");
  1569. if (mlx4_is_master(dev))
  1570. mlx4_parav_master_pf_caps(dev);
  1571. if (mlx4_low_memory_profile()) {
  1572. mlx4_info(dev, "Running from within kdump kernel. Using low memory profile\n");
  1573. profile = low_mem_profile;
  1574. } else {
  1575. profile = default_profile;
  1576. }
  1577. if (dev->caps.steering_mode ==
  1578. MLX4_STEERING_MODE_DEVICE_MANAGED)
  1579. profile.num_mcg = MLX4_FS_NUM_MCG;
  1580. icm_size = mlx4_make_profile(dev, &profile, &dev_cap,
  1581. &init_hca);
  1582. if ((long long) icm_size < 0) {
  1583. err = icm_size;
  1584. return err;
  1585. }
  1586. dev->caps.max_fmr_maps = (1 << (32 - ilog2(dev->caps.num_mpts))) - 1;
  1587. init_hca.log_uar_sz = ilog2(dev->caps.num_uars);
  1588. init_hca.uar_page_sz = PAGE_SHIFT - 12;
  1589. init_hca.mw_enabled = 0;
  1590. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW ||
  1591. dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN)
  1592. init_hca.mw_enabled = INIT_HCA_TPT_MW_ENABLE;
  1593. err = mlx4_init_icm(dev, &dev_cap, &init_hca, icm_size);
  1594. if (err)
  1595. return err;
  1596. err = mlx4_INIT_HCA(dev, &init_hca);
  1597. if (err) {
  1598. mlx4_err(dev, "INIT_HCA command failed, aborting\n");
  1599. goto err_free_icm;
  1600. }
  1601. if (dev_cap.flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS) {
  1602. err = mlx4_query_func(dev, &dev_cap);
  1603. if (err < 0) {
  1604. mlx4_err(dev, "QUERY_FUNC command failed, aborting.\n");
  1605. goto err_close;
  1606. } else if (err & MLX4_QUERY_FUNC_NUM_SYS_EQS) {
  1607. dev->caps.num_eqs = dev_cap.max_eqs;
  1608. dev->caps.reserved_eqs = dev_cap.reserved_eqs;
  1609. dev->caps.reserved_uars = dev_cap.reserved_uars;
  1610. }
  1611. }
  1612. /*
  1613. * If TS is supported by FW
  1614. * read HCA frequency by QUERY_HCA command
  1615. */
  1616. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS) {
  1617. memset(&init_hca, 0, sizeof(init_hca));
  1618. err = mlx4_QUERY_HCA(dev, &init_hca);
  1619. if (err) {
  1620. mlx4_err(dev, "QUERY_HCA command failed, disable timestamp\n");
  1621. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
  1622. } else {
  1623. dev->caps.hca_core_clock =
  1624. init_hca.hca_core_clock;
  1625. }
  1626. /* In case we got HCA frequency 0 - disable timestamping
  1627. * to avoid dividing by zero
  1628. */
  1629. if (!dev->caps.hca_core_clock) {
  1630. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
  1631. mlx4_err(dev,
  1632. "HCA frequency is 0 - timestamping is not supported\n");
  1633. } else if (map_internal_clock(dev)) {
  1634. /*
  1635. * Map internal clock,
  1636. * in case of failure disable timestamping
  1637. */
  1638. dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
  1639. mlx4_err(dev, "Failed to map internal clock. Timestamping is not supported\n");
  1640. }
  1641. }
  1642. if (dev->caps.dmfs_high_steer_mode !=
  1643. MLX4_STEERING_DMFS_A0_NOT_SUPPORTED) {
  1644. if (mlx4_validate_optimized_steering(dev))
  1645. mlx4_warn(dev, "Optimized steering validation failed\n");
  1646. if (dev->caps.dmfs_high_steer_mode ==
  1647. MLX4_STEERING_DMFS_A0_DISABLE) {
  1648. dev->caps.dmfs_high_rate_qpn_base =
  1649. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
  1650. dev->caps.dmfs_high_rate_qpn_range =
  1651. MLX4_A0_STEERING_TABLE_SIZE;
  1652. }
  1653. mlx4_dbg(dev, "DMFS high rate steer mode is: %s\n",
  1654. dmfs_high_rate_steering_mode_str(
  1655. dev->caps.dmfs_high_steer_mode));
  1656. }
  1657. } else {
  1658. err = mlx4_init_slave(dev);
  1659. if (err) {
  1660. if (err != -EPROBE_DEFER)
  1661. mlx4_err(dev, "Failed to initialize slave\n");
  1662. return err;
  1663. }
  1664. err = mlx4_slave_cap(dev);
  1665. if (err) {
  1666. mlx4_err(dev, "Failed to obtain slave caps\n");
  1667. goto err_close;
  1668. }
  1669. }
  1670. if (map_bf_area(dev))
  1671. mlx4_dbg(dev, "Failed to map blue flame area\n");
  1672. /*Only the master set the ports, all the rest got it from it.*/
  1673. if (!mlx4_is_slave(dev))
  1674. mlx4_set_port_mask(dev);
  1675. err = mlx4_QUERY_ADAPTER(dev, &adapter);
  1676. if (err) {
  1677. mlx4_err(dev, "QUERY_ADAPTER command failed, aborting\n");
  1678. goto unmap_bf;
  1679. }
  1680. /* Query CONFIG_DEV parameters */
  1681. err = mlx4_config_dev_retrieval(dev, &params);
  1682. if (err && err != -ENOTSUPP) {
  1683. mlx4_err(dev, "Failed to query CONFIG_DEV parameters\n");
  1684. } else if (!err) {
  1685. dev->caps.rx_checksum_flags_port[1] = params.rx_csum_flags_port_1;
  1686. dev->caps.rx_checksum_flags_port[2] = params.rx_csum_flags_port_2;
  1687. }
  1688. priv->eq_table.inta_pin = adapter.inta_pin;
  1689. memcpy(dev->board_id, adapter.board_id, sizeof dev->board_id);
  1690. return 0;
  1691. unmap_bf:
  1692. unmap_internal_clock(dev);
  1693. unmap_bf_area(dev);
  1694. if (mlx4_is_slave(dev)) {
  1695. kfree(dev->caps.qp0_qkey);
  1696. kfree(dev->caps.qp0_tunnel);
  1697. kfree(dev->caps.qp0_proxy);
  1698. kfree(dev->caps.qp1_tunnel);
  1699. kfree(dev->caps.qp1_proxy);
  1700. }
  1701. err_close:
  1702. if (mlx4_is_slave(dev))
  1703. mlx4_slave_exit(dev);
  1704. else
  1705. mlx4_CLOSE_HCA(dev, 0);
  1706. err_free_icm:
  1707. if (!mlx4_is_slave(dev))
  1708. mlx4_free_icms(dev);
  1709. return err;
  1710. }
  1711. static int mlx4_init_counters_table(struct mlx4_dev *dev)
  1712. {
  1713. struct mlx4_priv *priv = mlx4_priv(dev);
  1714. int nent;
  1715. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
  1716. return -ENOENT;
  1717. nent = dev->caps.max_counters;
  1718. return mlx4_bitmap_init(&priv->counters_bitmap, nent, nent - 1, 0, 0);
  1719. }
  1720. static void mlx4_cleanup_counters_table(struct mlx4_dev *dev)
  1721. {
  1722. mlx4_bitmap_cleanup(&mlx4_priv(dev)->counters_bitmap);
  1723. }
  1724. int __mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx)
  1725. {
  1726. struct mlx4_priv *priv = mlx4_priv(dev);
  1727. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
  1728. return -ENOENT;
  1729. *idx = mlx4_bitmap_alloc(&priv->counters_bitmap);
  1730. if (*idx == -1)
  1731. return -ENOMEM;
  1732. return 0;
  1733. }
  1734. int mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx)
  1735. {
  1736. u64 out_param;
  1737. int err;
  1738. if (mlx4_is_mfunc(dev)) {
  1739. err = mlx4_cmd_imm(dev, 0, &out_param, RES_COUNTER,
  1740. RES_OP_RESERVE, MLX4_CMD_ALLOC_RES,
  1741. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  1742. if (!err)
  1743. *idx = get_param_l(&out_param);
  1744. return err;
  1745. }
  1746. return __mlx4_counter_alloc(dev, idx);
  1747. }
  1748. EXPORT_SYMBOL_GPL(mlx4_counter_alloc);
  1749. void __mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
  1750. {
  1751. mlx4_bitmap_free(&mlx4_priv(dev)->counters_bitmap, idx, MLX4_USE_RR);
  1752. return;
  1753. }
  1754. void mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
  1755. {
  1756. u64 in_param = 0;
  1757. if (mlx4_is_mfunc(dev)) {
  1758. set_param_l(&in_param, idx);
  1759. mlx4_cmd(dev, in_param, RES_COUNTER, RES_OP_RESERVE,
  1760. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  1761. MLX4_CMD_WRAPPED);
  1762. return;
  1763. }
  1764. __mlx4_counter_free(dev, idx);
  1765. }
  1766. EXPORT_SYMBOL_GPL(mlx4_counter_free);
  1767. static int mlx4_setup_hca(struct mlx4_dev *dev)
  1768. {
  1769. struct mlx4_priv *priv = mlx4_priv(dev);
  1770. int err;
  1771. int port;
  1772. __be32 ib_port_default_caps;
  1773. err = mlx4_init_uar_table(dev);
  1774. if (err) {
  1775. mlx4_err(dev, "Failed to initialize user access region table, aborting\n");
  1776. return err;
  1777. }
  1778. err = mlx4_uar_alloc(dev, &priv->driver_uar);
  1779. if (err) {
  1780. mlx4_err(dev, "Failed to allocate driver access region, aborting\n");
  1781. goto err_uar_table_free;
  1782. }
  1783. priv->kar = ioremap((phys_addr_t) priv->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
  1784. if (!priv->kar) {
  1785. mlx4_err(dev, "Couldn't map kernel access region, aborting\n");
  1786. err = -ENOMEM;
  1787. goto err_uar_free;
  1788. }
  1789. err = mlx4_init_pd_table(dev);
  1790. if (err) {
  1791. mlx4_err(dev, "Failed to initialize protection domain table, aborting\n");
  1792. goto err_kar_unmap;
  1793. }
  1794. err = mlx4_init_xrcd_table(dev);
  1795. if (err) {
  1796. mlx4_err(dev, "Failed to initialize reliable connection domain table, aborting\n");
  1797. goto err_pd_table_free;
  1798. }
  1799. err = mlx4_init_mr_table(dev);
  1800. if (err) {
  1801. mlx4_err(dev, "Failed to initialize memory region table, aborting\n");
  1802. goto err_xrcd_table_free;
  1803. }
  1804. if (!mlx4_is_slave(dev)) {
  1805. err = mlx4_init_mcg_table(dev);
  1806. if (err) {
  1807. mlx4_err(dev, "Failed to initialize multicast group table, aborting\n");
  1808. goto err_mr_table_free;
  1809. }
  1810. err = mlx4_config_mad_demux(dev);
  1811. if (err) {
  1812. mlx4_err(dev, "Failed in config_mad_demux, aborting\n");
  1813. goto err_mcg_table_free;
  1814. }
  1815. }
  1816. err = mlx4_init_eq_table(dev);
  1817. if (err) {
  1818. mlx4_err(dev, "Failed to initialize event queue table, aborting\n");
  1819. goto err_mcg_table_free;
  1820. }
  1821. err = mlx4_cmd_use_events(dev);
  1822. if (err) {
  1823. mlx4_err(dev, "Failed to switch to event-driven firmware commands, aborting\n");
  1824. goto err_eq_table_free;
  1825. }
  1826. err = mlx4_NOP(dev);
  1827. if (err) {
  1828. if (dev->flags & MLX4_FLAG_MSI_X) {
  1829. mlx4_warn(dev, "NOP command failed to generate MSI-X interrupt IRQ %d)\n",
  1830. priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
  1831. mlx4_warn(dev, "Trying again without MSI-X\n");
  1832. } else {
  1833. mlx4_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting\n",
  1834. priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
  1835. mlx4_err(dev, "BIOS or ACPI interrupt routing problem?\n");
  1836. }
  1837. goto err_cmd_poll;
  1838. }
  1839. mlx4_dbg(dev, "NOP command IRQ test passed\n");
  1840. err = mlx4_init_cq_table(dev);
  1841. if (err) {
  1842. mlx4_err(dev, "Failed to initialize completion queue table, aborting\n");
  1843. goto err_cmd_poll;
  1844. }
  1845. err = mlx4_init_srq_table(dev);
  1846. if (err) {
  1847. mlx4_err(dev, "Failed to initialize shared receive queue table, aborting\n");
  1848. goto err_cq_table_free;
  1849. }
  1850. err = mlx4_init_qp_table(dev);
  1851. if (err) {
  1852. mlx4_err(dev, "Failed to initialize queue pair table, aborting\n");
  1853. goto err_srq_table_free;
  1854. }
  1855. err = mlx4_init_counters_table(dev);
  1856. if (err && err != -ENOENT) {
  1857. mlx4_err(dev, "Failed to initialize counters table, aborting\n");
  1858. goto err_qp_table_free;
  1859. }
  1860. if (!mlx4_is_slave(dev)) {
  1861. for (port = 1; port <= dev->caps.num_ports; port++) {
  1862. ib_port_default_caps = 0;
  1863. err = mlx4_get_port_ib_caps(dev, port,
  1864. &ib_port_default_caps);
  1865. if (err)
  1866. mlx4_warn(dev, "failed to get port %d default ib capabilities (%d). Continuing with caps = 0\n",
  1867. port, err);
  1868. dev->caps.ib_port_def_cap[port] = ib_port_default_caps;
  1869. /* initialize per-slave default ib port capabilities */
  1870. if (mlx4_is_master(dev)) {
  1871. int i;
  1872. for (i = 0; i < dev->num_slaves; i++) {
  1873. if (i == mlx4_master_func_num(dev))
  1874. continue;
  1875. priv->mfunc.master.slave_state[i].ib_cap_mask[port] =
  1876. ib_port_default_caps;
  1877. }
  1878. }
  1879. if (mlx4_is_mfunc(dev))
  1880. dev->caps.port_ib_mtu[port] = IB_MTU_2048;
  1881. else
  1882. dev->caps.port_ib_mtu[port] = IB_MTU_4096;
  1883. err = mlx4_SET_PORT(dev, port, mlx4_is_master(dev) ?
  1884. dev->caps.pkey_table_len[port] : -1);
  1885. if (err) {
  1886. mlx4_err(dev, "Failed to set port %d, aborting\n",
  1887. port);
  1888. goto err_counters_table_free;
  1889. }
  1890. }
  1891. }
  1892. return 0;
  1893. err_counters_table_free:
  1894. mlx4_cleanup_counters_table(dev);
  1895. err_qp_table_free:
  1896. mlx4_cleanup_qp_table(dev);
  1897. err_srq_table_free:
  1898. mlx4_cleanup_srq_table(dev);
  1899. err_cq_table_free:
  1900. mlx4_cleanup_cq_table(dev);
  1901. err_cmd_poll:
  1902. mlx4_cmd_use_polling(dev);
  1903. err_eq_table_free:
  1904. mlx4_cleanup_eq_table(dev);
  1905. err_mcg_table_free:
  1906. if (!mlx4_is_slave(dev))
  1907. mlx4_cleanup_mcg_table(dev);
  1908. err_mr_table_free:
  1909. mlx4_cleanup_mr_table(dev);
  1910. err_xrcd_table_free:
  1911. mlx4_cleanup_xrcd_table(dev);
  1912. err_pd_table_free:
  1913. mlx4_cleanup_pd_table(dev);
  1914. err_kar_unmap:
  1915. iounmap(priv->kar);
  1916. err_uar_free:
  1917. mlx4_uar_free(dev, &priv->driver_uar);
  1918. err_uar_table_free:
  1919. mlx4_cleanup_uar_table(dev);
  1920. return err;
  1921. }
  1922. static void mlx4_enable_msi_x(struct mlx4_dev *dev)
  1923. {
  1924. struct mlx4_priv *priv = mlx4_priv(dev);
  1925. struct msix_entry *entries;
  1926. int i;
  1927. if (msi_x) {
  1928. int nreq = dev->caps.num_ports * num_online_cpus() + MSIX_LEGACY_SZ;
  1929. nreq = min_t(int, dev->caps.num_eqs - dev->caps.reserved_eqs,
  1930. nreq);
  1931. entries = kcalloc(nreq, sizeof *entries, GFP_KERNEL);
  1932. if (!entries)
  1933. goto no_msi;
  1934. for (i = 0; i < nreq; ++i)
  1935. entries[i].entry = i;
  1936. nreq = pci_enable_msix_range(dev->pdev, entries, 2, nreq);
  1937. if (nreq < 0) {
  1938. kfree(entries);
  1939. goto no_msi;
  1940. } else if (nreq < MSIX_LEGACY_SZ +
  1941. dev->caps.num_ports * MIN_MSIX_P_PORT) {
  1942. /*Working in legacy mode , all EQ's shared*/
  1943. dev->caps.comp_pool = 0;
  1944. dev->caps.num_comp_vectors = nreq - 1;
  1945. } else {
  1946. dev->caps.comp_pool = nreq - MSIX_LEGACY_SZ;
  1947. dev->caps.num_comp_vectors = MSIX_LEGACY_SZ - 1;
  1948. }
  1949. for (i = 0; i < nreq; ++i)
  1950. priv->eq_table.eq[i].irq = entries[i].vector;
  1951. dev->flags |= MLX4_FLAG_MSI_X;
  1952. kfree(entries);
  1953. return;
  1954. }
  1955. no_msi:
  1956. dev->caps.num_comp_vectors = 1;
  1957. dev->caps.comp_pool = 0;
  1958. for (i = 0; i < 2; ++i)
  1959. priv->eq_table.eq[i].irq = dev->pdev->irq;
  1960. }
  1961. static int mlx4_init_port_info(struct mlx4_dev *dev, int port)
  1962. {
  1963. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  1964. int err = 0;
  1965. info->dev = dev;
  1966. info->port = port;
  1967. if (!mlx4_is_slave(dev)) {
  1968. mlx4_init_mac_table(dev, &info->mac_table);
  1969. mlx4_init_vlan_table(dev, &info->vlan_table);
  1970. mlx4_init_roce_gid_table(dev, &info->gid_table);
  1971. info->base_qpn = mlx4_get_base_qpn(dev, port);
  1972. }
  1973. sprintf(info->dev_name, "mlx4_port%d", port);
  1974. info->port_attr.attr.name = info->dev_name;
  1975. if (mlx4_is_mfunc(dev))
  1976. info->port_attr.attr.mode = S_IRUGO;
  1977. else {
  1978. info->port_attr.attr.mode = S_IRUGO | S_IWUSR;
  1979. info->port_attr.store = set_port_type;
  1980. }
  1981. info->port_attr.show = show_port_type;
  1982. sysfs_attr_init(&info->port_attr.attr);
  1983. err = device_create_file(&dev->pdev->dev, &info->port_attr);
  1984. if (err) {
  1985. mlx4_err(dev, "Failed to create file for port %d\n", port);
  1986. info->port = -1;
  1987. }
  1988. sprintf(info->dev_mtu_name, "mlx4_port%d_mtu", port);
  1989. info->port_mtu_attr.attr.name = info->dev_mtu_name;
  1990. if (mlx4_is_mfunc(dev))
  1991. info->port_mtu_attr.attr.mode = S_IRUGO;
  1992. else {
  1993. info->port_mtu_attr.attr.mode = S_IRUGO | S_IWUSR;
  1994. info->port_mtu_attr.store = set_port_ib_mtu;
  1995. }
  1996. info->port_mtu_attr.show = show_port_ib_mtu;
  1997. sysfs_attr_init(&info->port_mtu_attr.attr);
  1998. err = device_create_file(&dev->pdev->dev, &info->port_mtu_attr);
  1999. if (err) {
  2000. mlx4_err(dev, "Failed to create mtu file for port %d\n", port);
  2001. device_remove_file(&info->dev->pdev->dev, &info->port_attr);
  2002. info->port = -1;
  2003. }
  2004. return err;
  2005. }
  2006. static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
  2007. {
  2008. if (info->port < 0)
  2009. return;
  2010. device_remove_file(&info->dev->pdev->dev, &info->port_attr);
  2011. device_remove_file(&info->dev->pdev->dev, &info->port_mtu_attr);
  2012. }
  2013. static int mlx4_init_steering(struct mlx4_dev *dev)
  2014. {
  2015. struct mlx4_priv *priv = mlx4_priv(dev);
  2016. int num_entries = dev->caps.num_ports;
  2017. int i, j;
  2018. priv->steer = kzalloc(sizeof(struct mlx4_steer) * num_entries, GFP_KERNEL);
  2019. if (!priv->steer)
  2020. return -ENOMEM;
  2021. for (i = 0; i < num_entries; i++)
  2022. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  2023. INIT_LIST_HEAD(&priv->steer[i].promisc_qps[j]);
  2024. INIT_LIST_HEAD(&priv->steer[i].steer_entries[j]);
  2025. }
  2026. return 0;
  2027. }
  2028. static void mlx4_clear_steering(struct mlx4_dev *dev)
  2029. {
  2030. struct mlx4_priv *priv = mlx4_priv(dev);
  2031. struct mlx4_steer_index *entry, *tmp_entry;
  2032. struct mlx4_promisc_qp *pqp, *tmp_pqp;
  2033. int num_entries = dev->caps.num_ports;
  2034. int i, j;
  2035. for (i = 0; i < num_entries; i++) {
  2036. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  2037. list_for_each_entry_safe(pqp, tmp_pqp,
  2038. &priv->steer[i].promisc_qps[j],
  2039. list) {
  2040. list_del(&pqp->list);
  2041. kfree(pqp);
  2042. }
  2043. list_for_each_entry_safe(entry, tmp_entry,
  2044. &priv->steer[i].steer_entries[j],
  2045. list) {
  2046. list_del(&entry->list);
  2047. list_for_each_entry_safe(pqp, tmp_pqp,
  2048. &entry->duplicates,
  2049. list) {
  2050. list_del(&pqp->list);
  2051. kfree(pqp);
  2052. }
  2053. kfree(entry);
  2054. }
  2055. }
  2056. }
  2057. kfree(priv->steer);
  2058. }
  2059. static int extended_func_num(struct pci_dev *pdev)
  2060. {
  2061. return PCI_SLOT(pdev->devfn) * 8 + PCI_FUNC(pdev->devfn);
  2062. }
  2063. #define MLX4_OWNER_BASE 0x8069c
  2064. #define MLX4_OWNER_SIZE 4
  2065. static int mlx4_get_ownership(struct mlx4_dev *dev)
  2066. {
  2067. void __iomem *owner;
  2068. u32 ret;
  2069. if (pci_channel_offline(dev->pdev))
  2070. return -EIO;
  2071. owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
  2072. MLX4_OWNER_SIZE);
  2073. if (!owner) {
  2074. mlx4_err(dev, "Failed to obtain ownership bit\n");
  2075. return -ENOMEM;
  2076. }
  2077. ret = readl(owner);
  2078. iounmap(owner);
  2079. return (int) !!ret;
  2080. }
  2081. static void mlx4_free_ownership(struct mlx4_dev *dev)
  2082. {
  2083. void __iomem *owner;
  2084. if (pci_channel_offline(dev->pdev))
  2085. return;
  2086. owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
  2087. MLX4_OWNER_SIZE);
  2088. if (!owner) {
  2089. mlx4_err(dev, "Failed to obtain ownership bit\n");
  2090. return;
  2091. }
  2092. writel(0, owner);
  2093. msleep(1000);
  2094. iounmap(owner);
  2095. }
  2096. #define SRIOV_VALID_STATE(flags) (!!((flags) & MLX4_FLAG_SRIOV) ==\
  2097. !!((flags) & MLX4_FLAG_MASTER))
  2098. static u64 mlx4_enable_sriov(struct mlx4_dev *dev, struct pci_dev *pdev,
  2099. u8 total_vfs, int existing_vfs)
  2100. {
  2101. u64 dev_flags = dev->flags;
  2102. int err = 0;
  2103. atomic_inc(&pf_loading);
  2104. if (dev->flags & MLX4_FLAG_SRIOV) {
  2105. if (existing_vfs != total_vfs) {
  2106. mlx4_err(dev, "SR-IOV was already enabled, but with num_vfs (%d) different than requested (%d)\n",
  2107. existing_vfs, total_vfs);
  2108. total_vfs = existing_vfs;
  2109. }
  2110. }
  2111. dev->dev_vfs = kzalloc(total_vfs * sizeof(*dev->dev_vfs), GFP_KERNEL);
  2112. if (NULL == dev->dev_vfs) {
  2113. mlx4_err(dev, "Failed to allocate memory for VFs\n");
  2114. goto disable_sriov;
  2115. }
  2116. if (!(dev->flags & MLX4_FLAG_SRIOV)) {
  2117. mlx4_warn(dev, "Enabling SR-IOV with %d VFs\n", total_vfs);
  2118. err = pci_enable_sriov(pdev, total_vfs);
  2119. }
  2120. if (err) {
  2121. mlx4_err(dev, "Failed to enable SR-IOV, continuing without SR-IOV (err = %d)\n",
  2122. err);
  2123. goto disable_sriov;
  2124. } else {
  2125. mlx4_warn(dev, "Running in master mode\n");
  2126. dev_flags |= MLX4_FLAG_SRIOV |
  2127. MLX4_FLAG_MASTER;
  2128. dev_flags &= ~MLX4_FLAG_SLAVE;
  2129. dev->num_vfs = total_vfs;
  2130. }
  2131. return dev_flags;
  2132. disable_sriov:
  2133. atomic_dec(&pf_loading);
  2134. dev->num_vfs = 0;
  2135. kfree(dev->dev_vfs);
  2136. return dev_flags & ~MLX4_FLAG_MASTER;
  2137. }
  2138. enum {
  2139. MLX4_DEV_CAP_CHECK_NUM_VFS_ABOVE_64 = -1,
  2140. };
  2141. static int mlx4_check_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
  2142. int *nvfs)
  2143. {
  2144. int requested_vfs = nvfs[0] + nvfs[1] + nvfs[2];
  2145. /* Checking for 64 VFs as a limitation of CX2 */
  2146. if (!(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_80_VFS) &&
  2147. requested_vfs >= 64) {
  2148. mlx4_err(dev, "Requested %d VFs, but FW does not support more than 64\n",
  2149. requested_vfs);
  2150. return MLX4_DEV_CAP_CHECK_NUM_VFS_ABOVE_64;
  2151. }
  2152. return 0;
  2153. }
  2154. static int mlx4_load_one(struct pci_dev *pdev, int pci_dev_data,
  2155. int total_vfs, int *nvfs, struct mlx4_priv *priv)
  2156. {
  2157. struct mlx4_dev *dev;
  2158. unsigned sum = 0;
  2159. int err;
  2160. int port;
  2161. int i;
  2162. struct mlx4_dev_cap *dev_cap = NULL;
  2163. int existing_vfs = 0;
  2164. dev = &priv->dev;
  2165. INIT_LIST_HEAD(&priv->ctx_list);
  2166. spin_lock_init(&priv->ctx_lock);
  2167. mutex_init(&priv->port_mutex);
  2168. INIT_LIST_HEAD(&priv->pgdir_list);
  2169. mutex_init(&priv->pgdir_mutex);
  2170. INIT_LIST_HEAD(&priv->bf_list);
  2171. mutex_init(&priv->bf_mutex);
  2172. dev->rev_id = pdev->revision;
  2173. dev->numa_node = dev_to_node(&pdev->dev);
  2174. /* Detect if this device is a virtual function */
  2175. if (pci_dev_data & MLX4_PCI_DEV_IS_VF) {
  2176. mlx4_warn(dev, "Detected virtual function - running in slave mode\n");
  2177. dev->flags |= MLX4_FLAG_SLAVE;
  2178. } else {
  2179. /* We reset the device and enable SRIOV only for physical
  2180. * devices. Try to claim ownership on the device;
  2181. * if already taken, skip -- do not allow multiple PFs */
  2182. err = mlx4_get_ownership(dev);
  2183. if (err) {
  2184. if (err < 0)
  2185. return err;
  2186. else {
  2187. mlx4_warn(dev, "Multiple PFs not yet supported - Skipping PF\n");
  2188. return -EINVAL;
  2189. }
  2190. }
  2191. atomic_set(&priv->opreq_count, 0);
  2192. INIT_WORK(&priv->opreq_task, mlx4_opreq_action);
  2193. /*
  2194. * Now reset the HCA before we touch the PCI capabilities or
  2195. * attempt a firmware command, since a boot ROM may have left
  2196. * the HCA in an undefined state.
  2197. */
  2198. err = mlx4_reset(dev);
  2199. if (err) {
  2200. mlx4_err(dev, "Failed to reset HCA, aborting\n");
  2201. goto err_sriov;
  2202. }
  2203. if (total_vfs) {
  2204. dev->flags = MLX4_FLAG_MASTER;
  2205. existing_vfs = pci_num_vf(pdev);
  2206. if (existing_vfs)
  2207. dev->flags |= MLX4_FLAG_SRIOV;
  2208. dev->num_vfs = total_vfs;
  2209. }
  2210. }
  2211. slave_start:
  2212. err = mlx4_cmd_init(dev);
  2213. if (err) {
  2214. mlx4_err(dev, "Failed to init command interface, aborting\n");
  2215. goto err_sriov;
  2216. }
  2217. /* In slave functions, the communication channel must be initialized
  2218. * before posting commands. Also, init num_slaves before calling
  2219. * mlx4_init_hca */
  2220. if (mlx4_is_mfunc(dev)) {
  2221. if (mlx4_is_master(dev)) {
  2222. dev->num_slaves = MLX4_MAX_NUM_SLAVES;
  2223. } else {
  2224. dev->num_slaves = 0;
  2225. err = mlx4_multi_func_init(dev);
  2226. if (err) {
  2227. mlx4_err(dev, "Failed to init slave mfunc interface, aborting\n");
  2228. goto err_cmd;
  2229. }
  2230. }
  2231. }
  2232. err = mlx4_init_fw(dev);
  2233. if (err) {
  2234. mlx4_err(dev, "Failed to init fw, aborting.\n");
  2235. goto err_mfunc;
  2236. }
  2237. if (mlx4_is_master(dev)) {
  2238. /* when we hit the goto slave_start below, dev_cap already initialized */
  2239. if (!dev_cap) {
  2240. dev_cap = kzalloc(sizeof(*dev_cap), GFP_KERNEL);
  2241. if (!dev_cap) {
  2242. err = -ENOMEM;
  2243. goto err_fw;
  2244. }
  2245. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  2246. if (err) {
  2247. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  2248. goto err_fw;
  2249. }
  2250. if (mlx4_check_dev_cap(dev, dev_cap, nvfs))
  2251. goto err_fw;
  2252. if (!(dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS)) {
  2253. u64 dev_flags = mlx4_enable_sriov(dev, pdev, total_vfs,
  2254. existing_vfs);
  2255. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  2256. dev->flags = dev_flags;
  2257. if (!SRIOV_VALID_STATE(dev->flags)) {
  2258. mlx4_err(dev, "Invalid SRIOV state\n");
  2259. goto err_sriov;
  2260. }
  2261. err = mlx4_reset(dev);
  2262. if (err) {
  2263. mlx4_err(dev, "Failed to reset HCA, aborting.\n");
  2264. goto err_sriov;
  2265. }
  2266. goto slave_start;
  2267. }
  2268. } else {
  2269. /* Legacy mode FW requires SRIOV to be enabled before
  2270. * doing QUERY_DEV_CAP, since max_eq's value is different if
  2271. * SRIOV is enabled.
  2272. */
  2273. memset(dev_cap, 0, sizeof(*dev_cap));
  2274. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  2275. if (err) {
  2276. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  2277. goto err_fw;
  2278. }
  2279. if (mlx4_check_dev_cap(dev, dev_cap, nvfs))
  2280. goto err_fw;
  2281. }
  2282. }
  2283. err = mlx4_init_hca(dev);
  2284. if (err) {
  2285. if (err == -EACCES) {
  2286. /* Not primary Physical function
  2287. * Running in slave mode */
  2288. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  2289. /* We're not a PF */
  2290. if (dev->flags & MLX4_FLAG_SRIOV) {
  2291. if (!existing_vfs)
  2292. pci_disable_sriov(pdev);
  2293. if (mlx4_is_master(dev))
  2294. atomic_dec(&pf_loading);
  2295. dev->flags &= ~MLX4_FLAG_SRIOV;
  2296. }
  2297. if (!mlx4_is_slave(dev))
  2298. mlx4_free_ownership(dev);
  2299. dev->flags |= MLX4_FLAG_SLAVE;
  2300. dev->flags &= ~MLX4_FLAG_MASTER;
  2301. goto slave_start;
  2302. } else
  2303. goto err_fw;
  2304. }
  2305. if (mlx4_is_master(dev) && (dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS)) {
  2306. u64 dev_flags = mlx4_enable_sriov(dev, pdev, total_vfs, existing_vfs);
  2307. if ((dev->flags ^ dev_flags) & (MLX4_FLAG_MASTER | MLX4_FLAG_SLAVE)) {
  2308. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_VHCR);
  2309. dev->flags = dev_flags;
  2310. err = mlx4_cmd_init(dev);
  2311. if (err) {
  2312. /* Only VHCR is cleaned up, so could still
  2313. * send FW commands
  2314. */
  2315. mlx4_err(dev, "Failed to init VHCR command interface, aborting\n");
  2316. goto err_close;
  2317. }
  2318. } else {
  2319. dev->flags = dev_flags;
  2320. }
  2321. if (!SRIOV_VALID_STATE(dev->flags)) {
  2322. mlx4_err(dev, "Invalid SRIOV state\n");
  2323. goto err_close;
  2324. }
  2325. }
  2326. /* check if the device is functioning at its maximum possible speed.
  2327. * No return code for this call, just warn the user in case of PCI
  2328. * express device capabilities are under-satisfied by the bus.
  2329. */
  2330. if (!mlx4_is_slave(dev))
  2331. mlx4_check_pcie_caps(dev);
  2332. /* In master functions, the communication channel must be initialized
  2333. * after obtaining its address from fw */
  2334. if (mlx4_is_master(dev)) {
  2335. int ib_ports = 0;
  2336. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  2337. ib_ports++;
  2338. if (ib_ports &&
  2339. (num_vfs_argc > 1 || probe_vfs_argc > 1)) {
  2340. mlx4_err(dev,
  2341. "Invalid syntax of num_vfs/probe_vfs with IB port - single port VFs syntax is only supported when all ports are configured as ethernet\n");
  2342. err = -EINVAL;
  2343. goto err_close;
  2344. }
  2345. if (dev->caps.num_ports < 2 &&
  2346. num_vfs_argc > 1) {
  2347. err = -EINVAL;
  2348. mlx4_err(dev,
  2349. "Error: Trying to configure VFs on port 2, but HCA has only %d physical ports\n",
  2350. dev->caps.num_ports);
  2351. goto err_close;
  2352. }
  2353. memcpy(dev->nvfs, nvfs, sizeof(dev->nvfs));
  2354. for (i = 0; i < sizeof(dev->nvfs)/sizeof(dev->nvfs[0]); i++) {
  2355. unsigned j;
  2356. for (j = 0; j < dev->nvfs[i]; ++sum, ++j) {
  2357. dev->dev_vfs[sum].min_port = i < 2 ? i + 1 : 1;
  2358. dev->dev_vfs[sum].n_ports = i < 2 ? 1 :
  2359. dev->caps.num_ports;
  2360. }
  2361. }
  2362. /* In master functions, the communication channel
  2363. * must be initialized after obtaining its address from fw
  2364. */
  2365. err = mlx4_multi_func_init(dev);
  2366. if (err) {
  2367. mlx4_err(dev, "Failed to init master mfunc interface, aborting.\n");
  2368. goto err_close;
  2369. }
  2370. }
  2371. err = mlx4_alloc_eq_table(dev);
  2372. if (err)
  2373. goto err_master_mfunc;
  2374. priv->msix_ctl.pool_bm = 0;
  2375. mutex_init(&priv->msix_ctl.pool_lock);
  2376. mlx4_enable_msi_x(dev);
  2377. if ((mlx4_is_mfunc(dev)) &&
  2378. !(dev->flags & MLX4_FLAG_MSI_X)) {
  2379. err = -ENOSYS;
  2380. mlx4_err(dev, "INTx is not supported in multi-function mode, aborting\n");
  2381. goto err_free_eq;
  2382. }
  2383. if (!mlx4_is_slave(dev)) {
  2384. err = mlx4_init_steering(dev);
  2385. if (err)
  2386. goto err_disable_msix;
  2387. }
  2388. err = mlx4_setup_hca(dev);
  2389. if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X) &&
  2390. !mlx4_is_mfunc(dev)) {
  2391. dev->flags &= ~MLX4_FLAG_MSI_X;
  2392. dev->caps.num_comp_vectors = 1;
  2393. dev->caps.comp_pool = 0;
  2394. pci_disable_msix(pdev);
  2395. err = mlx4_setup_hca(dev);
  2396. }
  2397. if (err)
  2398. goto err_steer;
  2399. mlx4_init_quotas(dev);
  2400. for (port = 1; port <= dev->caps.num_ports; port++) {
  2401. err = mlx4_init_port_info(dev, port);
  2402. if (err)
  2403. goto err_port;
  2404. }
  2405. err = mlx4_register_device(dev);
  2406. if (err)
  2407. goto err_port;
  2408. mlx4_request_modules(dev);
  2409. mlx4_sense_init(dev);
  2410. mlx4_start_sense(dev);
  2411. priv->removed = 0;
  2412. if (mlx4_is_master(dev) && dev->num_vfs)
  2413. atomic_dec(&pf_loading);
  2414. kfree(dev_cap);
  2415. return 0;
  2416. err_port:
  2417. for (--port; port >= 1; --port)
  2418. mlx4_cleanup_port_info(&priv->port[port]);
  2419. mlx4_cleanup_counters_table(dev);
  2420. mlx4_cleanup_qp_table(dev);
  2421. mlx4_cleanup_srq_table(dev);
  2422. mlx4_cleanup_cq_table(dev);
  2423. mlx4_cmd_use_polling(dev);
  2424. mlx4_cleanup_eq_table(dev);
  2425. mlx4_cleanup_mcg_table(dev);
  2426. mlx4_cleanup_mr_table(dev);
  2427. mlx4_cleanup_xrcd_table(dev);
  2428. mlx4_cleanup_pd_table(dev);
  2429. mlx4_cleanup_uar_table(dev);
  2430. err_steer:
  2431. if (!mlx4_is_slave(dev))
  2432. mlx4_clear_steering(dev);
  2433. err_disable_msix:
  2434. if (dev->flags & MLX4_FLAG_MSI_X)
  2435. pci_disable_msix(pdev);
  2436. err_free_eq:
  2437. mlx4_free_eq_table(dev);
  2438. err_master_mfunc:
  2439. if (mlx4_is_master(dev))
  2440. mlx4_multi_func_cleanup(dev);
  2441. if (mlx4_is_slave(dev)) {
  2442. kfree(dev->caps.qp0_qkey);
  2443. kfree(dev->caps.qp0_tunnel);
  2444. kfree(dev->caps.qp0_proxy);
  2445. kfree(dev->caps.qp1_tunnel);
  2446. kfree(dev->caps.qp1_proxy);
  2447. }
  2448. err_close:
  2449. mlx4_close_hca(dev);
  2450. err_fw:
  2451. mlx4_close_fw(dev);
  2452. err_mfunc:
  2453. if (mlx4_is_slave(dev))
  2454. mlx4_multi_func_cleanup(dev);
  2455. err_cmd:
  2456. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  2457. err_sriov:
  2458. if (dev->flags & MLX4_FLAG_SRIOV && !existing_vfs)
  2459. pci_disable_sriov(pdev);
  2460. if (mlx4_is_master(dev) && dev->num_vfs)
  2461. atomic_dec(&pf_loading);
  2462. kfree(priv->dev.dev_vfs);
  2463. if (!mlx4_is_slave(dev))
  2464. mlx4_free_ownership(dev);
  2465. kfree(dev_cap);
  2466. return err;
  2467. }
  2468. static int __mlx4_init_one(struct pci_dev *pdev, int pci_dev_data,
  2469. struct mlx4_priv *priv)
  2470. {
  2471. int err;
  2472. int nvfs[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  2473. int prb_vf[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  2474. const int param_map[MLX4_MAX_PORTS + 1][MLX4_MAX_PORTS + 1] = {
  2475. {2, 0, 0}, {0, 1, 2}, {0, 1, 2} };
  2476. unsigned total_vfs = 0;
  2477. unsigned int i;
  2478. pr_info(DRV_NAME ": Initializing %s\n", pci_name(pdev));
  2479. err = pci_enable_device(pdev);
  2480. if (err) {
  2481. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  2482. return err;
  2483. }
  2484. /* Due to requirement that all VFs and the PF are *guaranteed* 2 MACS
  2485. * per port, we must limit the number of VFs to 63 (since their are
  2486. * 128 MACs)
  2487. */
  2488. for (i = 0; i < sizeof(nvfs)/sizeof(nvfs[0]) && i < num_vfs_argc;
  2489. total_vfs += nvfs[param_map[num_vfs_argc - 1][i]], i++) {
  2490. nvfs[param_map[num_vfs_argc - 1][i]] = num_vfs[i];
  2491. if (nvfs[i] < 0) {
  2492. dev_err(&pdev->dev, "num_vfs module parameter cannot be negative\n");
  2493. err = -EINVAL;
  2494. goto err_disable_pdev;
  2495. }
  2496. }
  2497. for (i = 0; i < sizeof(prb_vf)/sizeof(prb_vf[0]) && i < probe_vfs_argc;
  2498. i++) {
  2499. prb_vf[param_map[probe_vfs_argc - 1][i]] = probe_vf[i];
  2500. if (prb_vf[i] < 0 || prb_vf[i] > nvfs[i]) {
  2501. dev_err(&pdev->dev, "probe_vf module parameter cannot be negative or greater than num_vfs\n");
  2502. err = -EINVAL;
  2503. goto err_disable_pdev;
  2504. }
  2505. }
  2506. if (total_vfs >= MLX4_MAX_NUM_VF) {
  2507. dev_err(&pdev->dev,
  2508. "Requested more VF's (%d) than allowed (%d)\n",
  2509. total_vfs, MLX4_MAX_NUM_VF - 1);
  2510. err = -EINVAL;
  2511. goto err_disable_pdev;
  2512. }
  2513. for (i = 0; i < MLX4_MAX_PORTS; i++) {
  2514. if (nvfs[i] + nvfs[2] >= MLX4_MAX_NUM_VF_P_PORT) {
  2515. dev_err(&pdev->dev,
  2516. "Requested more VF's (%d) for port (%d) than allowed (%d)\n",
  2517. nvfs[i] + nvfs[2], i + 1,
  2518. MLX4_MAX_NUM_VF_P_PORT - 1);
  2519. err = -EINVAL;
  2520. goto err_disable_pdev;
  2521. }
  2522. }
  2523. /* Check for BARs. */
  2524. if (!(pci_dev_data & MLX4_PCI_DEV_IS_VF) &&
  2525. !(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  2526. dev_err(&pdev->dev, "Missing DCS, aborting (driver_data: 0x%x, pci_resource_flags(pdev, 0):0x%lx)\n",
  2527. pci_dev_data, pci_resource_flags(pdev, 0));
  2528. err = -ENODEV;
  2529. goto err_disable_pdev;
  2530. }
  2531. if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  2532. dev_err(&pdev->dev, "Missing UAR, aborting\n");
  2533. err = -ENODEV;
  2534. goto err_disable_pdev;
  2535. }
  2536. err = pci_request_regions(pdev, DRV_NAME);
  2537. if (err) {
  2538. dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
  2539. goto err_disable_pdev;
  2540. }
  2541. pci_set_master(pdev);
  2542. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  2543. if (err) {
  2544. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
  2545. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2546. if (err) {
  2547. dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
  2548. goto err_release_regions;
  2549. }
  2550. }
  2551. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  2552. if (err) {
  2553. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
  2554. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2555. if (err) {
  2556. dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, aborting\n");
  2557. goto err_release_regions;
  2558. }
  2559. }
  2560. /* Allow large DMA segments, up to the firmware limit of 1 GB */
  2561. dma_set_max_seg_size(&pdev->dev, 1024 * 1024 * 1024);
  2562. /* Detect if this device is a virtual function */
  2563. if (pci_dev_data & MLX4_PCI_DEV_IS_VF) {
  2564. /* When acting as pf, we normally skip vfs unless explicitly
  2565. * requested to probe them.
  2566. */
  2567. if (total_vfs) {
  2568. unsigned vfs_offset = 0;
  2569. for (i = 0; i < sizeof(nvfs)/sizeof(nvfs[0]) &&
  2570. vfs_offset + nvfs[i] < extended_func_num(pdev);
  2571. vfs_offset += nvfs[i], i++)
  2572. ;
  2573. if (i == sizeof(nvfs)/sizeof(nvfs[0])) {
  2574. err = -ENODEV;
  2575. goto err_release_regions;
  2576. }
  2577. if ((extended_func_num(pdev) - vfs_offset)
  2578. > prb_vf[i]) {
  2579. dev_warn(&pdev->dev, "Skipping virtual function:%d\n",
  2580. extended_func_num(pdev));
  2581. err = -ENODEV;
  2582. goto err_release_regions;
  2583. }
  2584. }
  2585. }
  2586. err = mlx4_load_one(pdev, pci_dev_data, total_vfs, nvfs, priv);
  2587. if (err)
  2588. goto err_release_regions;
  2589. return 0;
  2590. err_release_regions:
  2591. pci_release_regions(pdev);
  2592. err_disable_pdev:
  2593. pci_disable_device(pdev);
  2594. pci_set_drvdata(pdev, NULL);
  2595. return err;
  2596. }
  2597. static int mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  2598. {
  2599. struct mlx4_priv *priv;
  2600. struct mlx4_dev *dev;
  2601. int ret;
  2602. printk_once(KERN_INFO "%s", mlx4_version);
  2603. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  2604. if (!priv)
  2605. return -ENOMEM;
  2606. dev = &priv->dev;
  2607. dev->pdev = pdev;
  2608. pci_set_drvdata(pdev, dev);
  2609. priv->pci_dev_data = id->driver_data;
  2610. ret = __mlx4_init_one(pdev, id->driver_data, priv);
  2611. if (ret)
  2612. kfree(priv);
  2613. return ret;
  2614. }
  2615. static void mlx4_unload_one(struct pci_dev *pdev)
  2616. {
  2617. struct mlx4_dev *dev = pci_get_drvdata(pdev);
  2618. struct mlx4_priv *priv = mlx4_priv(dev);
  2619. int pci_dev_data;
  2620. int p;
  2621. int active_vfs = 0;
  2622. if (priv->removed)
  2623. return;
  2624. pci_dev_data = priv->pci_dev_data;
  2625. /* Disabling SR-IOV is not allowed while there are active vf's */
  2626. if (mlx4_is_master(dev)) {
  2627. active_vfs = mlx4_how_many_lives_vf(dev);
  2628. if (active_vfs) {
  2629. pr_warn("Removing PF when there are active VF's !!\n");
  2630. pr_warn("Will not disable SR-IOV.\n");
  2631. }
  2632. }
  2633. mlx4_stop_sense(dev);
  2634. mlx4_unregister_device(dev);
  2635. for (p = 1; p <= dev->caps.num_ports; p++) {
  2636. mlx4_cleanup_port_info(&priv->port[p]);
  2637. mlx4_CLOSE_PORT(dev, p);
  2638. }
  2639. if (mlx4_is_master(dev))
  2640. mlx4_free_resource_tracker(dev,
  2641. RES_TR_FREE_SLAVES_ONLY);
  2642. mlx4_cleanup_counters_table(dev);
  2643. mlx4_cleanup_qp_table(dev);
  2644. mlx4_cleanup_srq_table(dev);
  2645. mlx4_cleanup_cq_table(dev);
  2646. mlx4_cmd_use_polling(dev);
  2647. mlx4_cleanup_eq_table(dev);
  2648. mlx4_cleanup_mcg_table(dev);
  2649. mlx4_cleanup_mr_table(dev);
  2650. mlx4_cleanup_xrcd_table(dev);
  2651. mlx4_cleanup_pd_table(dev);
  2652. if (mlx4_is_master(dev))
  2653. mlx4_free_resource_tracker(dev,
  2654. RES_TR_FREE_STRUCTS_ONLY);
  2655. iounmap(priv->kar);
  2656. mlx4_uar_free(dev, &priv->driver_uar);
  2657. mlx4_cleanup_uar_table(dev);
  2658. if (!mlx4_is_slave(dev))
  2659. mlx4_clear_steering(dev);
  2660. mlx4_free_eq_table(dev);
  2661. if (mlx4_is_master(dev))
  2662. mlx4_multi_func_cleanup(dev);
  2663. mlx4_close_hca(dev);
  2664. mlx4_close_fw(dev);
  2665. if (mlx4_is_slave(dev))
  2666. mlx4_multi_func_cleanup(dev);
  2667. mlx4_cmd_cleanup(dev, MLX4_CMD_CLEANUP_ALL);
  2668. if (dev->flags & MLX4_FLAG_MSI_X)
  2669. pci_disable_msix(pdev);
  2670. if (dev->flags & MLX4_FLAG_SRIOV && !active_vfs) {
  2671. mlx4_warn(dev, "Disabling SR-IOV\n");
  2672. pci_disable_sriov(pdev);
  2673. dev->flags &= ~MLX4_FLAG_SRIOV;
  2674. dev->num_vfs = 0;
  2675. }
  2676. if (!mlx4_is_slave(dev))
  2677. mlx4_free_ownership(dev);
  2678. kfree(dev->caps.qp0_qkey);
  2679. kfree(dev->caps.qp0_tunnel);
  2680. kfree(dev->caps.qp0_proxy);
  2681. kfree(dev->caps.qp1_tunnel);
  2682. kfree(dev->caps.qp1_proxy);
  2683. kfree(dev->dev_vfs);
  2684. memset(priv, 0, sizeof(*priv));
  2685. priv->pci_dev_data = pci_dev_data;
  2686. priv->removed = 1;
  2687. }
  2688. static void mlx4_remove_one(struct pci_dev *pdev)
  2689. {
  2690. struct mlx4_dev *dev = pci_get_drvdata(pdev);
  2691. struct mlx4_priv *priv = mlx4_priv(dev);
  2692. mlx4_unload_one(pdev);
  2693. pci_release_regions(pdev);
  2694. pci_disable_device(pdev);
  2695. kfree(priv);
  2696. pci_set_drvdata(pdev, NULL);
  2697. }
  2698. int mlx4_restart_one(struct pci_dev *pdev)
  2699. {
  2700. struct mlx4_dev *dev = pci_get_drvdata(pdev);
  2701. struct mlx4_priv *priv = mlx4_priv(dev);
  2702. int nvfs[MLX4_MAX_PORTS + 1] = {0, 0, 0};
  2703. int pci_dev_data, err, total_vfs;
  2704. pci_dev_data = priv->pci_dev_data;
  2705. total_vfs = dev->num_vfs;
  2706. memcpy(nvfs, dev->nvfs, sizeof(dev->nvfs));
  2707. mlx4_unload_one(pdev);
  2708. err = mlx4_load_one(pdev, pci_dev_data, total_vfs, nvfs, priv);
  2709. if (err) {
  2710. mlx4_err(dev, "%s: ERROR: mlx4_load_one failed, pci_name=%s, err=%d\n",
  2711. __func__, pci_name(pdev), err);
  2712. return err;
  2713. }
  2714. return err;
  2715. }
  2716. static const struct pci_device_id mlx4_pci_table[] = {
  2717. /* MT25408 "Hermon" SDR */
  2718. { PCI_VDEVICE(MELLANOX, 0x6340), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2719. /* MT25408 "Hermon" DDR */
  2720. { PCI_VDEVICE(MELLANOX, 0x634a), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2721. /* MT25408 "Hermon" QDR */
  2722. { PCI_VDEVICE(MELLANOX, 0x6354), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2723. /* MT25408 "Hermon" DDR PCIe gen2 */
  2724. { PCI_VDEVICE(MELLANOX, 0x6732), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2725. /* MT25408 "Hermon" QDR PCIe gen2 */
  2726. { PCI_VDEVICE(MELLANOX, 0x673c), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2727. /* MT25408 "Hermon" EN 10GigE */
  2728. { PCI_VDEVICE(MELLANOX, 0x6368), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2729. /* MT25408 "Hermon" EN 10GigE PCIe gen2 */
  2730. { PCI_VDEVICE(MELLANOX, 0x6750), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2731. /* MT25458 ConnectX EN 10GBASE-T 10GigE */
  2732. { PCI_VDEVICE(MELLANOX, 0x6372), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2733. /* MT25458 ConnectX EN 10GBASE-T+Gen2 10GigE */
  2734. { PCI_VDEVICE(MELLANOX, 0x675a), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2735. /* MT26468 ConnectX EN 10GigE PCIe gen2*/
  2736. { PCI_VDEVICE(MELLANOX, 0x6764), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2737. /* MT26438 ConnectX EN 40GigE PCIe gen2 5GT/s */
  2738. { PCI_VDEVICE(MELLANOX, 0x6746), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2739. /* MT26478 ConnectX2 40GigE PCIe gen2 */
  2740. { PCI_VDEVICE(MELLANOX, 0x676e), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  2741. /* MT25400 Family [ConnectX-2 Virtual Function] */
  2742. { PCI_VDEVICE(MELLANOX, 0x1002), MLX4_PCI_DEV_IS_VF },
  2743. /* MT27500 Family [ConnectX-3] */
  2744. { PCI_VDEVICE(MELLANOX, 0x1003), 0 },
  2745. /* MT27500 Family [ConnectX-3 Virtual Function] */
  2746. { PCI_VDEVICE(MELLANOX, 0x1004), MLX4_PCI_DEV_IS_VF },
  2747. { PCI_VDEVICE(MELLANOX, 0x1005), 0 }, /* MT27510 Family */
  2748. { PCI_VDEVICE(MELLANOX, 0x1006), 0 }, /* MT27511 Family */
  2749. { PCI_VDEVICE(MELLANOX, 0x1007), 0 }, /* MT27520 Family */
  2750. { PCI_VDEVICE(MELLANOX, 0x1008), 0 }, /* MT27521 Family */
  2751. { PCI_VDEVICE(MELLANOX, 0x1009), 0 }, /* MT27530 Family */
  2752. { PCI_VDEVICE(MELLANOX, 0x100a), 0 }, /* MT27531 Family */
  2753. { PCI_VDEVICE(MELLANOX, 0x100b), 0 }, /* MT27540 Family */
  2754. { PCI_VDEVICE(MELLANOX, 0x100c), 0 }, /* MT27541 Family */
  2755. { PCI_VDEVICE(MELLANOX, 0x100d), 0 }, /* MT27550 Family */
  2756. { PCI_VDEVICE(MELLANOX, 0x100e), 0 }, /* MT27551 Family */
  2757. { PCI_VDEVICE(MELLANOX, 0x100f), 0 }, /* MT27560 Family */
  2758. { PCI_VDEVICE(MELLANOX, 0x1010), 0 }, /* MT27561 Family */
  2759. { 0, }
  2760. };
  2761. MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
  2762. static pci_ers_result_t mlx4_pci_err_detected(struct pci_dev *pdev,
  2763. pci_channel_state_t state)
  2764. {
  2765. mlx4_unload_one(pdev);
  2766. return state == pci_channel_io_perm_failure ?
  2767. PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
  2768. }
  2769. static pci_ers_result_t mlx4_pci_slot_reset(struct pci_dev *pdev)
  2770. {
  2771. struct mlx4_dev *dev = pci_get_drvdata(pdev);
  2772. struct mlx4_priv *priv = mlx4_priv(dev);
  2773. int ret;
  2774. ret = __mlx4_init_one(pdev, priv->pci_dev_data, priv);
  2775. return ret ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
  2776. }
  2777. static const struct pci_error_handlers mlx4_err_handler = {
  2778. .error_detected = mlx4_pci_err_detected,
  2779. .slot_reset = mlx4_pci_slot_reset,
  2780. };
  2781. static struct pci_driver mlx4_driver = {
  2782. .name = DRV_NAME,
  2783. .id_table = mlx4_pci_table,
  2784. .probe = mlx4_init_one,
  2785. .shutdown = mlx4_unload_one,
  2786. .remove = mlx4_remove_one,
  2787. .err_handler = &mlx4_err_handler,
  2788. };
  2789. static int __init mlx4_verify_params(void)
  2790. {
  2791. if ((log_num_mac < 0) || (log_num_mac > 7)) {
  2792. pr_warn("mlx4_core: bad num_mac: %d\n", log_num_mac);
  2793. return -1;
  2794. }
  2795. if (log_num_vlan != 0)
  2796. pr_warn("mlx4_core: log_num_vlan - obsolete module param, using %d\n",
  2797. MLX4_LOG_NUM_VLANS);
  2798. if (use_prio != 0)
  2799. pr_warn("mlx4_core: use_prio - obsolete module param, ignored\n");
  2800. if ((log_mtts_per_seg < 1) || (log_mtts_per_seg > 7)) {
  2801. pr_warn("mlx4_core: bad log_mtts_per_seg: %d\n",
  2802. log_mtts_per_seg);
  2803. return -1;
  2804. }
  2805. /* Check if module param for ports type has legal combination */
  2806. if (port_type_array[0] == false && port_type_array[1] == true) {
  2807. pr_warn("Module parameter configuration ETH/IB is not supported. Switching to default configuration IB/IB\n");
  2808. port_type_array[0] = true;
  2809. }
  2810. if (mlx4_log_num_mgm_entry_size < -7 ||
  2811. (mlx4_log_num_mgm_entry_size > 0 &&
  2812. (mlx4_log_num_mgm_entry_size < MLX4_MIN_MGM_LOG_ENTRY_SIZE ||
  2813. mlx4_log_num_mgm_entry_size > MLX4_MAX_MGM_LOG_ENTRY_SIZE))) {
  2814. pr_warn("mlx4_core: mlx4_log_num_mgm_entry_size (%d) not in legal range (-7..0 or %d..%d)\n",
  2815. mlx4_log_num_mgm_entry_size,
  2816. MLX4_MIN_MGM_LOG_ENTRY_SIZE,
  2817. MLX4_MAX_MGM_LOG_ENTRY_SIZE);
  2818. return -1;
  2819. }
  2820. return 0;
  2821. }
  2822. static int __init mlx4_init(void)
  2823. {
  2824. int ret;
  2825. if (mlx4_verify_params())
  2826. return -EINVAL;
  2827. mlx4_catas_init();
  2828. mlx4_wq = create_singlethread_workqueue("mlx4");
  2829. if (!mlx4_wq)
  2830. return -ENOMEM;
  2831. ret = pci_register_driver(&mlx4_driver);
  2832. if (ret < 0)
  2833. destroy_workqueue(mlx4_wq);
  2834. return ret < 0 ? ret : 0;
  2835. }
  2836. static void __exit mlx4_cleanup(void)
  2837. {
  2838. pci_unregister_driver(&mlx4_driver);
  2839. destroy_workqueue(mlx4_wq);
  2840. }
  2841. module_init(mlx4_init);
  2842. module_exit(mlx4_cleanup);