main.c 57 KB

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