main.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <asm-generic/kmap_types.h>
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/errno.h>
  36. #include <linux/pci.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/slab.h>
  39. #include <linux/io-mapping.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/mlx5/driver.h>
  42. #include <linux/mlx5/cq.h>
  43. #include <linux/mlx5/qp.h>
  44. #include <linux/mlx5/srq.h>
  45. #include <linux/debugfs.h>
  46. #include <linux/kmod.h>
  47. #include <linux/mlx5/mlx5_ifc.h>
  48. #include "mlx5_core.h"
  49. MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
  50. MODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver");
  51. MODULE_LICENSE("Dual BSD/GPL");
  52. MODULE_VERSION(DRIVER_VERSION);
  53. int mlx5_core_debug_mask;
  54. module_param_named(debug_mask, mlx5_core_debug_mask, int, 0644);
  55. MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
  56. #define MLX5_DEFAULT_PROF 2
  57. static int prof_sel = MLX5_DEFAULT_PROF;
  58. module_param_named(prof_sel, prof_sel, int, 0444);
  59. MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
  60. struct workqueue_struct *mlx5_core_wq;
  61. static LIST_HEAD(intf_list);
  62. static LIST_HEAD(dev_list);
  63. static DEFINE_MUTEX(intf_mutex);
  64. struct mlx5_device_context {
  65. struct list_head list;
  66. struct mlx5_interface *intf;
  67. void *context;
  68. };
  69. static struct mlx5_profile profile[] = {
  70. [0] = {
  71. .mask = 0,
  72. },
  73. [1] = {
  74. .mask = MLX5_PROF_MASK_QP_SIZE,
  75. .log_max_qp = 12,
  76. },
  77. [2] = {
  78. .mask = MLX5_PROF_MASK_QP_SIZE |
  79. MLX5_PROF_MASK_MR_CACHE,
  80. .log_max_qp = 17,
  81. .mr_cache[0] = {
  82. .size = 500,
  83. .limit = 250
  84. },
  85. .mr_cache[1] = {
  86. .size = 500,
  87. .limit = 250
  88. },
  89. .mr_cache[2] = {
  90. .size = 500,
  91. .limit = 250
  92. },
  93. .mr_cache[3] = {
  94. .size = 500,
  95. .limit = 250
  96. },
  97. .mr_cache[4] = {
  98. .size = 500,
  99. .limit = 250
  100. },
  101. .mr_cache[5] = {
  102. .size = 500,
  103. .limit = 250
  104. },
  105. .mr_cache[6] = {
  106. .size = 500,
  107. .limit = 250
  108. },
  109. .mr_cache[7] = {
  110. .size = 500,
  111. .limit = 250
  112. },
  113. .mr_cache[8] = {
  114. .size = 500,
  115. .limit = 250
  116. },
  117. .mr_cache[9] = {
  118. .size = 500,
  119. .limit = 250
  120. },
  121. .mr_cache[10] = {
  122. .size = 500,
  123. .limit = 250
  124. },
  125. .mr_cache[11] = {
  126. .size = 500,
  127. .limit = 250
  128. },
  129. .mr_cache[12] = {
  130. .size = 64,
  131. .limit = 32
  132. },
  133. .mr_cache[13] = {
  134. .size = 32,
  135. .limit = 16
  136. },
  137. .mr_cache[14] = {
  138. .size = 16,
  139. .limit = 8
  140. },
  141. .mr_cache[15] = {
  142. .size = 8,
  143. .limit = 4
  144. },
  145. },
  146. };
  147. static int set_dma_caps(struct pci_dev *pdev)
  148. {
  149. int err;
  150. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  151. if (err) {
  152. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
  153. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  154. if (err) {
  155. dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
  156. return err;
  157. }
  158. }
  159. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  160. if (err) {
  161. dev_warn(&pdev->dev,
  162. "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
  163. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  164. if (err) {
  165. dev_err(&pdev->dev,
  166. "Can't set consistent PCI DMA mask, aborting\n");
  167. return err;
  168. }
  169. }
  170. dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
  171. return err;
  172. }
  173. static int request_bar(struct pci_dev *pdev)
  174. {
  175. int err = 0;
  176. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  177. dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
  178. return -ENODEV;
  179. }
  180. err = pci_request_regions(pdev, DRIVER_NAME);
  181. if (err)
  182. dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
  183. return err;
  184. }
  185. static void release_bar(struct pci_dev *pdev)
  186. {
  187. pci_release_regions(pdev);
  188. }
  189. static int mlx5_enable_msix(struct mlx5_core_dev *dev)
  190. {
  191. struct mlx5_priv *priv = &dev->priv;
  192. struct mlx5_eq_table *table = &priv->eq_table;
  193. int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
  194. int nvec;
  195. int i;
  196. nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
  197. MLX5_EQ_VEC_COMP_BASE;
  198. nvec = min_t(int, nvec, num_eqs);
  199. if (nvec <= MLX5_EQ_VEC_COMP_BASE)
  200. return -ENOMEM;
  201. priv->msix_arr = kcalloc(nvec, sizeof(*priv->msix_arr), GFP_KERNEL);
  202. priv->irq_info = kcalloc(nvec, sizeof(*priv->irq_info), GFP_KERNEL);
  203. if (!priv->msix_arr || !priv->irq_info)
  204. goto err_free_msix;
  205. for (i = 0; i < nvec; i++)
  206. priv->msix_arr[i].entry = i;
  207. nvec = pci_enable_msix_range(dev->pdev, priv->msix_arr,
  208. MLX5_EQ_VEC_COMP_BASE + 1, nvec);
  209. if (nvec < 0)
  210. return nvec;
  211. table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
  212. return 0;
  213. err_free_msix:
  214. kfree(priv->irq_info);
  215. kfree(priv->msix_arr);
  216. return -ENOMEM;
  217. }
  218. static void mlx5_disable_msix(struct mlx5_core_dev *dev)
  219. {
  220. struct mlx5_priv *priv = &dev->priv;
  221. pci_disable_msix(dev->pdev);
  222. kfree(priv->irq_info);
  223. kfree(priv->msix_arr);
  224. }
  225. struct mlx5_reg_host_endianess {
  226. u8 he;
  227. u8 rsvd[15];
  228. };
  229. #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
  230. enum {
  231. MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
  232. MLX5_DEV_CAP_FLAG_DCT,
  233. };
  234. static u16 to_fw_pkey_sz(u32 size)
  235. {
  236. switch (size) {
  237. case 128:
  238. return 0;
  239. case 256:
  240. return 1;
  241. case 512:
  242. return 2;
  243. case 1024:
  244. return 3;
  245. case 2048:
  246. return 4;
  247. case 4096:
  248. return 5;
  249. default:
  250. pr_warn("invalid pkey table size %d\n", size);
  251. return 0;
  252. }
  253. }
  254. int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type,
  255. enum mlx5_cap_mode cap_mode)
  256. {
  257. u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
  258. int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
  259. void *out, *hca_caps;
  260. u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
  261. int err;
  262. memset(in, 0, sizeof(in));
  263. out = kzalloc(out_sz, GFP_KERNEL);
  264. if (!out)
  265. return -ENOMEM;
  266. MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
  267. MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
  268. err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
  269. if (err)
  270. goto query_ex;
  271. err = mlx5_cmd_status_to_err_v2(out);
  272. if (err) {
  273. mlx5_core_warn(dev,
  274. "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
  275. cap_type, cap_mode, err);
  276. goto query_ex;
  277. }
  278. hca_caps = MLX5_ADDR_OF(query_hca_cap_out, out, capability);
  279. switch (cap_mode) {
  280. case HCA_CAP_OPMOD_GET_MAX:
  281. memcpy(dev->hca_caps_max[cap_type], hca_caps,
  282. MLX5_UN_SZ_BYTES(hca_cap_union));
  283. break;
  284. case HCA_CAP_OPMOD_GET_CUR:
  285. memcpy(dev->hca_caps_cur[cap_type], hca_caps,
  286. MLX5_UN_SZ_BYTES(hca_cap_union));
  287. break;
  288. default:
  289. mlx5_core_warn(dev,
  290. "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
  291. cap_type, cap_mode);
  292. err = -EINVAL;
  293. break;
  294. }
  295. query_ex:
  296. kfree(out);
  297. return err;
  298. }
  299. static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz)
  300. {
  301. u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)];
  302. int err;
  303. memset(out, 0, sizeof(out));
  304. MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
  305. err = mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
  306. if (err)
  307. return err;
  308. err = mlx5_cmd_status_to_err_v2(out);
  309. return err;
  310. }
  311. static int handle_hca_cap(struct mlx5_core_dev *dev)
  312. {
  313. void *set_ctx = NULL;
  314. struct mlx5_profile *prof = dev->profile;
  315. int err = -ENOMEM;
  316. int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
  317. void *set_hca_cap;
  318. set_ctx = kzalloc(set_sz, GFP_KERNEL);
  319. if (!set_ctx)
  320. goto query_ex;
  321. err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL, HCA_CAP_OPMOD_GET_MAX);
  322. if (err)
  323. goto query_ex;
  324. err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL, HCA_CAP_OPMOD_GET_CUR);
  325. if (err)
  326. goto query_ex;
  327. set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
  328. capability);
  329. memcpy(set_hca_cap, dev->hca_caps_cur[MLX5_CAP_GENERAL],
  330. MLX5_ST_SZ_BYTES(cmd_hca_cap));
  331. mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
  332. mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
  333. 128);
  334. /* we limit the size of the pkey table to 128 entries for now */
  335. MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
  336. to_fw_pkey_sz(128));
  337. if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
  338. MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
  339. prof->log_max_qp);
  340. /* disable cmdif checksum */
  341. MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
  342. err = set_caps(dev, set_ctx, set_sz);
  343. query_ex:
  344. kfree(set_ctx);
  345. return err;
  346. }
  347. static int set_hca_ctrl(struct mlx5_core_dev *dev)
  348. {
  349. struct mlx5_reg_host_endianess he_in;
  350. struct mlx5_reg_host_endianess he_out;
  351. int err;
  352. memset(&he_in, 0, sizeof(he_in));
  353. he_in.he = MLX5_SET_HOST_ENDIANNESS;
  354. err = mlx5_core_access_reg(dev, &he_in, sizeof(he_in),
  355. &he_out, sizeof(he_out),
  356. MLX5_REG_HOST_ENDIANNESS, 0, 1);
  357. return err;
  358. }
  359. static int mlx5_core_enable_hca(struct mlx5_core_dev *dev)
  360. {
  361. int err;
  362. struct mlx5_enable_hca_mbox_in in;
  363. struct mlx5_enable_hca_mbox_out out;
  364. memset(&in, 0, sizeof(in));
  365. memset(&out, 0, sizeof(out));
  366. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_ENABLE_HCA);
  367. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  368. if (err)
  369. return err;
  370. if (out.hdr.status)
  371. return mlx5_cmd_status_to_err(&out.hdr);
  372. return 0;
  373. }
  374. static int mlx5_core_disable_hca(struct mlx5_core_dev *dev)
  375. {
  376. int err;
  377. struct mlx5_disable_hca_mbox_in in;
  378. struct mlx5_disable_hca_mbox_out out;
  379. memset(&in, 0, sizeof(in));
  380. memset(&out, 0, sizeof(out));
  381. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DISABLE_HCA);
  382. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  383. if (err)
  384. return err;
  385. if (out.hdr.status)
  386. return mlx5_cmd_status_to_err(&out.hdr);
  387. return 0;
  388. }
  389. static int mlx5_irq_set_affinity_hint(struct mlx5_core_dev *mdev, int i)
  390. {
  391. struct mlx5_priv *priv = &mdev->priv;
  392. struct msix_entry *msix = priv->msix_arr;
  393. int irq = msix[i + MLX5_EQ_VEC_COMP_BASE].vector;
  394. int numa_node = dev_to_node(&mdev->pdev->dev);
  395. int err;
  396. if (!zalloc_cpumask_var(&priv->irq_info[i].mask, GFP_KERNEL)) {
  397. mlx5_core_warn(mdev, "zalloc_cpumask_var failed");
  398. return -ENOMEM;
  399. }
  400. cpumask_set_cpu(cpumask_local_spread(i, numa_node),
  401. priv->irq_info[i].mask);
  402. err = irq_set_affinity_hint(irq, priv->irq_info[i].mask);
  403. if (err) {
  404. mlx5_core_warn(mdev, "irq_set_affinity_hint failed,irq 0x%.4x",
  405. irq);
  406. goto err_clear_mask;
  407. }
  408. return 0;
  409. err_clear_mask:
  410. free_cpumask_var(priv->irq_info[i].mask);
  411. return err;
  412. }
  413. static void mlx5_irq_clear_affinity_hint(struct mlx5_core_dev *mdev, int i)
  414. {
  415. struct mlx5_priv *priv = &mdev->priv;
  416. struct msix_entry *msix = priv->msix_arr;
  417. int irq = msix[i + MLX5_EQ_VEC_COMP_BASE].vector;
  418. irq_set_affinity_hint(irq, NULL);
  419. free_cpumask_var(priv->irq_info[i].mask);
  420. }
  421. static int mlx5_irq_set_affinity_hints(struct mlx5_core_dev *mdev)
  422. {
  423. int err;
  424. int i;
  425. for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++) {
  426. err = mlx5_irq_set_affinity_hint(mdev, i);
  427. if (err)
  428. goto err_out;
  429. }
  430. return 0;
  431. err_out:
  432. for (i--; i >= 0; i--)
  433. mlx5_irq_clear_affinity_hint(mdev, i);
  434. return err;
  435. }
  436. static void mlx5_irq_clear_affinity_hints(struct mlx5_core_dev *mdev)
  437. {
  438. int i;
  439. for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++)
  440. mlx5_irq_clear_affinity_hint(mdev, i);
  441. }
  442. int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn, int *irqn)
  443. {
  444. struct mlx5_eq_table *table = &dev->priv.eq_table;
  445. struct mlx5_eq *eq, *n;
  446. int err = -ENOENT;
  447. spin_lock(&table->lock);
  448. list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
  449. if (eq->index == vector) {
  450. *eqn = eq->eqn;
  451. *irqn = eq->irqn;
  452. err = 0;
  453. break;
  454. }
  455. }
  456. spin_unlock(&table->lock);
  457. return err;
  458. }
  459. EXPORT_SYMBOL(mlx5_vector2eqn);
  460. static void free_comp_eqs(struct mlx5_core_dev *dev)
  461. {
  462. struct mlx5_eq_table *table = &dev->priv.eq_table;
  463. struct mlx5_eq *eq, *n;
  464. spin_lock(&table->lock);
  465. list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
  466. list_del(&eq->list);
  467. spin_unlock(&table->lock);
  468. if (mlx5_destroy_unmap_eq(dev, eq))
  469. mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
  470. eq->eqn);
  471. kfree(eq);
  472. spin_lock(&table->lock);
  473. }
  474. spin_unlock(&table->lock);
  475. }
  476. static int alloc_comp_eqs(struct mlx5_core_dev *dev)
  477. {
  478. struct mlx5_eq_table *table = &dev->priv.eq_table;
  479. char name[MLX5_MAX_IRQ_NAME];
  480. struct mlx5_eq *eq;
  481. int ncomp_vec;
  482. int nent;
  483. int err;
  484. int i;
  485. INIT_LIST_HEAD(&table->comp_eqs_list);
  486. ncomp_vec = table->num_comp_vectors;
  487. nent = MLX5_COMP_EQ_SIZE;
  488. for (i = 0; i < ncomp_vec; i++) {
  489. eq = kzalloc(sizeof(*eq), GFP_KERNEL);
  490. if (!eq) {
  491. err = -ENOMEM;
  492. goto clean;
  493. }
  494. snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
  495. err = mlx5_create_map_eq(dev, eq,
  496. i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
  497. name, &dev->priv.uuari.uars[0]);
  498. if (err) {
  499. kfree(eq);
  500. goto clean;
  501. }
  502. mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
  503. eq->index = i;
  504. spin_lock(&table->lock);
  505. list_add_tail(&eq->list, &table->comp_eqs_list);
  506. spin_unlock(&table->lock);
  507. }
  508. return 0;
  509. clean:
  510. free_comp_eqs(dev);
  511. return err;
  512. }
  513. #ifdef CONFIG_MLX5_CORE_EN
  514. static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
  515. {
  516. u32 query_in[MLX5_ST_SZ_DW(query_issi_in)];
  517. u32 query_out[MLX5_ST_SZ_DW(query_issi_out)];
  518. u32 set_in[MLX5_ST_SZ_DW(set_issi_in)];
  519. u32 set_out[MLX5_ST_SZ_DW(set_issi_out)];
  520. int err;
  521. u32 sup_issi;
  522. memset(query_in, 0, sizeof(query_in));
  523. memset(query_out, 0, sizeof(query_out));
  524. MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
  525. err = mlx5_cmd_exec_check_status(dev, query_in, sizeof(query_in),
  526. query_out, sizeof(query_out));
  527. if (err) {
  528. if (((struct mlx5_outbox_hdr *)query_out)->status ==
  529. MLX5_CMD_STAT_BAD_OP_ERR) {
  530. pr_debug("Only ISSI 0 is supported\n");
  531. return 0;
  532. }
  533. pr_err("failed to query ISSI\n");
  534. return err;
  535. }
  536. sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
  537. if (sup_issi & (1 << 1)) {
  538. memset(set_in, 0, sizeof(set_in));
  539. memset(set_out, 0, sizeof(set_out));
  540. MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
  541. MLX5_SET(set_issi_in, set_in, current_issi, 1);
  542. err = mlx5_cmd_exec_check_status(dev, set_in, sizeof(set_in),
  543. set_out, sizeof(set_out));
  544. if (err) {
  545. pr_err("failed to set ISSI=1\n");
  546. return err;
  547. }
  548. dev->issi = 1;
  549. return 0;
  550. } else if (sup_issi & (1 << 0) || !sup_issi) {
  551. return 0;
  552. }
  553. return -ENOTSUPP;
  554. }
  555. #endif
  556. static int mlx5_dev_init(struct mlx5_core_dev *dev, struct pci_dev *pdev)
  557. {
  558. struct mlx5_priv *priv = &dev->priv;
  559. int err;
  560. dev->pdev = pdev;
  561. pci_set_drvdata(dev->pdev, dev);
  562. strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
  563. priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
  564. mutex_init(&priv->pgdir_mutex);
  565. INIT_LIST_HEAD(&priv->pgdir_list);
  566. spin_lock_init(&priv->mkey_lock);
  567. priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
  568. if (!priv->dbg_root)
  569. return -ENOMEM;
  570. err = pci_enable_device(pdev);
  571. if (err) {
  572. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  573. goto err_dbg;
  574. }
  575. err = request_bar(pdev);
  576. if (err) {
  577. dev_err(&pdev->dev, "error requesting BARs, aborting\n");
  578. goto err_disable;
  579. }
  580. pci_set_master(pdev);
  581. err = set_dma_caps(pdev);
  582. if (err) {
  583. dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
  584. goto err_clr_master;
  585. }
  586. dev->iseg_base = pci_resource_start(dev->pdev, 0);
  587. dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
  588. if (!dev->iseg) {
  589. err = -ENOMEM;
  590. dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
  591. goto err_clr_master;
  592. }
  593. dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
  594. fw_rev_min(dev), fw_rev_sub(dev));
  595. err = mlx5_cmd_init(dev);
  596. if (err) {
  597. dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
  598. goto err_unmap;
  599. }
  600. mlx5_pagealloc_init(dev);
  601. err = mlx5_core_enable_hca(dev);
  602. if (err) {
  603. dev_err(&pdev->dev, "enable hca failed\n");
  604. goto err_pagealloc_cleanup;
  605. }
  606. #ifdef CONFIG_MLX5_CORE_EN
  607. err = mlx5_core_set_issi(dev);
  608. if (err) {
  609. dev_err(&pdev->dev, "failed to set issi\n");
  610. goto err_disable_hca;
  611. }
  612. #endif
  613. err = mlx5_satisfy_startup_pages(dev, 1);
  614. if (err) {
  615. dev_err(&pdev->dev, "failed to allocate boot pages\n");
  616. goto err_disable_hca;
  617. }
  618. err = set_hca_ctrl(dev);
  619. if (err) {
  620. dev_err(&pdev->dev, "set_hca_ctrl failed\n");
  621. goto reclaim_boot_pages;
  622. }
  623. err = handle_hca_cap(dev);
  624. if (err) {
  625. dev_err(&pdev->dev, "handle_hca_cap failed\n");
  626. goto reclaim_boot_pages;
  627. }
  628. err = mlx5_satisfy_startup_pages(dev, 0);
  629. if (err) {
  630. dev_err(&pdev->dev, "failed to allocate init pages\n");
  631. goto reclaim_boot_pages;
  632. }
  633. err = mlx5_pagealloc_start(dev);
  634. if (err) {
  635. dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
  636. goto reclaim_boot_pages;
  637. }
  638. err = mlx5_cmd_init_hca(dev);
  639. if (err) {
  640. dev_err(&pdev->dev, "init hca failed\n");
  641. goto err_pagealloc_stop;
  642. }
  643. mlx5_start_health_poll(dev);
  644. err = mlx5_query_hca_caps(dev);
  645. if (err) {
  646. dev_err(&pdev->dev, "query hca failed\n");
  647. goto err_stop_poll;
  648. }
  649. err = mlx5_query_board_id(dev);
  650. if (err) {
  651. dev_err(&pdev->dev, "query board id failed\n");
  652. goto err_stop_poll;
  653. }
  654. err = mlx5_enable_msix(dev);
  655. if (err) {
  656. dev_err(&pdev->dev, "enable msix failed\n");
  657. goto err_stop_poll;
  658. }
  659. err = mlx5_eq_init(dev);
  660. if (err) {
  661. dev_err(&pdev->dev, "failed to initialize eq\n");
  662. goto disable_msix;
  663. }
  664. err = mlx5_alloc_uuars(dev, &priv->uuari);
  665. if (err) {
  666. dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
  667. goto err_eq_cleanup;
  668. }
  669. err = mlx5_start_eqs(dev);
  670. if (err) {
  671. dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
  672. goto err_free_uar;
  673. }
  674. err = alloc_comp_eqs(dev);
  675. if (err) {
  676. dev_err(&pdev->dev, "Failed to alloc completion EQs\n");
  677. goto err_stop_eqs;
  678. }
  679. err = mlx5_irq_set_affinity_hints(dev);
  680. if (err) {
  681. dev_err(&pdev->dev, "Failed to alloc affinity hint cpumask\n");
  682. goto err_free_comp_eqs;
  683. }
  684. MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock);
  685. mlx5_init_cq_table(dev);
  686. mlx5_init_qp_table(dev);
  687. mlx5_init_srq_table(dev);
  688. mlx5_init_mr_table(dev);
  689. return 0;
  690. err_free_comp_eqs:
  691. free_comp_eqs(dev);
  692. err_stop_eqs:
  693. mlx5_stop_eqs(dev);
  694. err_free_uar:
  695. mlx5_free_uuars(dev, &priv->uuari);
  696. err_eq_cleanup:
  697. mlx5_eq_cleanup(dev);
  698. disable_msix:
  699. mlx5_disable_msix(dev);
  700. err_stop_poll:
  701. mlx5_stop_health_poll(dev);
  702. if (mlx5_cmd_teardown_hca(dev)) {
  703. dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
  704. return err;
  705. }
  706. err_pagealloc_stop:
  707. mlx5_pagealloc_stop(dev);
  708. reclaim_boot_pages:
  709. mlx5_reclaim_startup_pages(dev);
  710. err_disable_hca:
  711. mlx5_core_disable_hca(dev);
  712. err_pagealloc_cleanup:
  713. mlx5_pagealloc_cleanup(dev);
  714. mlx5_cmd_cleanup(dev);
  715. err_unmap:
  716. iounmap(dev->iseg);
  717. err_clr_master:
  718. pci_clear_master(dev->pdev);
  719. release_bar(dev->pdev);
  720. err_disable:
  721. pci_disable_device(dev->pdev);
  722. err_dbg:
  723. debugfs_remove(priv->dbg_root);
  724. return err;
  725. }
  726. static void mlx5_dev_cleanup(struct mlx5_core_dev *dev)
  727. {
  728. struct mlx5_priv *priv = &dev->priv;
  729. mlx5_cleanup_srq_table(dev);
  730. mlx5_cleanup_qp_table(dev);
  731. mlx5_cleanup_cq_table(dev);
  732. mlx5_irq_clear_affinity_hints(dev);
  733. free_comp_eqs(dev);
  734. mlx5_stop_eqs(dev);
  735. mlx5_free_uuars(dev, &priv->uuari);
  736. mlx5_eq_cleanup(dev);
  737. mlx5_disable_msix(dev);
  738. mlx5_stop_health_poll(dev);
  739. if (mlx5_cmd_teardown_hca(dev)) {
  740. dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
  741. return;
  742. }
  743. mlx5_pagealloc_stop(dev);
  744. mlx5_reclaim_startup_pages(dev);
  745. mlx5_core_disable_hca(dev);
  746. mlx5_pagealloc_cleanup(dev);
  747. mlx5_cmd_cleanup(dev);
  748. iounmap(dev->iseg);
  749. pci_clear_master(dev->pdev);
  750. release_bar(dev->pdev);
  751. pci_disable_device(dev->pdev);
  752. debugfs_remove(priv->dbg_root);
  753. }
  754. static void mlx5_add_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
  755. {
  756. struct mlx5_device_context *dev_ctx;
  757. struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
  758. dev_ctx = kmalloc(sizeof(*dev_ctx), GFP_KERNEL);
  759. if (!dev_ctx) {
  760. pr_warn("mlx5_add_device: alloc context failed\n");
  761. return;
  762. }
  763. dev_ctx->intf = intf;
  764. dev_ctx->context = intf->add(dev);
  765. if (dev_ctx->context) {
  766. spin_lock_irq(&priv->ctx_lock);
  767. list_add_tail(&dev_ctx->list, &priv->ctx_list);
  768. spin_unlock_irq(&priv->ctx_lock);
  769. } else {
  770. kfree(dev_ctx);
  771. }
  772. }
  773. static void mlx5_remove_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
  774. {
  775. struct mlx5_device_context *dev_ctx;
  776. struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
  777. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  778. if (dev_ctx->intf == intf) {
  779. spin_lock_irq(&priv->ctx_lock);
  780. list_del(&dev_ctx->list);
  781. spin_unlock_irq(&priv->ctx_lock);
  782. intf->remove(dev, dev_ctx->context);
  783. kfree(dev_ctx);
  784. return;
  785. }
  786. }
  787. static int mlx5_register_device(struct mlx5_core_dev *dev)
  788. {
  789. struct mlx5_priv *priv = &dev->priv;
  790. struct mlx5_interface *intf;
  791. mutex_lock(&intf_mutex);
  792. list_add_tail(&priv->dev_list, &dev_list);
  793. list_for_each_entry(intf, &intf_list, list)
  794. mlx5_add_device(intf, priv);
  795. mutex_unlock(&intf_mutex);
  796. return 0;
  797. }
  798. static void mlx5_unregister_device(struct mlx5_core_dev *dev)
  799. {
  800. struct mlx5_priv *priv = &dev->priv;
  801. struct mlx5_interface *intf;
  802. mutex_lock(&intf_mutex);
  803. list_for_each_entry(intf, &intf_list, list)
  804. mlx5_remove_device(intf, priv);
  805. list_del(&priv->dev_list);
  806. mutex_unlock(&intf_mutex);
  807. }
  808. int mlx5_register_interface(struct mlx5_interface *intf)
  809. {
  810. struct mlx5_priv *priv;
  811. if (!intf->add || !intf->remove)
  812. return -EINVAL;
  813. mutex_lock(&intf_mutex);
  814. list_add_tail(&intf->list, &intf_list);
  815. list_for_each_entry(priv, &dev_list, dev_list)
  816. mlx5_add_device(intf, priv);
  817. mutex_unlock(&intf_mutex);
  818. return 0;
  819. }
  820. EXPORT_SYMBOL(mlx5_register_interface);
  821. void mlx5_unregister_interface(struct mlx5_interface *intf)
  822. {
  823. struct mlx5_priv *priv;
  824. mutex_lock(&intf_mutex);
  825. list_for_each_entry(priv, &dev_list, dev_list)
  826. mlx5_remove_device(intf, priv);
  827. list_del(&intf->list);
  828. mutex_unlock(&intf_mutex);
  829. }
  830. EXPORT_SYMBOL(mlx5_unregister_interface);
  831. void *mlx5_get_protocol_dev(struct mlx5_core_dev *mdev, int protocol)
  832. {
  833. struct mlx5_priv *priv = &mdev->priv;
  834. struct mlx5_device_context *dev_ctx;
  835. unsigned long flags;
  836. void *result = NULL;
  837. spin_lock_irqsave(&priv->ctx_lock, flags);
  838. list_for_each_entry(dev_ctx, &mdev->priv.ctx_list, list)
  839. if ((dev_ctx->intf->protocol == protocol) &&
  840. dev_ctx->intf->get_dev) {
  841. result = dev_ctx->intf->get_dev(dev_ctx->context);
  842. break;
  843. }
  844. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  845. return result;
  846. }
  847. EXPORT_SYMBOL(mlx5_get_protocol_dev);
  848. static void mlx5_core_event(struct mlx5_core_dev *dev, enum mlx5_dev_event event,
  849. unsigned long param)
  850. {
  851. struct mlx5_priv *priv = &dev->priv;
  852. struct mlx5_device_context *dev_ctx;
  853. unsigned long flags;
  854. spin_lock_irqsave(&priv->ctx_lock, flags);
  855. list_for_each_entry(dev_ctx, &priv->ctx_list, list)
  856. if (dev_ctx->intf->event)
  857. dev_ctx->intf->event(dev, dev_ctx->context, event, param);
  858. spin_unlock_irqrestore(&priv->ctx_lock, flags);
  859. }
  860. struct mlx5_core_event_handler {
  861. void (*event)(struct mlx5_core_dev *dev,
  862. enum mlx5_dev_event event,
  863. void *data);
  864. };
  865. #define MLX5_IB_MOD "mlx5_ib"
  866. static int init_one(struct pci_dev *pdev,
  867. const struct pci_device_id *id)
  868. {
  869. struct mlx5_core_dev *dev;
  870. struct mlx5_priv *priv;
  871. int err;
  872. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  873. if (!dev) {
  874. dev_err(&pdev->dev, "kzalloc failed\n");
  875. return -ENOMEM;
  876. }
  877. priv = &dev->priv;
  878. pci_set_drvdata(pdev, dev);
  879. if (prof_sel < 0 || prof_sel >= ARRAY_SIZE(profile)) {
  880. pr_warn("selected profile out of range, selecting default (%d)\n",
  881. MLX5_DEFAULT_PROF);
  882. prof_sel = MLX5_DEFAULT_PROF;
  883. }
  884. dev->profile = &profile[prof_sel];
  885. dev->event = mlx5_core_event;
  886. INIT_LIST_HEAD(&priv->ctx_list);
  887. spin_lock_init(&priv->ctx_lock);
  888. err = mlx5_dev_init(dev, pdev);
  889. if (err) {
  890. dev_err(&pdev->dev, "mlx5_dev_init failed %d\n", err);
  891. goto out;
  892. }
  893. err = mlx5_register_device(dev);
  894. if (err) {
  895. dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
  896. goto out_init;
  897. }
  898. err = request_module_nowait(MLX5_IB_MOD);
  899. if (err)
  900. pr_info("failed request module on %s\n", MLX5_IB_MOD);
  901. return 0;
  902. out_init:
  903. mlx5_dev_cleanup(dev);
  904. out:
  905. kfree(dev);
  906. return err;
  907. }
  908. static void remove_one(struct pci_dev *pdev)
  909. {
  910. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  911. mlx5_unregister_device(dev);
  912. mlx5_dev_cleanup(dev);
  913. kfree(dev);
  914. }
  915. static const struct pci_device_id mlx5_core_pci_table[] = {
  916. { PCI_VDEVICE(MELLANOX, 0x1011) }, /* Connect-IB */
  917. { PCI_VDEVICE(MELLANOX, 0x1012) }, /* Connect-IB VF */
  918. { PCI_VDEVICE(MELLANOX, 0x1013) }, /* ConnectX-4 */
  919. { PCI_VDEVICE(MELLANOX, 0x1014) }, /* ConnectX-4 VF */
  920. { PCI_VDEVICE(MELLANOX, 0x1015) }, /* ConnectX-4LX */
  921. { PCI_VDEVICE(MELLANOX, 0x1016) }, /* ConnectX-4LX VF */
  922. { 0, }
  923. };
  924. MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
  925. static struct pci_driver mlx5_core_driver = {
  926. .name = DRIVER_NAME,
  927. .id_table = mlx5_core_pci_table,
  928. .probe = init_one,
  929. .remove = remove_one
  930. };
  931. static int __init init(void)
  932. {
  933. int err;
  934. mlx5_register_debugfs();
  935. mlx5_core_wq = create_singlethread_workqueue("mlx5_core_wq");
  936. if (!mlx5_core_wq) {
  937. err = -ENOMEM;
  938. goto err_debug;
  939. }
  940. mlx5_health_init();
  941. err = pci_register_driver(&mlx5_core_driver);
  942. if (err)
  943. goto err_health;
  944. #ifdef CONFIG_MLX5_CORE_EN
  945. mlx5e_init();
  946. #endif
  947. return 0;
  948. err_health:
  949. mlx5_health_cleanup();
  950. destroy_workqueue(mlx5_core_wq);
  951. err_debug:
  952. mlx5_unregister_debugfs();
  953. return err;
  954. }
  955. static void __exit cleanup(void)
  956. {
  957. #ifdef CONFIG_MLX5_CORE_EN
  958. mlx5e_cleanup();
  959. #endif
  960. pci_unregister_driver(&mlx5_core_driver);
  961. mlx5_health_cleanup();
  962. destroy_workqueue(mlx5_core_wq);
  963. mlx5_unregister_debugfs();
  964. }
  965. module_init(init);
  966. module_exit(cleanup);