main.c 83 KB

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