qed_main.c 28 KB

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