main.c 41 KB

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