main.c 68 KB

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