main.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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 <linux/highmem.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/delay.h>
  42. #include <linux/mlx5/driver.h>
  43. #include <linux/mlx5/cq.h>
  44. #include <linux/mlx5/qp.h>
  45. #include <linux/mlx5/srq.h>
  46. #include <linux/debugfs.h>
  47. #include <linux/kmod.h>
  48. #include <linux/mlx5/mlx5_ifc.h>
  49. #ifdef CONFIG_RFS_ACCEL
  50. #include <linux/cpu_rmap.h>
  51. #endif
  52. #include <net/devlink.h>
  53. #include "mlx5_core.h"
  54. #include "fs_core.h"
  55. #ifdef CONFIG_MLX5_CORE_EN
  56. #include "eswitch.h"
  57. #endif
  58. MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
  59. MODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver");
  60. MODULE_LICENSE("Dual BSD/GPL");
  61. MODULE_VERSION(DRIVER_VERSION);
  62. unsigned int mlx5_core_debug_mask;
  63. module_param_named(debug_mask, mlx5_core_debug_mask, uint, 0644);
  64. MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
  65. #define MLX5_DEFAULT_PROF 2
  66. static unsigned int prof_sel = MLX5_DEFAULT_PROF;
  67. module_param_named(prof_sel, prof_sel, uint, 0444);
  68. MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
  69. enum {
  70. MLX5_ATOMIC_REQ_MODE_BE = 0x0,
  71. MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
  72. };
  73. static struct mlx5_profile profile[] = {
  74. [0] = {
  75. .mask = 0,
  76. },
  77. [1] = {
  78. .mask = MLX5_PROF_MASK_QP_SIZE,
  79. .log_max_qp = 12,
  80. },
  81. [2] = {
  82. .mask = MLX5_PROF_MASK_QP_SIZE |
  83. MLX5_PROF_MASK_MR_CACHE,
  84. .log_max_qp = 18,
  85. .mr_cache[0] = {
  86. .size = 500,
  87. .limit = 250
  88. },
  89. .mr_cache[1] = {
  90. .size = 500,
  91. .limit = 250
  92. },
  93. .mr_cache[2] = {
  94. .size = 500,
  95. .limit = 250
  96. },
  97. .mr_cache[3] = {
  98. .size = 500,
  99. .limit = 250
  100. },
  101. .mr_cache[4] = {
  102. .size = 500,
  103. .limit = 250
  104. },
  105. .mr_cache[5] = {
  106. .size = 500,
  107. .limit = 250
  108. },
  109. .mr_cache[6] = {
  110. .size = 500,
  111. .limit = 250
  112. },
  113. .mr_cache[7] = {
  114. .size = 500,
  115. .limit = 250
  116. },
  117. .mr_cache[8] = {
  118. .size = 500,
  119. .limit = 250
  120. },
  121. .mr_cache[9] = {
  122. .size = 500,
  123. .limit = 250
  124. },
  125. .mr_cache[10] = {
  126. .size = 500,
  127. .limit = 250
  128. },
  129. .mr_cache[11] = {
  130. .size = 500,
  131. .limit = 250
  132. },
  133. .mr_cache[12] = {
  134. .size = 64,
  135. .limit = 32
  136. },
  137. .mr_cache[13] = {
  138. .size = 32,
  139. .limit = 16
  140. },
  141. .mr_cache[14] = {
  142. .size = 16,
  143. .limit = 8
  144. },
  145. .mr_cache[15] = {
  146. .size = 8,
  147. .limit = 4
  148. },
  149. },
  150. };
  151. #define FW_INIT_TIMEOUT_MILI 2000
  152. #define FW_INIT_WAIT_MS 2
  153. #define FW_PRE_INIT_TIMEOUT_MILI 10000
  154. static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili)
  155. {
  156. unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
  157. int err = 0;
  158. while (fw_initializing(dev)) {
  159. if (time_after(jiffies, end)) {
  160. err = -EBUSY;
  161. break;
  162. }
  163. msleep(FW_INIT_WAIT_MS);
  164. }
  165. return err;
  166. }
  167. static int set_dma_caps(struct pci_dev *pdev)
  168. {
  169. int err;
  170. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  171. if (err) {
  172. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
  173. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  174. if (err) {
  175. dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
  176. return err;
  177. }
  178. }
  179. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  180. if (err) {
  181. dev_warn(&pdev->dev,
  182. "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
  183. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  184. if (err) {
  185. dev_err(&pdev->dev,
  186. "Can't set consistent PCI DMA mask, aborting\n");
  187. return err;
  188. }
  189. }
  190. dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
  191. return err;
  192. }
  193. static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
  194. {
  195. struct pci_dev *pdev = dev->pdev;
  196. int err = 0;
  197. mutex_lock(&dev->pci_status_mutex);
  198. if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
  199. err = pci_enable_device(pdev);
  200. if (!err)
  201. dev->pci_status = MLX5_PCI_STATUS_ENABLED;
  202. }
  203. mutex_unlock(&dev->pci_status_mutex);
  204. return err;
  205. }
  206. static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
  207. {
  208. struct pci_dev *pdev = dev->pdev;
  209. mutex_lock(&dev->pci_status_mutex);
  210. if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
  211. pci_disable_device(pdev);
  212. dev->pci_status = MLX5_PCI_STATUS_DISABLED;
  213. }
  214. mutex_unlock(&dev->pci_status_mutex);
  215. }
  216. static int request_bar(struct pci_dev *pdev)
  217. {
  218. int err = 0;
  219. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  220. dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
  221. return -ENODEV;
  222. }
  223. err = pci_request_regions(pdev, DRIVER_NAME);
  224. if (err)
  225. dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
  226. return err;
  227. }
  228. static void release_bar(struct pci_dev *pdev)
  229. {
  230. pci_release_regions(pdev);
  231. }
  232. static int mlx5_enable_msix(struct mlx5_core_dev *dev)
  233. {
  234. struct mlx5_priv *priv = &dev->priv;
  235. struct mlx5_eq_table *table = &priv->eq_table;
  236. int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
  237. int nvec;
  238. int i;
  239. nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
  240. MLX5_EQ_VEC_COMP_BASE;
  241. nvec = min_t(int, nvec, num_eqs);
  242. if (nvec <= MLX5_EQ_VEC_COMP_BASE)
  243. return -ENOMEM;
  244. priv->msix_arr = kcalloc(nvec, sizeof(*priv->msix_arr), GFP_KERNEL);
  245. priv->irq_info = kcalloc(nvec, sizeof(*priv->irq_info), GFP_KERNEL);
  246. if (!priv->msix_arr || !priv->irq_info)
  247. goto err_free_msix;
  248. for (i = 0; i < nvec; i++)
  249. priv->msix_arr[i].entry = i;
  250. nvec = pci_enable_msix_range(dev->pdev, priv->msix_arr,
  251. MLX5_EQ_VEC_COMP_BASE + 1, nvec);
  252. if (nvec < 0)
  253. return nvec;
  254. table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
  255. return 0;
  256. err_free_msix:
  257. kfree(priv->irq_info);
  258. kfree(priv->msix_arr);
  259. return -ENOMEM;
  260. }
  261. static void mlx5_disable_msix(struct mlx5_core_dev *dev)
  262. {
  263. struct mlx5_priv *priv = &dev->priv;
  264. pci_disable_msix(dev->pdev);
  265. kfree(priv->irq_info);
  266. kfree(priv->msix_arr);
  267. }
  268. struct mlx5_reg_host_endianess {
  269. u8 he;
  270. u8 rsvd[15];
  271. };
  272. #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
  273. enum {
  274. MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
  275. MLX5_DEV_CAP_FLAG_DCT,
  276. };
  277. static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
  278. {
  279. switch (size) {
  280. case 128:
  281. return 0;
  282. case 256:
  283. return 1;
  284. case 512:
  285. return 2;
  286. case 1024:
  287. return 3;
  288. case 2048:
  289. return 4;
  290. case 4096:
  291. return 5;
  292. default:
  293. mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
  294. return 0;
  295. }
  296. }
  297. static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
  298. enum mlx5_cap_type cap_type,
  299. enum mlx5_cap_mode cap_mode)
  300. {
  301. u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
  302. int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
  303. void *out, *hca_caps;
  304. u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
  305. int err;
  306. memset(in, 0, sizeof(in));
  307. out = kzalloc(out_sz, GFP_KERNEL);
  308. if (!out)
  309. return -ENOMEM;
  310. MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
  311. MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
  312. err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
  313. if (err) {
  314. mlx5_core_warn(dev,
  315. "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
  316. cap_type, cap_mode, err);
  317. goto query_ex;
  318. }
  319. hca_caps = MLX5_ADDR_OF(query_hca_cap_out, out, capability);
  320. switch (cap_mode) {
  321. case HCA_CAP_OPMOD_GET_MAX:
  322. memcpy(dev->hca_caps_max[cap_type], hca_caps,
  323. MLX5_UN_SZ_BYTES(hca_cap_union));
  324. break;
  325. case HCA_CAP_OPMOD_GET_CUR:
  326. memcpy(dev->hca_caps_cur[cap_type], hca_caps,
  327. MLX5_UN_SZ_BYTES(hca_cap_union));
  328. break;
  329. default:
  330. mlx5_core_warn(dev,
  331. "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
  332. cap_type, cap_mode);
  333. err = -EINVAL;
  334. break;
  335. }
  336. query_ex:
  337. kfree(out);
  338. return err;
  339. }
  340. int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
  341. {
  342. int ret;
  343. ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
  344. if (ret)
  345. return ret;
  346. return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
  347. }
  348. static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz, int opmod)
  349. {
  350. u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)] = {0};
  351. MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
  352. MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
  353. return mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
  354. }
  355. static int handle_hca_cap_atomic(struct mlx5_core_dev *dev)
  356. {
  357. void *set_ctx;
  358. void *set_hca_cap;
  359. int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
  360. int req_endianness;
  361. int err;
  362. if (MLX5_CAP_GEN(dev, atomic)) {
  363. err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
  364. if (err)
  365. return err;
  366. } else {
  367. return 0;
  368. }
  369. req_endianness =
  370. MLX5_CAP_ATOMIC(dev,
  371. supported_atomic_req_8B_endianess_mode_1);
  372. if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
  373. return 0;
  374. set_ctx = kzalloc(set_sz, GFP_KERNEL);
  375. if (!set_ctx)
  376. return -ENOMEM;
  377. set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
  378. /* Set requestor to host endianness */
  379. MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianess_mode,
  380. MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
  381. err = set_caps(dev, set_ctx, set_sz, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);
  382. kfree(set_ctx);
  383. return err;
  384. }
  385. static int handle_hca_cap(struct mlx5_core_dev *dev)
  386. {
  387. void *set_ctx = NULL;
  388. struct mlx5_profile *prof = dev->profile;
  389. int err = -ENOMEM;
  390. int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
  391. void *set_hca_cap;
  392. set_ctx = kzalloc(set_sz, GFP_KERNEL);
  393. if (!set_ctx)
  394. goto query_ex;
  395. err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
  396. if (err)
  397. goto query_ex;
  398. set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
  399. capability);
  400. memcpy(set_hca_cap, dev->hca_caps_cur[MLX5_CAP_GENERAL],
  401. MLX5_ST_SZ_BYTES(cmd_hca_cap));
  402. mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
  403. mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
  404. 128);
  405. /* we limit the size of the pkey table to 128 entries for now */
  406. MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
  407. to_fw_pkey_sz(dev, 128));
  408. /* Check log_max_qp from HCA caps to set in current profile */
  409. if (MLX5_CAP_GEN_MAX(dev, log_max_qp) < profile[prof_sel].log_max_qp) {
  410. mlx5_core_warn(dev, "log_max_qp value in current profile is %d, changing it to HCA capability limit (%d)\n",
  411. profile[prof_sel].log_max_qp,
  412. MLX5_CAP_GEN_MAX(dev, log_max_qp));
  413. profile[prof_sel].log_max_qp = MLX5_CAP_GEN_MAX(dev, log_max_qp);
  414. }
  415. if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
  416. MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
  417. prof->log_max_qp);
  418. /* disable cmdif checksum */
  419. MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
  420. MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
  421. err = set_caps(dev, set_ctx, set_sz,
  422. MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
  423. query_ex:
  424. kfree(set_ctx);
  425. return err;
  426. }
  427. static int set_hca_ctrl(struct mlx5_core_dev *dev)
  428. {
  429. struct mlx5_reg_host_endianess he_in;
  430. struct mlx5_reg_host_endianess he_out;
  431. int err;
  432. if (!mlx5_core_is_pf(dev))
  433. return 0;
  434. memset(&he_in, 0, sizeof(he_in));
  435. he_in.he = MLX5_SET_HOST_ENDIANNESS;
  436. err = mlx5_core_access_reg(dev, &he_in, sizeof(he_in),
  437. &he_out, sizeof(he_out),
  438. MLX5_REG_HOST_ENDIANNESS, 0, 1);
  439. return err;
  440. }
  441. int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
  442. {
  443. u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {0};
  444. u32 in[MLX5_ST_SZ_DW(enable_hca_in)] = {0};
  445. MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
  446. MLX5_SET(enable_hca_in, in, function_id, func_id);
  447. return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  448. }
  449. int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
  450. {
  451. u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {0};
  452. u32 in[MLX5_ST_SZ_DW(disable_hca_in)] = {0};
  453. MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
  454. MLX5_SET(disable_hca_in, in, function_id, func_id);
  455. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  456. }
  457. cycle_t mlx5_read_internal_timer(struct mlx5_core_dev *dev)
  458. {
  459. u32 timer_h, timer_h1, timer_l;
  460. timer_h = ioread32be(&dev->iseg->internal_timer_h);
  461. timer_l = ioread32be(&dev->iseg->internal_timer_l);
  462. timer_h1 = ioread32be(&dev->iseg->internal_timer_h);
  463. if (timer_h != timer_h1) /* wrap around */
  464. timer_l = ioread32be(&dev->iseg->internal_timer_l);
  465. return (cycle_t)timer_l | (cycle_t)timer_h1 << 32;
  466. }
  467. static int mlx5_irq_set_affinity_hint(struct mlx5_core_dev *mdev, int i)
  468. {
  469. struct mlx5_priv *priv = &mdev->priv;
  470. struct msix_entry *msix = priv->msix_arr;
  471. int irq = msix[i + MLX5_EQ_VEC_COMP_BASE].vector;
  472. if (!zalloc_cpumask_var(&priv->irq_info[i].mask, GFP_KERNEL)) {
  473. mlx5_core_warn(mdev, "zalloc_cpumask_var failed");
  474. return -ENOMEM;
  475. }
  476. cpumask_set_cpu(cpumask_local_spread(i, priv->numa_node),
  477. priv->irq_info[i].mask);
  478. if (IS_ENABLED(CONFIG_SMP) &&
  479. irq_set_affinity_hint(irq, priv->irq_info[i].mask))
  480. mlx5_core_warn(mdev, "irq_set_affinity_hint failed, irq 0x%.4x", irq);
  481. return 0;
  482. }
  483. static void mlx5_irq_clear_affinity_hint(struct mlx5_core_dev *mdev, int i)
  484. {
  485. struct mlx5_priv *priv = &mdev->priv;
  486. struct msix_entry *msix = priv->msix_arr;
  487. int irq = msix[i + MLX5_EQ_VEC_COMP_BASE].vector;
  488. irq_set_affinity_hint(irq, NULL);
  489. free_cpumask_var(priv->irq_info[i].mask);
  490. }
  491. static int mlx5_irq_set_affinity_hints(struct mlx5_core_dev *mdev)
  492. {
  493. int err;
  494. int i;
  495. for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++) {
  496. err = mlx5_irq_set_affinity_hint(mdev, i);
  497. if (err)
  498. goto err_out;
  499. }
  500. return 0;
  501. err_out:
  502. for (i--; i >= 0; i--)
  503. mlx5_irq_clear_affinity_hint(mdev, i);
  504. return err;
  505. }
  506. static void mlx5_irq_clear_affinity_hints(struct mlx5_core_dev *mdev)
  507. {
  508. int i;
  509. for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++)
  510. mlx5_irq_clear_affinity_hint(mdev, i);
  511. }
  512. int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
  513. unsigned int *irqn)
  514. {
  515. struct mlx5_eq_table *table = &dev->priv.eq_table;
  516. struct mlx5_eq *eq, *n;
  517. int err = -ENOENT;
  518. spin_lock(&table->lock);
  519. list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
  520. if (eq->index == vector) {
  521. *eqn = eq->eqn;
  522. *irqn = eq->irqn;
  523. err = 0;
  524. break;
  525. }
  526. }
  527. spin_unlock(&table->lock);
  528. return err;
  529. }
  530. EXPORT_SYMBOL(mlx5_vector2eqn);
  531. struct mlx5_eq *mlx5_eqn2eq(struct mlx5_core_dev *dev, int eqn)
  532. {
  533. struct mlx5_eq_table *table = &dev->priv.eq_table;
  534. struct mlx5_eq *eq;
  535. spin_lock(&table->lock);
  536. list_for_each_entry(eq, &table->comp_eqs_list, list)
  537. if (eq->eqn == eqn) {
  538. spin_unlock(&table->lock);
  539. return eq;
  540. }
  541. spin_unlock(&table->lock);
  542. return ERR_PTR(-ENOENT);
  543. }
  544. static void free_comp_eqs(struct mlx5_core_dev *dev)
  545. {
  546. struct mlx5_eq_table *table = &dev->priv.eq_table;
  547. struct mlx5_eq *eq, *n;
  548. #ifdef CONFIG_RFS_ACCEL
  549. if (dev->rmap) {
  550. free_irq_cpu_rmap(dev->rmap);
  551. dev->rmap = NULL;
  552. }
  553. #endif
  554. spin_lock(&table->lock);
  555. list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
  556. list_del(&eq->list);
  557. spin_unlock(&table->lock);
  558. if (mlx5_destroy_unmap_eq(dev, eq))
  559. mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
  560. eq->eqn);
  561. kfree(eq);
  562. spin_lock(&table->lock);
  563. }
  564. spin_unlock(&table->lock);
  565. }
  566. static int alloc_comp_eqs(struct mlx5_core_dev *dev)
  567. {
  568. struct mlx5_eq_table *table = &dev->priv.eq_table;
  569. char name[MLX5_MAX_IRQ_NAME];
  570. struct mlx5_eq *eq;
  571. int ncomp_vec;
  572. int nent;
  573. int err;
  574. int i;
  575. INIT_LIST_HEAD(&table->comp_eqs_list);
  576. ncomp_vec = table->num_comp_vectors;
  577. nent = MLX5_COMP_EQ_SIZE;
  578. #ifdef CONFIG_RFS_ACCEL
  579. dev->rmap = alloc_irq_cpu_rmap(ncomp_vec);
  580. if (!dev->rmap)
  581. return -ENOMEM;
  582. #endif
  583. for (i = 0; i < ncomp_vec; i++) {
  584. eq = kzalloc(sizeof(*eq), GFP_KERNEL);
  585. if (!eq) {
  586. err = -ENOMEM;
  587. goto clean;
  588. }
  589. #ifdef CONFIG_RFS_ACCEL
  590. irq_cpu_rmap_add(dev->rmap,
  591. dev->priv.msix_arr[i + MLX5_EQ_VEC_COMP_BASE].vector);
  592. #endif
  593. snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
  594. err = mlx5_create_map_eq(dev, eq,
  595. i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
  596. name, &dev->priv.uuari.uars[0]);
  597. if (err) {
  598. kfree(eq);
  599. goto clean;
  600. }
  601. mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
  602. eq->index = i;
  603. spin_lock(&table->lock);
  604. list_add_tail(&eq->list, &table->comp_eqs_list);
  605. spin_unlock(&table->lock);
  606. }
  607. return 0;
  608. clean:
  609. free_comp_eqs(dev);
  610. return err;
  611. }
  612. static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
  613. {
  614. u32 query_in[MLX5_ST_SZ_DW(query_issi_in)] = {0};
  615. u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {0};
  616. u32 sup_issi;
  617. int err;
  618. MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
  619. err = mlx5_cmd_exec(dev, query_in, sizeof(query_in),
  620. query_out, sizeof(query_out));
  621. if (err) {
  622. u32 syndrome;
  623. u8 status;
  624. mlx5_cmd_mbox_status(query_out, &status, &syndrome);
  625. if (!status || syndrome == MLX5_DRIVER_SYND) {
  626. mlx5_core_err(dev, "Failed to query ISSI err(%d) status(%d) synd(%d)\n",
  627. err, status, syndrome);
  628. return err;
  629. }
  630. mlx5_core_warn(dev, "Query ISSI is not supported by FW, ISSI is 0\n");
  631. dev->issi = 0;
  632. return 0;
  633. }
  634. sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
  635. if (sup_issi & (1 << 1)) {
  636. u32 set_in[MLX5_ST_SZ_DW(set_issi_in)] = {0};
  637. u32 set_out[MLX5_ST_SZ_DW(set_issi_out)] = {0};
  638. MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
  639. MLX5_SET(set_issi_in, set_in, current_issi, 1);
  640. err = mlx5_cmd_exec(dev, set_in, sizeof(set_in),
  641. set_out, sizeof(set_out));
  642. if (err) {
  643. mlx5_core_err(dev, "Failed to set ISSI to 1 err(%d)\n",
  644. err);
  645. return err;
  646. }
  647. dev->issi = 1;
  648. return 0;
  649. } else if (sup_issi & (1 << 0) || !sup_issi) {
  650. return 0;
  651. }
  652. return -ENOTSUPP;
  653. }
  654. static int mlx5_pci_init(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
  655. {
  656. struct pci_dev *pdev = dev->pdev;
  657. int err = 0;
  658. pci_set_drvdata(dev->pdev, dev);
  659. strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
  660. priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
  661. mutex_init(&priv->pgdir_mutex);
  662. INIT_LIST_HEAD(&priv->pgdir_list);
  663. spin_lock_init(&priv->mkey_lock);
  664. mutex_init(&priv->alloc_mutex);
  665. priv->numa_node = dev_to_node(&dev->pdev->dev);
  666. priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
  667. if (!priv->dbg_root)
  668. return -ENOMEM;
  669. err = mlx5_pci_enable_device(dev);
  670. if (err) {
  671. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  672. goto err_dbg;
  673. }
  674. err = request_bar(pdev);
  675. if (err) {
  676. dev_err(&pdev->dev, "error requesting BARs, aborting\n");
  677. goto err_disable;
  678. }
  679. pci_set_master(pdev);
  680. err = set_dma_caps(pdev);
  681. if (err) {
  682. dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
  683. goto err_clr_master;
  684. }
  685. dev->iseg_base = pci_resource_start(dev->pdev, 0);
  686. dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
  687. if (!dev->iseg) {
  688. err = -ENOMEM;
  689. dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
  690. goto err_clr_master;
  691. }
  692. return 0;
  693. err_clr_master:
  694. pci_clear_master(dev->pdev);
  695. release_bar(dev->pdev);
  696. err_disable:
  697. mlx5_pci_disable_device(dev);
  698. err_dbg:
  699. debugfs_remove(priv->dbg_root);
  700. return err;
  701. }
  702. static void mlx5_pci_close(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
  703. {
  704. iounmap(dev->iseg);
  705. pci_clear_master(dev->pdev);
  706. release_bar(dev->pdev);
  707. mlx5_pci_disable_device(dev);
  708. debugfs_remove(priv->dbg_root);
  709. }
  710. static int mlx5_init_once(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
  711. {
  712. struct pci_dev *pdev = dev->pdev;
  713. int err;
  714. err = mlx5_query_board_id(dev);
  715. if (err) {
  716. dev_err(&pdev->dev, "query board id failed\n");
  717. goto out;
  718. }
  719. err = mlx5_eq_init(dev);
  720. if (err) {
  721. dev_err(&pdev->dev, "failed to initialize eq\n");
  722. goto out;
  723. }
  724. MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock);
  725. err = mlx5_init_cq_table(dev);
  726. if (err) {
  727. dev_err(&pdev->dev, "failed to initialize cq table\n");
  728. goto err_eq_cleanup;
  729. }
  730. mlx5_init_qp_table(dev);
  731. mlx5_init_srq_table(dev);
  732. mlx5_init_mkey_table(dev);
  733. err = mlx5_init_rl_table(dev);
  734. if (err) {
  735. dev_err(&pdev->dev, "Failed to init rate limiting\n");
  736. goto err_tables_cleanup;
  737. }
  738. #ifdef CONFIG_MLX5_CORE_EN
  739. err = mlx5_eswitch_init(dev);
  740. if (err) {
  741. dev_err(&pdev->dev, "Failed to init eswitch %d\n", err);
  742. goto err_rl_cleanup;
  743. }
  744. #endif
  745. err = mlx5_sriov_init(dev);
  746. if (err) {
  747. dev_err(&pdev->dev, "Failed to init sriov %d\n", err);
  748. goto err_eswitch_cleanup;
  749. }
  750. return 0;
  751. err_eswitch_cleanup:
  752. #ifdef CONFIG_MLX5_CORE_EN
  753. mlx5_eswitch_cleanup(dev->priv.eswitch);
  754. err_rl_cleanup:
  755. #endif
  756. mlx5_cleanup_rl_table(dev);
  757. err_tables_cleanup:
  758. mlx5_cleanup_mkey_table(dev);
  759. mlx5_cleanup_srq_table(dev);
  760. mlx5_cleanup_qp_table(dev);
  761. mlx5_cleanup_cq_table(dev);
  762. err_eq_cleanup:
  763. mlx5_eq_cleanup(dev);
  764. out:
  765. return err;
  766. }
  767. static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
  768. {
  769. mlx5_sriov_cleanup(dev);
  770. #ifdef CONFIG_MLX5_CORE_EN
  771. mlx5_eswitch_cleanup(dev->priv.eswitch);
  772. #endif
  773. mlx5_cleanup_rl_table(dev);
  774. mlx5_cleanup_mkey_table(dev);
  775. mlx5_cleanup_srq_table(dev);
  776. mlx5_cleanup_qp_table(dev);
  777. mlx5_cleanup_cq_table(dev);
  778. mlx5_eq_cleanup(dev);
  779. }
  780. static int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
  781. bool boot)
  782. {
  783. struct pci_dev *pdev = dev->pdev;
  784. int err;
  785. mutex_lock(&dev->intf_state_mutex);
  786. if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
  787. dev_warn(&dev->pdev->dev, "%s: interface is up, NOP\n",
  788. __func__);
  789. goto out;
  790. }
  791. dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
  792. fw_rev_min(dev), fw_rev_sub(dev));
  793. /* on load removing any previous indication of internal error, device is
  794. * up
  795. */
  796. dev->state = MLX5_DEVICE_STATE_UP;
  797. /* wait for firmware to accept initialization segments configurations
  798. */
  799. err = wait_fw_init(dev, FW_PRE_INIT_TIMEOUT_MILI);
  800. if (err) {
  801. dev_err(&dev->pdev->dev, "Firmware over %d MS in pre-initializing state, aborting\n",
  802. FW_PRE_INIT_TIMEOUT_MILI);
  803. goto out;
  804. }
  805. err = mlx5_cmd_init(dev);
  806. if (err) {
  807. dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
  808. goto out_err;
  809. }
  810. err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI);
  811. if (err) {
  812. dev_err(&dev->pdev->dev, "Firmware over %d MS in initializing state, aborting\n",
  813. FW_INIT_TIMEOUT_MILI);
  814. goto err_cmd_cleanup;
  815. }
  816. err = mlx5_core_enable_hca(dev, 0);
  817. if (err) {
  818. dev_err(&pdev->dev, "enable hca failed\n");
  819. goto err_cmd_cleanup;
  820. }
  821. err = mlx5_core_set_issi(dev);
  822. if (err) {
  823. dev_err(&pdev->dev, "failed to set issi\n");
  824. goto err_disable_hca;
  825. }
  826. err = mlx5_satisfy_startup_pages(dev, 1);
  827. if (err) {
  828. dev_err(&pdev->dev, "failed to allocate boot pages\n");
  829. goto err_disable_hca;
  830. }
  831. err = set_hca_ctrl(dev);
  832. if (err) {
  833. dev_err(&pdev->dev, "set_hca_ctrl failed\n");
  834. goto reclaim_boot_pages;
  835. }
  836. err = handle_hca_cap(dev);
  837. if (err) {
  838. dev_err(&pdev->dev, "handle_hca_cap failed\n");
  839. goto reclaim_boot_pages;
  840. }
  841. err = handle_hca_cap_atomic(dev);
  842. if (err) {
  843. dev_err(&pdev->dev, "handle_hca_cap_atomic failed\n");
  844. goto reclaim_boot_pages;
  845. }
  846. err = mlx5_satisfy_startup_pages(dev, 0);
  847. if (err) {
  848. dev_err(&pdev->dev, "failed to allocate init pages\n");
  849. goto reclaim_boot_pages;
  850. }
  851. err = mlx5_pagealloc_start(dev);
  852. if (err) {
  853. dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
  854. goto reclaim_boot_pages;
  855. }
  856. err = mlx5_cmd_init_hca(dev);
  857. if (err) {
  858. dev_err(&pdev->dev, "init hca failed\n");
  859. goto err_pagealloc_stop;
  860. }
  861. mlx5_start_health_poll(dev);
  862. err = mlx5_query_hca_caps(dev);
  863. if (err) {
  864. dev_err(&pdev->dev, "query hca failed\n");
  865. goto err_stop_poll;
  866. }
  867. if (boot && mlx5_init_once(dev, priv)) {
  868. dev_err(&pdev->dev, "sw objs init failed\n");
  869. goto err_stop_poll;
  870. }
  871. err = mlx5_enable_msix(dev);
  872. if (err) {
  873. dev_err(&pdev->dev, "enable msix failed\n");
  874. goto err_cleanup_once;
  875. }
  876. err = mlx5_alloc_uuars(dev, &priv->uuari);
  877. if (err) {
  878. dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
  879. goto err_disable_msix;
  880. }
  881. err = mlx5_start_eqs(dev);
  882. if (err) {
  883. dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
  884. goto err_free_uar;
  885. }
  886. err = alloc_comp_eqs(dev);
  887. if (err) {
  888. dev_err(&pdev->dev, "Failed to alloc completion EQs\n");
  889. goto err_stop_eqs;
  890. }
  891. err = mlx5_irq_set_affinity_hints(dev);
  892. if (err) {
  893. dev_err(&pdev->dev, "Failed to alloc affinity hint cpumask\n");
  894. goto err_affinity_hints;
  895. }
  896. err = mlx5_init_fs(dev);
  897. if (err) {
  898. dev_err(&pdev->dev, "Failed to init flow steering\n");
  899. goto err_fs;
  900. }
  901. #ifdef CONFIG_MLX5_CORE_EN
  902. mlx5_eswitch_attach(dev->priv.eswitch);
  903. #endif
  904. err = mlx5_sriov_attach(dev);
  905. if (err) {
  906. dev_err(&pdev->dev, "sriov init failed %d\n", err);
  907. goto err_sriov;
  908. }
  909. if (mlx5_device_registered(dev)) {
  910. mlx5_attach_device(dev);
  911. } else {
  912. err = mlx5_register_device(dev);
  913. if (err) {
  914. dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
  915. goto err_reg_dev;
  916. }
  917. }
  918. clear_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state);
  919. set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
  920. out:
  921. mutex_unlock(&dev->intf_state_mutex);
  922. return 0;
  923. err_reg_dev:
  924. mlx5_sriov_detach(dev);
  925. err_sriov:
  926. #ifdef CONFIG_MLX5_CORE_EN
  927. mlx5_eswitch_detach(dev->priv.eswitch);
  928. #endif
  929. mlx5_cleanup_fs(dev);
  930. err_fs:
  931. mlx5_irq_clear_affinity_hints(dev);
  932. err_affinity_hints:
  933. free_comp_eqs(dev);
  934. err_stop_eqs:
  935. mlx5_stop_eqs(dev);
  936. err_free_uar:
  937. mlx5_free_uuars(dev, &priv->uuari);
  938. err_disable_msix:
  939. mlx5_disable_msix(dev);
  940. err_cleanup_once:
  941. if (boot)
  942. mlx5_cleanup_once(dev);
  943. err_stop_poll:
  944. mlx5_stop_health_poll(dev);
  945. if (mlx5_cmd_teardown_hca(dev)) {
  946. dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
  947. goto out_err;
  948. }
  949. err_pagealloc_stop:
  950. mlx5_pagealloc_stop(dev);
  951. reclaim_boot_pages:
  952. mlx5_reclaim_startup_pages(dev);
  953. err_disable_hca:
  954. mlx5_core_disable_hca(dev, 0);
  955. err_cmd_cleanup:
  956. mlx5_cmd_cleanup(dev);
  957. out_err:
  958. dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
  959. mutex_unlock(&dev->intf_state_mutex);
  960. return err;
  961. }
  962. static int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
  963. bool cleanup)
  964. {
  965. int err = 0;
  966. if (cleanup)
  967. mlx5_drain_health_recovery(dev);
  968. mutex_lock(&dev->intf_state_mutex);
  969. if (test_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state)) {
  970. dev_warn(&dev->pdev->dev, "%s: interface is down, NOP\n",
  971. __func__);
  972. if (cleanup)
  973. mlx5_cleanup_once(dev);
  974. goto out;
  975. }
  976. if (mlx5_device_registered(dev))
  977. mlx5_detach_device(dev);
  978. mlx5_sriov_detach(dev);
  979. #ifdef CONFIG_MLX5_CORE_EN
  980. mlx5_eswitch_detach(dev->priv.eswitch);
  981. #endif
  982. mlx5_cleanup_fs(dev);
  983. mlx5_irq_clear_affinity_hints(dev);
  984. free_comp_eqs(dev);
  985. mlx5_stop_eqs(dev);
  986. mlx5_free_uuars(dev, &priv->uuari);
  987. mlx5_disable_msix(dev);
  988. if (cleanup)
  989. mlx5_cleanup_once(dev);
  990. mlx5_stop_health_poll(dev);
  991. err = mlx5_cmd_teardown_hca(dev);
  992. if (err) {
  993. dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
  994. goto out;
  995. }
  996. mlx5_pagealloc_stop(dev);
  997. mlx5_reclaim_startup_pages(dev);
  998. mlx5_core_disable_hca(dev, 0);
  999. mlx5_cmd_cleanup(dev);
  1000. out:
  1001. clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
  1002. set_bit(MLX5_INTERFACE_STATE_DOWN, &dev->intf_state);
  1003. mutex_unlock(&dev->intf_state_mutex);
  1004. return err;
  1005. }
  1006. struct mlx5_core_event_handler {
  1007. void (*event)(struct mlx5_core_dev *dev,
  1008. enum mlx5_dev_event event,
  1009. void *data);
  1010. };
  1011. static const struct devlink_ops mlx5_devlink_ops = {
  1012. #ifdef CONFIG_MLX5_CORE_EN
  1013. .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
  1014. .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
  1015. #endif
  1016. };
  1017. #define MLX5_IB_MOD "mlx5_ib"
  1018. static int init_one(struct pci_dev *pdev,
  1019. const struct pci_device_id *id)
  1020. {
  1021. struct mlx5_core_dev *dev;
  1022. struct devlink *devlink;
  1023. struct mlx5_priv *priv;
  1024. int err;
  1025. devlink = devlink_alloc(&mlx5_devlink_ops, sizeof(*dev));
  1026. if (!devlink) {
  1027. dev_err(&pdev->dev, "kzalloc failed\n");
  1028. return -ENOMEM;
  1029. }
  1030. dev = devlink_priv(devlink);
  1031. priv = &dev->priv;
  1032. priv->pci_dev_data = id->driver_data;
  1033. pci_set_drvdata(pdev, dev);
  1034. dev->pdev = pdev;
  1035. dev->event = mlx5_core_event;
  1036. dev->profile = &profile[prof_sel];
  1037. INIT_LIST_HEAD(&priv->ctx_list);
  1038. spin_lock_init(&priv->ctx_lock);
  1039. mutex_init(&dev->pci_status_mutex);
  1040. mutex_init(&dev->intf_state_mutex);
  1041. err = mlx5_pci_init(dev, priv);
  1042. if (err) {
  1043. dev_err(&pdev->dev, "mlx5_pci_init failed with error code %d\n", err);
  1044. goto clean_dev;
  1045. }
  1046. err = mlx5_health_init(dev);
  1047. if (err) {
  1048. dev_err(&pdev->dev, "mlx5_health_init failed with error code %d\n", err);
  1049. goto close_pci;
  1050. }
  1051. mlx5_pagealloc_init(dev);
  1052. err = mlx5_load_one(dev, priv, true);
  1053. if (err) {
  1054. dev_err(&pdev->dev, "mlx5_load_one failed with error code %d\n", err);
  1055. goto clean_health;
  1056. }
  1057. err = request_module_nowait(MLX5_IB_MOD);
  1058. if (err)
  1059. pr_info("failed request module on %s\n", MLX5_IB_MOD);
  1060. err = devlink_register(devlink, &pdev->dev);
  1061. if (err)
  1062. goto clean_load;
  1063. pci_save_state(pdev);
  1064. return 0;
  1065. clean_load:
  1066. mlx5_unload_one(dev, priv, true);
  1067. clean_health:
  1068. mlx5_pagealloc_cleanup(dev);
  1069. mlx5_health_cleanup(dev);
  1070. close_pci:
  1071. mlx5_pci_close(dev, priv);
  1072. clean_dev:
  1073. pci_set_drvdata(pdev, NULL);
  1074. devlink_free(devlink);
  1075. return err;
  1076. }
  1077. static void remove_one(struct pci_dev *pdev)
  1078. {
  1079. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  1080. struct devlink *devlink = priv_to_devlink(dev);
  1081. struct mlx5_priv *priv = &dev->priv;
  1082. devlink_unregister(devlink);
  1083. mlx5_unregister_device(dev);
  1084. if (mlx5_unload_one(dev, priv, true)) {
  1085. dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n");
  1086. mlx5_health_cleanup(dev);
  1087. return;
  1088. }
  1089. mlx5_pagealloc_cleanup(dev);
  1090. mlx5_health_cleanup(dev);
  1091. mlx5_pci_close(dev, priv);
  1092. pci_set_drvdata(pdev, NULL);
  1093. devlink_free(devlink);
  1094. }
  1095. static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
  1096. pci_channel_state_t state)
  1097. {
  1098. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  1099. struct mlx5_priv *priv = &dev->priv;
  1100. dev_info(&pdev->dev, "%s was called\n", __func__);
  1101. mlx5_enter_error_state(dev);
  1102. mlx5_unload_one(dev, priv, false);
  1103. /* In case of kernel call drain the health wq */
  1104. if (state) {
  1105. mlx5_drain_health_wq(dev);
  1106. mlx5_pci_disable_device(dev);
  1107. }
  1108. return state == pci_channel_io_perm_failure ?
  1109. PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
  1110. }
  1111. /* wait for the device to show vital signs by waiting
  1112. * for the health counter to start counting.
  1113. */
  1114. static int wait_vital(struct pci_dev *pdev)
  1115. {
  1116. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  1117. struct mlx5_core_health *health = &dev->priv.health;
  1118. const int niter = 100;
  1119. u32 last_count = 0;
  1120. u32 count;
  1121. int i;
  1122. for (i = 0; i < niter; i++) {
  1123. count = ioread32be(health->health_counter);
  1124. if (count && count != 0xffffffff) {
  1125. if (last_count && last_count != count) {
  1126. dev_info(&pdev->dev, "Counter value 0x%x after %d iterations\n", count, i);
  1127. return 0;
  1128. }
  1129. last_count = count;
  1130. }
  1131. msleep(50);
  1132. }
  1133. return -ETIMEDOUT;
  1134. }
  1135. static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
  1136. {
  1137. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  1138. int err;
  1139. dev_info(&pdev->dev, "%s was called\n", __func__);
  1140. err = mlx5_pci_enable_device(dev);
  1141. if (err) {
  1142. dev_err(&pdev->dev, "%s: mlx5_pci_enable_device failed with error code: %d\n"
  1143. , __func__, err);
  1144. return PCI_ERS_RESULT_DISCONNECT;
  1145. }
  1146. pci_set_master(pdev);
  1147. pci_restore_state(pdev);
  1148. pci_save_state(pdev);
  1149. if (wait_vital(pdev)) {
  1150. dev_err(&pdev->dev, "%s: wait_vital timed out\n", __func__);
  1151. return PCI_ERS_RESULT_DISCONNECT;
  1152. }
  1153. return PCI_ERS_RESULT_RECOVERED;
  1154. }
  1155. static void mlx5_pci_resume(struct pci_dev *pdev)
  1156. {
  1157. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  1158. struct mlx5_priv *priv = &dev->priv;
  1159. int err;
  1160. dev_info(&pdev->dev, "%s was called\n", __func__);
  1161. err = mlx5_load_one(dev, priv, false);
  1162. if (err)
  1163. dev_err(&pdev->dev, "%s: mlx5_load_one failed with error code: %d\n"
  1164. , __func__, err);
  1165. else
  1166. dev_info(&pdev->dev, "%s: device recovered\n", __func__);
  1167. }
  1168. static const struct pci_error_handlers mlx5_err_handler = {
  1169. .error_detected = mlx5_pci_err_detected,
  1170. .slot_reset = mlx5_pci_slot_reset,
  1171. .resume = mlx5_pci_resume
  1172. };
  1173. static void shutdown(struct pci_dev *pdev)
  1174. {
  1175. struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
  1176. struct mlx5_priv *priv = &dev->priv;
  1177. dev_info(&pdev->dev, "Shutdown was called\n");
  1178. /* Notify mlx5 clients that the kernel is being shut down */
  1179. set_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &dev->intf_state);
  1180. mlx5_unload_one(dev, priv, false);
  1181. mlx5_pci_disable_device(dev);
  1182. }
  1183. static const struct pci_device_id mlx5_core_pci_table[] = {
  1184. { PCI_VDEVICE(MELLANOX, 0x1011) }, /* Connect-IB */
  1185. { PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF}, /* Connect-IB VF */
  1186. { PCI_VDEVICE(MELLANOX, 0x1013) }, /* ConnectX-4 */
  1187. { PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF}, /* ConnectX-4 VF */
  1188. { PCI_VDEVICE(MELLANOX, 0x1015) }, /* ConnectX-4LX */
  1189. { PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF}, /* ConnectX-4LX VF */
  1190. { PCI_VDEVICE(MELLANOX, 0x1017) }, /* ConnectX-5, PCIe 3.0 */
  1191. { PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF}, /* ConnectX-5 VF */
  1192. { PCI_VDEVICE(MELLANOX, 0x1019) }, /* ConnectX-5, PCIe 4.0 */
  1193. { 0, }
  1194. };
  1195. MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
  1196. void mlx5_disable_device(struct mlx5_core_dev *dev)
  1197. {
  1198. mlx5_pci_err_detected(dev->pdev, 0);
  1199. }
  1200. void mlx5_recover_device(struct mlx5_core_dev *dev)
  1201. {
  1202. mlx5_pci_disable_device(dev);
  1203. if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED)
  1204. mlx5_pci_resume(dev->pdev);
  1205. }
  1206. static struct pci_driver mlx5_core_driver = {
  1207. .name = DRIVER_NAME,
  1208. .id_table = mlx5_core_pci_table,
  1209. .probe = init_one,
  1210. .remove = remove_one,
  1211. .shutdown = shutdown,
  1212. .err_handler = &mlx5_err_handler,
  1213. .sriov_configure = mlx5_core_sriov_configure,
  1214. };
  1215. static void mlx5_core_verify_params(void)
  1216. {
  1217. if (prof_sel >= ARRAY_SIZE(profile)) {
  1218. pr_warn("mlx5_core: WARNING: Invalid module parameter prof_sel %d, valid range 0-%zu, changing back to default(%d)\n",
  1219. prof_sel,
  1220. ARRAY_SIZE(profile) - 1,
  1221. MLX5_DEFAULT_PROF);
  1222. prof_sel = MLX5_DEFAULT_PROF;
  1223. }
  1224. }
  1225. static int __init init(void)
  1226. {
  1227. int err;
  1228. mlx5_core_verify_params();
  1229. mlx5_register_debugfs();
  1230. err = pci_register_driver(&mlx5_core_driver);
  1231. if (err)
  1232. goto err_debug;
  1233. #ifdef CONFIG_MLX5_CORE_EN
  1234. mlx5e_init();
  1235. #endif
  1236. return 0;
  1237. err_debug:
  1238. mlx5_unregister_debugfs();
  1239. return err;
  1240. }
  1241. static void __exit cleanup(void)
  1242. {
  1243. #ifdef CONFIG_MLX5_CORE_EN
  1244. mlx5e_cleanup();
  1245. #endif
  1246. pci_unregister_driver(&mlx5_core_driver);
  1247. mlx5_unregister_debugfs();
  1248. }
  1249. module_init(init);
  1250. module_exit(cleanup);