qed_main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /* QLogic qed NIC Driver
  2. * Copyright (c) 2015 QLogic Corporation
  3. *
  4. * This software is available under the terms of the GNU General Public License
  5. * (GPL) Version 2, available from the file COPYING in the main directory of
  6. * this source tree.
  7. */
  8. #include <linux/stddef.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/version.h>
  13. #include <linux/delay.h>
  14. #include <asm/byteorder.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/string.h>
  17. #include <linux/module.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/ethtool.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/qed/qed_if.h>
  24. #include "qed.h"
  25. #include "qed_sp.h"
  26. #include "qed_dev_api.h"
  27. #include "qed_mcp.h"
  28. #include "qed_hw.h"
  29. static const char version[] =
  30. "QLogic QL4xxx 40G/100G Ethernet Driver qed " DRV_MODULE_VERSION "\n";
  31. MODULE_DESCRIPTION("QLogic 25G/40G/50G/100G Core Module");
  32. MODULE_LICENSE("GPL");
  33. MODULE_VERSION(DRV_MODULE_VERSION);
  34. #define FW_FILE_VERSION \
  35. __stringify(FW_MAJOR_VERSION) "." \
  36. __stringify(FW_MINOR_VERSION) "." \
  37. __stringify(FW_REVISION_VERSION) "." \
  38. __stringify(FW_ENGINEERING_VERSION)
  39. #define QED_FW_FILE_NAME \
  40. "qed/qed_init_values_zipped-" FW_FILE_VERSION ".bin"
  41. static int __init qed_init(void)
  42. {
  43. pr_notice("qed_init called\n");
  44. pr_info("%s", version);
  45. return 0;
  46. }
  47. static void __exit qed_cleanup(void)
  48. {
  49. pr_notice("qed_cleanup called\n");
  50. }
  51. module_init(qed_init);
  52. module_exit(qed_cleanup);
  53. /* Check if the DMA controller on the machine can properly handle the DMA
  54. * addressing required by the device.
  55. */
  56. static int qed_set_coherency_mask(struct qed_dev *cdev)
  57. {
  58. struct device *dev = &cdev->pdev->dev;
  59. if (dma_set_mask(dev, DMA_BIT_MASK(64)) == 0) {
  60. if (dma_set_coherent_mask(dev, DMA_BIT_MASK(64)) != 0) {
  61. DP_NOTICE(cdev,
  62. "Can't request 64-bit consistent allocations\n");
  63. return -EIO;
  64. }
  65. } else if (dma_set_mask(dev, DMA_BIT_MASK(32)) != 0) {
  66. DP_NOTICE(cdev, "Can't request 64b/32b DMA addresses\n");
  67. return -EIO;
  68. }
  69. return 0;
  70. }
  71. static void qed_free_pci(struct qed_dev *cdev)
  72. {
  73. struct pci_dev *pdev = cdev->pdev;
  74. if (cdev->doorbells)
  75. iounmap(cdev->doorbells);
  76. if (cdev->regview)
  77. iounmap(cdev->regview);
  78. if (atomic_read(&pdev->enable_cnt) == 1)
  79. pci_release_regions(pdev);
  80. pci_disable_device(pdev);
  81. }
  82. /* Performs PCI initializations as well as initializing PCI-related parameters
  83. * in the device structrue. Returns 0 in case of success.
  84. */
  85. static int qed_init_pci(struct qed_dev *cdev,
  86. struct pci_dev *pdev)
  87. {
  88. int rc;
  89. cdev->pdev = pdev;
  90. rc = pci_enable_device(pdev);
  91. if (rc) {
  92. DP_NOTICE(cdev, "Cannot enable PCI device\n");
  93. goto err0;
  94. }
  95. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  96. DP_NOTICE(cdev, "No memory region found in bar #0\n");
  97. rc = -EIO;
  98. goto err1;
  99. }
  100. if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  101. DP_NOTICE(cdev, "No memory region found in bar #2\n");
  102. rc = -EIO;
  103. goto err1;
  104. }
  105. if (atomic_read(&pdev->enable_cnt) == 1) {
  106. rc = pci_request_regions(pdev, "qed");
  107. if (rc) {
  108. DP_NOTICE(cdev,
  109. "Failed to request PCI memory resources\n");
  110. goto err1;
  111. }
  112. pci_set_master(pdev);
  113. pci_save_state(pdev);
  114. }
  115. if (!pci_is_pcie(pdev)) {
  116. DP_NOTICE(cdev, "The bus is not PCI Express\n");
  117. rc = -EIO;
  118. goto err2;
  119. }
  120. cdev->pci_params.pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  121. if (cdev->pci_params.pm_cap == 0)
  122. DP_NOTICE(cdev, "Cannot find power management capability\n");
  123. rc = qed_set_coherency_mask(cdev);
  124. if (rc)
  125. goto err2;
  126. cdev->pci_params.mem_start = pci_resource_start(pdev, 0);
  127. cdev->pci_params.mem_end = pci_resource_end(pdev, 0);
  128. cdev->pci_params.irq = pdev->irq;
  129. cdev->regview = pci_ioremap_bar(pdev, 0);
  130. if (!cdev->regview) {
  131. DP_NOTICE(cdev, "Cannot map register space, aborting\n");
  132. rc = -ENOMEM;
  133. goto err2;
  134. }
  135. cdev->db_phys_addr = pci_resource_start(cdev->pdev, 2);
  136. cdev->db_size = pci_resource_len(cdev->pdev, 2);
  137. cdev->doorbells = ioremap_wc(cdev->db_phys_addr, cdev->db_size);
  138. if (!cdev->doorbells) {
  139. DP_NOTICE(cdev, "Cannot map doorbell space\n");
  140. return -ENOMEM;
  141. }
  142. return 0;
  143. err2:
  144. pci_release_regions(pdev);
  145. err1:
  146. pci_disable_device(pdev);
  147. err0:
  148. return rc;
  149. }
  150. int qed_fill_dev_info(struct qed_dev *cdev,
  151. struct qed_dev_info *dev_info)
  152. {
  153. struct qed_ptt *ptt;
  154. memset(dev_info, 0, sizeof(struct qed_dev_info));
  155. dev_info->num_hwfns = cdev->num_hwfns;
  156. dev_info->pci_mem_start = cdev->pci_params.mem_start;
  157. dev_info->pci_mem_end = cdev->pci_params.mem_end;
  158. dev_info->pci_irq = cdev->pci_params.irq;
  159. dev_info->is_mf = IS_MF(&cdev->hwfns[0]);
  160. ether_addr_copy(dev_info->hw_mac, cdev->hwfns[0].hw_info.hw_mac_addr);
  161. dev_info->fw_major = FW_MAJOR_VERSION;
  162. dev_info->fw_minor = FW_MINOR_VERSION;
  163. dev_info->fw_rev = FW_REVISION_VERSION;
  164. dev_info->fw_eng = FW_ENGINEERING_VERSION;
  165. dev_info->mf_mode = cdev->mf_mode;
  166. qed_mcp_get_mfw_ver(cdev, &dev_info->mfw_rev);
  167. ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
  168. if (ptt) {
  169. qed_mcp_get_flash_size(QED_LEADING_HWFN(cdev), ptt,
  170. &dev_info->flash_size);
  171. qed_ptt_release(QED_LEADING_HWFN(cdev), ptt);
  172. }
  173. return 0;
  174. }
  175. static void qed_free_cdev(struct qed_dev *cdev)
  176. {
  177. kfree((void *)cdev);
  178. }
  179. static struct qed_dev *qed_alloc_cdev(struct pci_dev *pdev)
  180. {
  181. struct qed_dev *cdev;
  182. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  183. if (!cdev)
  184. return cdev;
  185. qed_init_struct(cdev);
  186. return cdev;
  187. }
  188. /* Sets the requested power state */
  189. static int qed_set_power_state(struct qed_dev *cdev,
  190. pci_power_t state)
  191. {
  192. if (!cdev)
  193. return -ENODEV;
  194. DP_VERBOSE(cdev, NETIF_MSG_DRV, "Omitting Power state change\n");
  195. return 0;
  196. }
  197. /* probing */
  198. static struct qed_dev *qed_probe(struct pci_dev *pdev,
  199. enum qed_protocol protocol,
  200. u32 dp_module,
  201. u8 dp_level)
  202. {
  203. struct qed_dev *cdev;
  204. int rc;
  205. cdev = qed_alloc_cdev(pdev);
  206. if (!cdev)
  207. goto err0;
  208. cdev->protocol = protocol;
  209. qed_init_dp(cdev, dp_module, dp_level);
  210. rc = qed_init_pci(cdev, pdev);
  211. if (rc) {
  212. DP_ERR(cdev, "init pci failed\n");
  213. goto err1;
  214. }
  215. DP_INFO(cdev, "PCI init completed successfully\n");
  216. rc = qed_hw_prepare(cdev, QED_PCI_DEFAULT);
  217. if (rc) {
  218. DP_ERR(cdev, "hw prepare failed\n");
  219. goto err2;
  220. }
  221. DP_INFO(cdev, "qed_probe completed successffuly\n");
  222. return cdev;
  223. err2:
  224. qed_free_pci(cdev);
  225. err1:
  226. qed_free_cdev(cdev);
  227. err0:
  228. return NULL;
  229. }
  230. static void qed_remove(struct qed_dev *cdev)
  231. {
  232. if (!cdev)
  233. return;
  234. qed_hw_remove(cdev);
  235. qed_free_pci(cdev);
  236. qed_set_power_state(cdev, PCI_D3hot);
  237. qed_free_cdev(cdev);
  238. }
  239. static void qed_disable_msix(struct qed_dev *cdev)
  240. {
  241. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  242. pci_disable_msix(cdev->pdev);
  243. kfree(cdev->int_params.msix_table);
  244. } else if (cdev->int_params.out.int_mode == QED_INT_MODE_MSI) {
  245. pci_disable_msi(cdev->pdev);
  246. }
  247. memset(&cdev->int_params.out, 0, sizeof(struct qed_int_param));
  248. }
  249. static int qed_enable_msix(struct qed_dev *cdev,
  250. struct qed_int_params *int_params)
  251. {
  252. int i, rc, cnt;
  253. cnt = int_params->in.num_vectors;
  254. for (i = 0; i < cnt; i++)
  255. int_params->msix_table[i].entry = i;
  256. rc = pci_enable_msix_range(cdev->pdev, int_params->msix_table,
  257. int_params->in.min_msix_cnt, cnt);
  258. if (rc < cnt && rc >= int_params->in.min_msix_cnt &&
  259. (rc % cdev->num_hwfns)) {
  260. pci_disable_msix(cdev->pdev);
  261. /* If fastpath is initialized, we need at least one interrupt
  262. * per hwfn [and the slow path interrupts]. New requested number
  263. * should be a multiple of the number of hwfns.
  264. */
  265. cnt = (rc / cdev->num_hwfns) * cdev->num_hwfns;
  266. DP_NOTICE(cdev,
  267. "Trying to enable MSI-X with less vectors (%d out of %d)\n",
  268. cnt, int_params->in.num_vectors);
  269. rc = pci_enable_msix_exact(cdev->pdev,
  270. int_params->msix_table, cnt);
  271. if (!rc)
  272. rc = cnt;
  273. }
  274. if (rc > 0) {
  275. /* MSI-x configuration was achieved */
  276. int_params->out.int_mode = QED_INT_MODE_MSIX;
  277. int_params->out.num_vectors = rc;
  278. rc = 0;
  279. } else {
  280. DP_NOTICE(cdev,
  281. "Failed to enable MSI-X [Requested %d vectors][rc %d]\n",
  282. cnt, rc);
  283. }
  284. return rc;
  285. }
  286. /* This function outputs the int mode and the number of enabled msix vector */
  287. static int qed_set_int_mode(struct qed_dev *cdev, bool force_mode)
  288. {
  289. struct qed_int_params *int_params = &cdev->int_params;
  290. struct msix_entry *tbl;
  291. int rc = 0, cnt;
  292. switch (int_params->in.int_mode) {
  293. case QED_INT_MODE_MSIX:
  294. /* Allocate MSIX table */
  295. cnt = int_params->in.num_vectors;
  296. int_params->msix_table = kcalloc(cnt, sizeof(*tbl), GFP_KERNEL);
  297. if (!int_params->msix_table) {
  298. rc = -ENOMEM;
  299. goto out;
  300. }
  301. /* Enable MSIX */
  302. rc = qed_enable_msix(cdev, int_params);
  303. if (!rc)
  304. goto out;
  305. DP_NOTICE(cdev, "Failed to enable MSI-X\n");
  306. kfree(int_params->msix_table);
  307. if (force_mode)
  308. goto out;
  309. /* Fallthrough */
  310. case QED_INT_MODE_MSI:
  311. rc = pci_enable_msi(cdev->pdev);
  312. if (!rc) {
  313. int_params->out.int_mode = QED_INT_MODE_MSI;
  314. goto out;
  315. }
  316. DP_NOTICE(cdev, "Failed to enable MSI\n");
  317. if (force_mode)
  318. goto out;
  319. /* Fallthrough */
  320. case QED_INT_MODE_INTA:
  321. int_params->out.int_mode = QED_INT_MODE_INTA;
  322. rc = 0;
  323. goto out;
  324. default:
  325. DP_NOTICE(cdev, "Unknown int_mode value %d\n",
  326. int_params->in.int_mode);
  327. rc = -EINVAL;
  328. }
  329. out:
  330. cdev->int_coalescing_mode = QED_COAL_MODE_ENABLE;
  331. return rc;
  332. }
  333. static void qed_simd_handler_config(struct qed_dev *cdev, void *token,
  334. int index, void(*handler)(void *))
  335. {
  336. struct qed_hwfn *hwfn = &cdev->hwfns[index % cdev->num_hwfns];
  337. int relative_idx = index / cdev->num_hwfns;
  338. hwfn->simd_proto_handler[relative_idx].func = handler;
  339. hwfn->simd_proto_handler[relative_idx].token = token;
  340. }
  341. static void qed_simd_handler_clean(struct qed_dev *cdev, int index)
  342. {
  343. struct qed_hwfn *hwfn = &cdev->hwfns[index % cdev->num_hwfns];
  344. int relative_idx = index / cdev->num_hwfns;
  345. memset(&hwfn->simd_proto_handler[relative_idx], 0,
  346. sizeof(struct qed_simd_fp_handler));
  347. }
  348. static irqreturn_t qed_msix_sp_int(int irq, void *tasklet)
  349. {
  350. tasklet_schedule((struct tasklet_struct *)tasklet);
  351. return IRQ_HANDLED;
  352. }
  353. static irqreturn_t qed_single_int(int irq, void *dev_instance)
  354. {
  355. struct qed_dev *cdev = (struct qed_dev *)dev_instance;
  356. struct qed_hwfn *hwfn;
  357. irqreturn_t rc = IRQ_NONE;
  358. u64 status;
  359. int i, j;
  360. for (i = 0; i < cdev->num_hwfns; i++) {
  361. status = qed_int_igu_read_sisr_reg(&cdev->hwfns[i]);
  362. if (!status)
  363. continue;
  364. hwfn = &cdev->hwfns[i];
  365. /* Slowpath interrupt */
  366. if (unlikely(status & 0x1)) {
  367. tasklet_schedule(hwfn->sp_dpc);
  368. status &= ~0x1;
  369. rc = IRQ_HANDLED;
  370. }
  371. /* Fastpath interrupts */
  372. for (j = 0; j < 64; j++) {
  373. if ((0x2ULL << j) & status) {
  374. hwfn->simd_proto_handler[j].func(
  375. hwfn->simd_proto_handler[j].token);
  376. status &= ~(0x2ULL << j);
  377. rc = IRQ_HANDLED;
  378. }
  379. }
  380. if (unlikely(status))
  381. DP_VERBOSE(hwfn, NETIF_MSG_INTR,
  382. "got an unknown interrupt status 0x%llx\n",
  383. status);
  384. }
  385. return rc;
  386. }
  387. int qed_slowpath_irq_req(struct qed_hwfn *hwfn)
  388. {
  389. struct qed_dev *cdev = hwfn->cdev;
  390. int rc = 0;
  391. u8 id;
  392. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  393. id = hwfn->my_id;
  394. snprintf(hwfn->name, NAME_SIZE, "sp-%d-%02x:%02x.%02x",
  395. id, cdev->pdev->bus->number,
  396. PCI_SLOT(cdev->pdev->devfn), hwfn->abs_pf_id);
  397. rc = request_irq(cdev->int_params.msix_table[id].vector,
  398. qed_msix_sp_int, 0, hwfn->name, hwfn->sp_dpc);
  399. if (!rc)
  400. DP_VERBOSE(hwfn, (NETIF_MSG_INTR | QED_MSG_SP),
  401. "Requested slowpath MSI-X\n");
  402. } else {
  403. unsigned long flags = 0;
  404. snprintf(cdev->name, NAME_SIZE, "%02x:%02x.%02x",
  405. cdev->pdev->bus->number, PCI_SLOT(cdev->pdev->devfn),
  406. PCI_FUNC(cdev->pdev->devfn));
  407. if (cdev->int_params.out.int_mode == QED_INT_MODE_INTA)
  408. flags |= IRQF_SHARED;
  409. rc = request_irq(cdev->pdev->irq, qed_single_int,
  410. flags, cdev->name, cdev);
  411. }
  412. return rc;
  413. }
  414. static void qed_slowpath_irq_free(struct qed_dev *cdev)
  415. {
  416. int i;
  417. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  418. for_each_hwfn(cdev, i) {
  419. if (!cdev->hwfns[i].b_int_requested)
  420. break;
  421. synchronize_irq(cdev->int_params.msix_table[i].vector);
  422. free_irq(cdev->int_params.msix_table[i].vector,
  423. cdev->hwfns[i].sp_dpc);
  424. }
  425. } else {
  426. if (QED_LEADING_HWFN(cdev)->b_int_requested)
  427. free_irq(cdev->pdev->irq, cdev);
  428. }
  429. qed_int_disable_post_isr_release(cdev);
  430. }
  431. static int qed_nic_stop(struct qed_dev *cdev)
  432. {
  433. int i, rc;
  434. rc = qed_hw_stop(cdev);
  435. for (i = 0; i < cdev->num_hwfns; i++) {
  436. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  437. if (p_hwfn->b_sp_dpc_enabled) {
  438. tasklet_disable(p_hwfn->sp_dpc);
  439. p_hwfn->b_sp_dpc_enabled = false;
  440. DP_VERBOSE(cdev, NETIF_MSG_IFDOWN,
  441. "Disabled sp taskelt [hwfn %d] at %p\n",
  442. i, p_hwfn->sp_dpc);
  443. }
  444. }
  445. return rc;
  446. }
  447. static int qed_nic_reset(struct qed_dev *cdev)
  448. {
  449. int rc;
  450. rc = qed_hw_reset(cdev);
  451. if (rc)
  452. return rc;
  453. qed_resc_free(cdev);
  454. return 0;
  455. }
  456. static int qed_nic_setup(struct qed_dev *cdev)
  457. {
  458. int rc;
  459. rc = qed_resc_alloc(cdev);
  460. if (rc)
  461. return rc;
  462. DP_INFO(cdev, "Allocated qed resources\n");
  463. qed_resc_setup(cdev);
  464. return rc;
  465. }
  466. static int qed_set_int_fp(struct qed_dev *cdev, u16 cnt)
  467. {
  468. int limit = 0;
  469. /* Mark the fastpath as free/used */
  470. cdev->int_params.fp_initialized = cnt ? true : false;
  471. if (cdev->int_params.out.int_mode != QED_INT_MODE_MSIX)
  472. limit = cdev->num_hwfns * 63;
  473. else if (cdev->int_params.fp_msix_cnt)
  474. limit = cdev->int_params.fp_msix_cnt;
  475. if (!limit)
  476. return -ENOMEM;
  477. return min_t(int, cnt, limit);
  478. }
  479. static int qed_get_int_fp(struct qed_dev *cdev, struct qed_int_info *info)
  480. {
  481. memset(info, 0, sizeof(struct qed_int_info));
  482. if (!cdev->int_params.fp_initialized) {
  483. DP_INFO(cdev,
  484. "Protocol driver requested interrupt information, but its support is not yet configured\n");
  485. return -EINVAL;
  486. }
  487. /* Need to expose only MSI-X information; Single IRQ is handled solely
  488. * by qed.
  489. */
  490. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  491. int msix_base = cdev->int_params.fp_msix_base;
  492. info->msix_cnt = cdev->int_params.fp_msix_cnt;
  493. info->msix = &cdev->int_params.msix_table[msix_base];
  494. }
  495. return 0;
  496. }
  497. static int qed_slowpath_setup_int(struct qed_dev *cdev,
  498. enum qed_int_mode int_mode)
  499. {
  500. int rc, i;
  501. u8 num_vectors = 0;
  502. memset(&cdev->int_params, 0, sizeof(struct qed_int_params));
  503. cdev->int_params.in.int_mode = int_mode;
  504. for_each_hwfn(cdev, i)
  505. num_vectors += qed_int_get_num_sbs(&cdev->hwfns[i], NULL) + 1;
  506. cdev->int_params.in.num_vectors = num_vectors;
  507. /* We want a minimum of one slowpath and one fastpath vector per hwfn */
  508. cdev->int_params.in.min_msix_cnt = cdev->num_hwfns * 2;
  509. rc = qed_set_int_mode(cdev, false);
  510. if (rc) {
  511. DP_ERR(cdev, "qed_slowpath_setup_int ERR\n");
  512. return rc;
  513. }
  514. cdev->int_params.fp_msix_base = cdev->num_hwfns;
  515. cdev->int_params.fp_msix_cnt = cdev->int_params.out.num_vectors -
  516. cdev->num_hwfns;
  517. return 0;
  518. }
  519. u32 qed_unzip_data(struct qed_hwfn *p_hwfn, u32 input_len,
  520. u8 *input_buf, u32 max_size, u8 *unzip_buf)
  521. {
  522. int rc;
  523. p_hwfn->stream->next_in = input_buf;
  524. p_hwfn->stream->avail_in = input_len;
  525. p_hwfn->stream->next_out = unzip_buf;
  526. p_hwfn->stream->avail_out = max_size;
  527. rc = zlib_inflateInit2(p_hwfn->stream, MAX_WBITS);
  528. if (rc != Z_OK) {
  529. DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "zlib init failed, rc = %d\n",
  530. rc);
  531. return 0;
  532. }
  533. rc = zlib_inflate(p_hwfn->stream, Z_FINISH);
  534. zlib_inflateEnd(p_hwfn->stream);
  535. if (rc != Z_OK && rc != Z_STREAM_END) {
  536. DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "FW unzip error: %s, rc=%d\n",
  537. p_hwfn->stream->msg, rc);
  538. return 0;
  539. }
  540. return p_hwfn->stream->total_out / 4;
  541. }
  542. static int qed_alloc_stream_mem(struct qed_dev *cdev)
  543. {
  544. int i;
  545. void *workspace;
  546. for_each_hwfn(cdev, i) {
  547. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  548. p_hwfn->stream = kzalloc(sizeof(*p_hwfn->stream), GFP_KERNEL);
  549. if (!p_hwfn->stream)
  550. return -ENOMEM;
  551. workspace = vzalloc(zlib_inflate_workspacesize());
  552. if (!workspace)
  553. return -ENOMEM;
  554. p_hwfn->stream->workspace = workspace;
  555. }
  556. return 0;
  557. }
  558. static void qed_free_stream_mem(struct qed_dev *cdev)
  559. {
  560. int i;
  561. for_each_hwfn(cdev, i) {
  562. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  563. if (!p_hwfn->stream)
  564. return;
  565. vfree(p_hwfn->stream->workspace);
  566. kfree(p_hwfn->stream);
  567. }
  568. }
  569. static void qed_update_pf_params(struct qed_dev *cdev,
  570. struct qed_pf_params *params)
  571. {
  572. int i;
  573. for (i = 0; i < cdev->num_hwfns; i++) {
  574. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  575. p_hwfn->pf_params = *params;
  576. }
  577. }
  578. static int qed_slowpath_start(struct qed_dev *cdev,
  579. struct qed_slowpath_params *params)
  580. {
  581. struct qed_mcp_drv_version drv_version;
  582. const u8 *data = NULL;
  583. struct qed_hwfn *hwfn;
  584. int rc;
  585. rc = request_firmware(&cdev->firmware, QED_FW_FILE_NAME,
  586. &cdev->pdev->dev);
  587. if (rc) {
  588. DP_NOTICE(cdev,
  589. "Failed to find fw file - /lib/firmware/%s\n",
  590. QED_FW_FILE_NAME);
  591. goto err;
  592. }
  593. rc = qed_nic_setup(cdev);
  594. if (rc)
  595. goto err;
  596. rc = qed_slowpath_setup_int(cdev, params->int_mode);
  597. if (rc)
  598. goto err1;
  599. /* Allocate stream for unzipping */
  600. rc = qed_alloc_stream_mem(cdev);
  601. if (rc) {
  602. DP_NOTICE(cdev, "Failed to allocate stream memory\n");
  603. goto err2;
  604. }
  605. /* Start the slowpath */
  606. data = cdev->firmware->data;
  607. rc = qed_hw_init(cdev, true, cdev->int_params.out.int_mode,
  608. true, data);
  609. if (rc)
  610. goto err3;
  611. DP_INFO(cdev,
  612. "HW initialization and function start completed successfully\n");
  613. hwfn = QED_LEADING_HWFN(cdev);
  614. drv_version.version = (params->drv_major << 24) |
  615. (params->drv_minor << 16) |
  616. (params->drv_rev << 8) |
  617. (params->drv_eng);
  618. strlcpy(drv_version.name, params->name,
  619. MCP_DRV_VER_STR_SIZE - 4);
  620. rc = qed_mcp_send_drv_version(hwfn, hwfn->p_main_ptt,
  621. &drv_version);
  622. if (rc) {
  623. DP_NOTICE(cdev, "Failed sending drv version command\n");
  624. return rc;
  625. }
  626. return 0;
  627. err3:
  628. qed_free_stream_mem(cdev);
  629. qed_slowpath_irq_free(cdev);
  630. err2:
  631. qed_disable_msix(cdev);
  632. err1:
  633. qed_resc_free(cdev);
  634. err:
  635. release_firmware(cdev->firmware);
  636. return rc;
  637. }
  638. static int qed_slowpath_stop(struct qed_dev *cdev)
  639. {
  640. if (!cdev)
  641. return -ENODEV;
  642. qed_free_stream_mem(cdev);
  643. qed_nic_stop(cdev);
  644. qed_slowpath_irq_free(cdev);
  645. qed_disable_msix(cdev);
  646. qed_nic_reset(cdev);
  647. release_firmware(cdev->firmware);
  648. return 0;
  649. }
  650. static void qed_set_id(struct qed_dev *cdev, char name[NAME_SIZE],
  651. char ver_str[VER_SIZE])
  652. {
  653. int i;
  654. memcpy(cdev->name, name, NAME_SIZE);
  655. for_each_hwfn(cdev, i)
  656. snprintf(cdev->hwfns[i].name, NAME_SIZE, "%s-%d", name, i);
  657. memcpy(cdev->ver_str, ver_str, VER_SIZE);
  658. cdev->drv_type = DRV_ID_DRV_TYPE_LINUX;
  659. }
  660. static u32 qed_sb_init(struct qed_dev *cdev,
  661. struct qed_sb_info *sb_info,
  662. void *sb_virt_addr,
  663. dma_addr_t sb_phy_addr, u16 sb_id,
  664. enum qed_sb_type type)
  665. {
  666. struct qed_hwfn *p_hwfn;
  667. int hwfn_index;
  668. u16 rel_sb_id;
  669. u8 n_hwfns;
  670. u32 rc;
  671. /* RoCE uses single engine and CMT uses two engines. When using both
  672. * we force only a single engine. Storage uses only engine 0 too.
  673. */
  674. if (type == QED_SB_TYPE_L2_QUEUE)
  675. n_hwfns = cdev->num_hwfns;
  676. else
  677. n_hwfns = 1;
  678. hwfn_index = sb_id % n_hwfns;
  679. p_hwfn = &cdev->hwfns[hwfn_index];
  680. rel_sb_id = sb_id / n_hwfns;
  681. DP_VERBOSE(cdev, NETIF_MSG_INTR,
  682. "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
  683. hwfn_index, rel_sb_id, sb_id);
  684. rc = qed_int_sb_init(p_hwfn, p_hwfn->p_main_ptt, sb_info,
  685. sb_virt_addr, sb_phy_addr, rel_sb_id);
  686. return rc;
  687. }
  688. static u32 qed_sb_release(struct qed_dev *cdev,
  689. struct qed_sb_info *sb_info,
  690. u16 sb_id)
  691. {
  692. struct qed_hwfn *p_hwfn;
  693. int hwfn_index;
  694. u16 rel_sb_id;
  695. u32 rc;
  696. hwfn_index = sb_id % cdev->num_hwfns;
  697. p_hwfn = &cdev->hwfns[hwfn_index];
  698. rel_sb_id = sb_id / cdev->num_hwfns;
  699. DP_VERBOSE(cdev, NETIF_MSG_INTR,
  700. "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
  701. hwfn_index, rel_sb_id, sb_id);
  702. rc = qed_int_sb_release(p_hwfn, sb_info, rel_sb_id);
  703. return rc;
  704. }
  705. static int qed_set_link(struct qed_dev *cdev,
  706. struct qed_link_params *params)
  707. {
  708. struct qed_hwfn *hwfn;
  709. struct qed_mcp_link_params *link_params;
  710. struct qed_ptt *ptt;
  711. int rc;
  712. if (!cdev)
  713. return -ENODEV;
  714. /* The link should be set only once per PF */
  715. hwfn = &cdev->hwfns[0];
  716. ptt = qed_ptt_acquire(hwfn);
  717. if (!ptt)
  718. return -EBUSY;
  719. link_params = qed_mcp_get_link_params(hwfn);
  720. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_AUTONEG)
  721. link_params->speed.autoneg = params->autoneg;
  722. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_ADV_SPEEDS) {
  723. link_params->speed.advertised_speeds = 0;
  724. if ((params->adv_speeds & SUPPORTED_1000baseT_Half) ||
  725. (params->adv_speeds & SUPPORTED_1000baseT_Full))
  726. link_params->speed.advertised_speeds |=
  727. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G;
  728. if (params->adv_speeds & SUPPORTED_10000baseKR_Full)
  729. link_params->speed.advertised_speeds |=
  730. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G;
  731. if (params->adv_speeds & SUPPORTED_40000baseLR4_Full)
  732. link_params->speed.advertised_speeds |=
  733. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G;
  734. if (params->adv_speeds & 0)
  735. link_params->speed.advertised_speeds |=
  736. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G;
  737. if (params->adv_speeds & 0)
  738. link_params->speed.advertised_speeds |=
  739. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_100G;
  740. }
  741. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_FORCED_SPEED)
  742. link_params->speed.forced_speed = params->forced_speed;
  743. rc = qed_mcp_set_link(hwfn, ptt, params->link_up);
  744. qed_ptt_release(hwfn, ptt);
  745. return rc;
  746. }
  747. static int qed_get_port_type(u32 media_type)
  748. {
  749. int port_type;
  750. switch (media_type) {
  751. case MEDIA_SFPP_10G_FIBER:
  752. case MEDIA_SFP_1G_FIBER:
  753. case MEDIA_XFP_FIBER:
  754. case MEDIA_KR:
  755. port_type = PORT_FIBRE;
  756. break;
  757. case MEDIA_DA_TWINAX:
  758. port_type = PORT_DA;
  759. break;
  760. case MEDIA_BASE_T:
  761. port_type = PORT_TP;
  762. break;
  763. case MEDIA_NOT_PRESENT:
  764. port_type = PORT_NONE;
  765. break;
  766. case MEDIA_UNSPECIFIED:
  767. default:
  768. port_type = PORT_OTHER;
  769. break;
  770. }
  771. return port_type;
  772. }
  773. static void qed_fill_link(struct qed_hwfn *hwfn,
  774. struct qed_link_output *if_link)
  775. {
  776. struct qed_mcp_link_params params;
  777. struct qed_mcp_link_state link;
  778. struct qed_mcp_link_capabilities link_caps;
  779. u32 media_type;
  780. memset(if_link, 0, sizeof(*if_link));
  781. /* Prepare source inputs */
  782. memcpy(&params, qed_mcp_get_link_params(hwfn), sizeof(params));
  783. memcpy(&link, qed_mcp_get_link_state(hwfn), sizeof(link));
  784. memcpy(&link_caps, qed_mcp_get_link_capabilities(hwfn),
  785. sizeof(link_caps));
  786. /* Set the link parameters to pass to protocol driver */
  787. if (link.link_up)
  788. if_link->link_up = true;
  789. /* TODO - at the moment assume supported and advertised speed equal */
  790. if_link->supported_caps = SUPPORTED_FIBRE;
  791. if (params.speed.autoneg)
  792. if_link->supported_caps |= SUPPORTED_Autoneg;
  793. if (params.pause.autoneg ||
  794. (params.pause.forced_rx && params.pause.forced_tx))
  795. if_link->supported_caps |= SUPPORTED_Asym_Pause;
  796. if (params.pause.autoneg || params.pause.forced_rx ||
  797. params.pause.forced_tx)
  798. if_link->supported_caps |= SUPPORTED_Pause;
  799. if_link->advertised_caps = if_link->supported_caps;
  800. if (params.speed.advertised_speeds &
  801. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G)
  802. if_link->advertised_caps |= SUPPORTED_1000baseT_Half |
  803. SUPPORTED_1000baseT_Full;
  804. if (params.speed.advertised_speeds &
  805. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G)
  806. if_link->advertised_caps |= SUPPORTED_10000baseKR_Full;
  807. if (params.speed.advertised_speeds &
  808. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G)
  809. if_link->advertised_caps |= SUPPORTED_40000baseLR4_Full;
  810. if (params.speed.advertised_speeds &
  811. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G)
  812. if_link->advertised_caps |= 0;
  813. if (params.speed.advertised_speeds &
  814. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_100G)
  815. if_link->advertised_caps |= 0;
  816. if (link_caps.speed_capabilities &
  817. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G)
  818. if_link->supported_caps |= SUPPORTED_1000baseT_Half |
  819. SUPPORTED_1000baseT_Full;
  820. if (link_caps.speed_capabilities &
  821. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G)
  822. if_link->supported_caps |= SUPPORTED_10000baseKR_Full;
  823. if (link_caps.speed_capabilities &
  824. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G)
  825. if_link->supported_caps |= SUPPORTED_40000baseLR4_Full;
  826. if (link_caps.speed_capabilities &
  827. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G)
  828. if_link->supported_caps |= 0;
  829. if (link_caps.speed_capabilities &
  830. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_100G)
  831. if_link->supported_caps |= 0;
  832. if (link.link_up)
  833. if_link->speed = link.speed;
  834. /* TODO - fill duplex properly */
  835. if_link->duplex = DUPLEX_FULL;
  836. qed_mcp_get_media_type(hwfn->cdev, &media_type);
  837. if_link->port = qed_get_port_type(media_type);
  838. if_link->autoneg = params.speed.autoneg;
  839. if (params.pause.autoneg)
  840. if_link->pause_config |= QED_LINK_PAUSE_AUTONEG_ENABLE;
  841. if (params.pause.forced_rx)
  842. if_link->pause_config |= QED_LINK_PAUSE_RX_ENABLE;
  843. if (params.pause.forced_tx)
  844. if_link->pause_config |= QED_LINK_PAUSE_TX_ENABLE;
  845. /* Link partner capabilities */
  846. if (link.partner_adv_speed &
  847. QED_LINK_PARTNER_SPEED_1G_HD)
  848. if_link->lp_caps |= SUPPORTED_1000baseT_Half;
  849. if (link.partner_adv_speed &
  850. QED_LINK_PARTNER_SPEED_1G_FD)
  851. if_link->lp_caps |= SUPPORTED_1000baseT_Full;
  852. if (link.partner_adv_speed &
  853. QED_LINK_PARTNER_SPEED_10G)
  854. if_link->lp_caps |= SUPPORTED_10000baseKR_Full;
  855. if (link.partner_adv_speed &
  856. QED_LINK_PARTNER_SPEED_40G)
  857. if_link->lp_caps |= SUPPORTED_40000baseLR4_Full;
  858. if (link.partner_adv_speed &
  859. QED_LINK_PARTNER_SPEED_50G)
  860. if_link->lp_caps |= 0;
  861. if (link.partner_adv_speed &
  862. QED_LINK_PARTNER_SPEED_100G)
  863. if_link->lp_caps |= 0;
  864. if (link.an_complete)
  865. if_link->lp_caps |= SUPPORTED_Autoneg;
  866. if (link.partner_adv_pause)
  867. if_link->lp_caps |= SUPPORTED_Pause;
  868. if (link.partner_adv_pause == QED_LINK_PARTNER_ASYMMETRIC_PAUSE ||
  869. link.partner_adv_pause == QED_LINK_PARTNER_BOTH_PAUSE)
  870. if_link->lp_caps |= SUPPORTED_Asym_Pause;
  871. }
  872. static void qed_get_current_link(struct qed_dev *cdev,
  873. struct qed_link_output *if_link)
  874. {
  875. qed_fill_link(&cdev->hwfns[0], if_link);
  876. }
  877. void qed_link_update(struct qed_hwfn *hwfn)
  878. {
  879. void *cookie = hwfn->cdev->ops_cookie;
  880. struct qed_common_cb_ops *op = hwfn->cdev->protocol_ops.common;
  881. struct qed_link_output if_link;
  882. qed_fill_link(hwfn, &if_link);
  883. if (IS_LEAD_HWFN(hwfn) && cookie)
  884. op->link_update(cookie, &if_link);
  885. }
  886. static int qed_drain(struct qed_dev *cdev)
  887. {
  888. struct qed_hwfn *hwfn;
  889. struct qed_ptt *ptt;
  890. int i, rc;
  891. for_each_hwfn(cdev, i) {
  892. hwfn = &cdev->hwfns[i];
  893. ptt = qed_ptt_acquire(hwfn);
  894. if (!ptt) {
  895. DP_NOTICE(hwfn, "Failed to drain NIG; No PTT\n");
  896. return -EBUSY;
  897. }
  898. rc = qed_mcp_drain(hwfn, ptt);
  899. if (rc)
  900. return rc;
  901. qed_ptt_release(hwfn, ptt);
  902. }
  903. return 0;
  904. }
  905. static int qed_set_led(struct qed_dev *cdev, enum qed_led_mode mode)
  906. {
  907. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  908. struct qed_ptt *ptt;
  909. int status = 0;
  910. ptt = qed_ptt_acquire(hwfn);
  911. if (!ptt)
  912. return -EAGAIN;
  913. status = qed_mcp_set_led(hwfn, ptt, mode);
  914. qed_ptt_release(hwfn, ptt);
  915. return status;
  916. }
  917. const struct qed_common_ops qed_common_ops_pass = {
  918. .probe = &qed_probe,
  919. .remove = &qed_remove,
  920. .set_power_state = &qed_set_power_state,
  921. .set_id = &qed_set_id,
  922. .update_pf_params = &qed_update_pf_params,
  923. .slowpath_start = &qed_slowpath_start,
  924. .slowpath_stop = &qed_slowpath_stop,
  925. .set_fp_int = &qed_set_int_fp,
  926. .get_fp_int = &qed_get_int_fp,
  927. .sb_init = &qed_sb_init,
  928. .sb_release = &qed_sb_release,
  929. .simd_handler_config = &qed_simd_handler_config,
  930. .simd_handler_clean = &qed_simd_handler_clean,
  931. .set_link = &qed_set_link,
  932. .get_link = &qed_get_current_link,
  933. .drain = &qed_drain,
  934. .update_msglvl = &qed_init_dp,
  935. .chain_alloc = &qed_chain_alloc,
  936. .chain_free = &qed_chain_free,
  937. .set_led = &qed_set_led,
  938. };
  939. u32 qed_get_protocol_version(enum qed_protocol protocol)
  940. {
  941. switch (protocol) {
  942. case QED_PROTOCOL_ETH:
  943. return QED_ETH_INTERFACE_VERSION;
  944. default:
  945. return 0;
  946. }
  947. }
  948. EXPORT_SYMBOL(qed_get_protocol_version);