qed_main.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. /* QLogic qed NIC Driver
  2. * Copyright (c) 2015-2017 QLogic Corporation
  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/stddef.h>
  33. #include <linux/pci.h>
  34. #include <linux/kernel.h>
  35. #include <linux/slab.h>
  36. #include <linux/version.h>
  37. #include <linux/delay.h>
  38. #include <asm/byteorder.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/string.h>
  41. #include <linux/module.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/workqueue.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/etherdevice.h>
  46. #include <linux/vmalloc.h>
  47. #include <linux/qed/qed_if.h>
  48. #include <linux/qed/qed_ll2_if.h>
  49. #include "qed.h"
  50. #include "qed_sriov.h"
  51. #include "qed_sp.h"
  52. #include "qed_dev_api.h"
  53. #include "qed_ll2.h"
  54. #include "qed_fcoe.h"
  55. #include "qed_mcp.h"
  56. #include "qed_hw.h"
  57. #include "qed_selftest.h"
  58. #include "qed_debug.h"
  59. #define QED_ROCE_QPS (8192)
  60. #define QED_ROCE_DPIS (8)
  61. static char version[] =
  62. "QLogic FastLinQ 4xxxx Core Module qed " DRV_MODULE_VERSION "\n";
  63. MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx Core Module");
  64. MODULE_LICENSE("GPL");
  65. MODULE_VERSION(DRV_MODULE_VERSION);
  66. #define FW_FILE_VERSION \
  67. __stringify(FW_MAJOR_VERSION) "." \
  68. __stringify(FW_MINOR_VERSION) "." \
  69. __stringify(FW_REVISION_VERSION) "." \
  70. __stringify(FW_ENGINEERING_VERSION)
  71. #define QED_FW_FILE_NAME \
  72. "qed/qed_init_values_zipped-" FW_FILE_VERSION ".bin"
  73. MODULE_FIRMWARE(QED_FW_FILE_NAME);
  74. static int __init qed_init(void)
  75. {
  76. pr_info("%s", version);
  77. return 0;
  78. }
  79. static void __exit qed_cleanup(void)
  80. {
  81. pr_notice("qed_cleanup called\n");
  82. }
  83. module_init(qed_init);
  84. module_exit(qed_cleanup);
  85. /* Check if the DMA controller on the machine can properly handle the DMA
  86. * addressing required by the device.
  87. */
  88. static int qed_set_coherency_mask(struct qed_dev *cdev)
  89. {
  90. struct device *dev = &cdev->pdev->dev;
  91. if (dma_set_mask(dev, DMA_BIT_MASK(64)) == 0) {
  92. if (dma_set_coherent_mask(dev, DMA_BIT_MASK(64)) != 0) {
  93. DP_NOTICE(cdev,
  94. "Can't request 64-bit consistent allocations\n");
  95. return -EIO;
  96. }
  97. } else if (dma_set_mask(dev, DMA_BIT_MASK(32)) != 0) {
  98. DP_NOTICE(cdev, "Can't request 64b/32b DMA addresses\n");
  99. return -EIO;
  100. }
  101. return 0;
  102. }
  103. static void qed_free_pci(struct qed_dev *cdev)
  104. {
  105. struct pci_dev *pdev = cdev->pdev;
  106. if (cdev->doorbells)
  107. iounmap(cdev->doorbells);
  108. if (cdev->regview)
  109. iounmap(cdev->regview);
  110. if (atomic_read(&pdev->enable_cnt) == 1)
  111. pci_release_regions(pdev);
  112. pci_disable_device(pdev);
  113. }
  114. #define PCI_REVISION_ID_ERROR_VAL 0xff
  115. /* Performs PCI initializations as well as initializing PCI-related parameters
  116. * in the device structrue. Returns 0 in case of success.
  117. */
  118. static int qed_init_pci(struct qed_dev *cdev, struct pci_dev *pdev)
  119. {
  120. u8 rev_id;
  121. int rc;
  122. cdev->pdev = pdev;
  123. rc = pci_enable_device(pdev);
  124. if (rc) {
  125. DP_NOTICE(cdev, "Cannot enable PCI device\n");
  126. goto err0;
  127. }
  128. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  129. DP_NOTICE(cdev, "No memory region found in bar #0\n");
  130. rc = -EIO;
  131. goto err1;
  132. }
  133. if (IS_PF(cdev) && !(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  134. DP_NOTICE(cdev, "No memory region found in bar #2\n");
  135. rc = -EIO;
  136. goto err1;
  137. }
  138. if (atomic_read(&pdev->enable_cnt) == 1) {
  139. rc = pci_request_regions(pdev, "qed");
  140. if (rc) {
  141. DP_NOTICE(cdev,
  142. "Failed to request PCI memory resources\n");
  143. goto err1;
  144. }
  145. pci_set_master(pdev);
  146. pci_save_state(pdev);
  147. }
  148. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  149. if (rev_id == PCI_REVISION_ID_ERROR_VAL) {
  150. DP_NOTICE(cdev,
  151. "Detected PCI device error [rev_id 0x%x]. Probably due to prior indication. Aborting.\n",
  152. rev_id);
  153. rc = -ENODEV;
  154. goto err2;
  155. }
  156. if (!pci_is_pcie(pdev)) {
  157. DP_NOTICE(cdev, "The bus is not PCI Express\n");
  158. rc = -EIO;
  159. goto err2;
  160. }
  161. cdev->pci_params.pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  162. if (IS_PF(cdev) && !cdev->pci_params.pm_cap)
  163. DP_NOTICE(cdev, "Cannot find power management capability\n");
  164. rc = qed_set_coherency_mask(cdev);
  165. if (rc)
  166. goto err2;
  167. cdev->pci_params.mem_start = pci_resource_start(pdev, 0);
  168. cdev->pci_params.mem_end = pci_resource_end(pdev, 0);
  169. cdev->pci_params.irq = pdev->irq;
  170. cdev->regview = pci_ioremap_bar(pdev, 0);
  171. if (!cdev->regview) {
  172. DP_NOTICE(cdev, "Cannot map register space, aborting\n");
  173. rc = -ENOMEM;
  174. goto err2;
  175. }
  176. if (IS_PF(cdev)) {
  177. cdev->db_phys_addr = pci_resource_start(cdev->pdev, 2);
  178. cdev->db_size = pci_resource_len(cdev->pdev, 2);
  179. cdev->doorbells = ioremap_wc(cdev->db_phys_addr, cdev->db_size);
  180. if (!cdev->doorbells) {
  181. DP_NOTICE(cdev, "Cannot map doorbell space\n");
  182. return -ENOMEM;
  183. }
  184. }
  185. return 0;
  186. err2:
  187. pci_release_regions(pdev);
  188. err1:
  189. pci_disable_device(pdev);
  190. err0:
  191. return rc;
  192. }
  193. int qed_fill_dev_info(struct qed_dev *cdev,
  194. struct qed_dev_info *dev_info)
  195. {
  196. struct qed_ptt *ptt;
  197. memset(dev_info, 0, sizeof(struct qed_dev_info));
  198. dev_info->num_hwfns = cdev->num_hwfns;
  199. dev_info->pci_mem_start = cdev->pci_params.mem_start;
  200. dev_info->pci_mem_end = cdev->pci_params.mem_end;
  201. dev_info->pci_irq = cdev->pci_params.irq;
  202. dev_info->rdma_supported = (cdev->hwfns[0].hw_info.personality ==
  203. QED_PCI_ETH_ROCE);
  204. dev_info->is_mf_default = IS_MF_DEFAULT(&cdev->hwfns[0]);
  205. ether_addr_copy(dev_info->hw_mac, cdev->hwfns[0].hw_info.hw_mac_addr);
  206. if (IS_PF(cdev)) {
  207. dev_info->fw_major = FW_MAJOR_VERSION;
  208. dev_info->fw_minor = FW_MINOR_VERSION;
  209. dev_info->fw_rev = FW_REVISION_VERSION;
  210. dev_info->fw_eng = FW_ENGINEERING_VERSION;
  211. dev_info->mf_mode = cdev->mf_mode;
  212. dev_info->tx_switching = true;
  213. if (QED_LEADING_HWFN(cdev)->hw_info.b_wol_support ==
  214. QED_WOL_SUPPORT_PME)
  215. dev_info->wol_support = true;
  216. } else {
  217. qed_vf_get_fw_version(&cdev->hwfns[0], &dev_info->fw_major,
  218. &dev_info->fw_minor, &dev_info->fw_rev,
  219. &dev_info->fw_eng);
  220. }
  221. if (IS_PF(cdev)) {
  222. ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
  223. if (ptt) {
  224. qed_mcp_get_mfw_ver(QED_LEADING_HWFN(cdev), ptt,
  225. &dev_info->mfw_rev, NULL);
  226. qed_mcp_get_flash_size(QED_LEADING_HWFN(cdev), ptt,
  227. &dev_info->flash_size);
  228. qed_ptt_release(QED_LEADING_HWFN(cdev), ptt);
  229. }
  230. } else {
  231. qed_mcp_get_mfw_ver(QED_LEADING_HWFN(cdev), NULL,
  232. &dev_info->mfw_rev, NULL);
  233. }
  234. dev_info->mtu = QED_LEADING_HWFN(cdev)->hw_info.mtu;
  235. return 0;
  236. }
  237. static void qed_free_cdev(struct qed_dev *cdev)
  238. {
  239. kfree((void *)cdev);
  240. }
  241. static struct qed_dev *qed_alloc_cdev(struct pci_dev *pdev)
  242. {
  243. struct qed_dev *cdev;
  244. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  245. if (!cdev)
  246. return cdev;
  247. qed_init_struct(cdev);
  248. return cdev;
  249. }
  250. /* Sets the requested power state */
  251. static int qed_set_power_state(struct qed_dev *cdev, pci_power_t state)
  252. {
  253. if (!cdev)
  254. return -ENODEV;
  255. DP_VERBOSE(cdev, NETIF_MSG_DRV, "Omitting Power state change\n");
  256. return 0;
  257. }
  258. /* probing */
  259. static struct qed_dev *qed_probe(struct pci_dev *pdev,
  260. struct qed_probe_params *params)
  261. {
  262. struct qed_dev *cdev;
  263. int rc;
  264. cdev = qed_alloc_cdev(pdev);
  265. if (!cdev)
  266. goto err0;
  267. cdev->protocol = params->protocol;
  268. if (params->is_vf)
  269. cdev->b_is_vf = true;
  270. qed_init_dp(cdev, params->dp_module, params->dp_level);
  271. rc = qed_init_pci(cdev, pdev);
  272. if (rc) {
  273. DP_ERR(cdev, "init pci failed\n");
  274. goto err1;
  275. }
  276. DP_INFO(cdev, "PCI init completed successfully\n");
  277. rc = qed_hw_prepare(cdev, QED_PCI_DEFAULT);
  278. if (rc) {
  279. DP_ERR(cdev, "hw prepare failed\n");
  280. goto err2;
  281. }
  282. DP_INFO(cdev, "qed_probe completed successffuly\n");
  283. return cdev;
  284. err2:
  285. qed_free_pci(cdev);
  286. err1:
  287. qed_free_cdev(cdev);
  288. err0:
  289. return NULL;
  290. }
  291. static void qed_remove(struct qed_dev *cdev)
  292. {
  293. if (!cdev)
  294. return;
  295. qed_hw_remove(cdev);
  296. qed_free_pci(cdev);
  297. qed_set_power_state(cdev, PCI_D3hot);
  298. qed_free_cdev(cdev);
  299. }
  300. static void qed_disable_msix(struct qed_dev *cdev)
  301. {
  302. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  303. pci_disable_msix(cdev->pdev);
  304. kfree(cdev->int_params.msix_table);
  305. } else if (cdev->int_params.out.int_mode == QED_INT_MODE_MSI) {
  306. pci_disable_msi(cdev->pdev);
  307. }
  308. memset(&cdev->int_params.out, 0, sizeof(struct qed_int_param));
  309. }
  310. static int qed_enable_msix(struct qed_dev *cdev,
  311. struct qed_int_params *int_params)
  312. {
  313. int i, rc, cnt;
  314. cnt = int_params->in.num_vectors;
  315. for (i = 0; i < cnt; i++)
  316. int_params->msix_table[i].entry = i;
  317. rc = pci_enable_msix_range(cdev->pdev, int_params->msix_table,
  318. int_params->in.min_msix_cnt, cnt);
  319. if (rc < cnt && rc >= int_params->in.min_msix_cnt &&
  320. (rc % cdev->num_hwfns)) {
  321. pci_disable_msix(cdev->pdev);
  322. /* If fastpath is initialized, we need at least one interrupt
  323. * per hwfn [and the slow path interrupts]. New requested number
  324. * should be a multiple of the number of hwfns.
  325. */
  326. cnt = (rc / cdev->num_hwfns) * cdev->num_hwfns;
  327. DP_NOTICE(cdev,
  328. "Trying to enable MSI-X with less vectors (%d out of %d)\n",
  329. cnt, int_params->in.num_vectors);
  330. rc = pci_enable_msix_exact(cdev->pdev, int_params->msix_table,
  331. cnt);
  332. if (!rc)
  333. rc = cnt;
  334. }
  335. if (rc > 0) {
  336. /* MSI-x configuration was achieved */
  337. int_params->out.int_mode = QED_INT_MODE_MSIX;
  338. int_params->out.num_vectors = rc;
  339. rc = 0;
  340. } else {
  341. DP_NOTICE(cdev,
  342. "Failed to enable MSI-X [Requested %d vectors][rc %d]\n",
  343. cnt, rc);
  344. }
  345. return rc;
  346. }
  347. /* This function outputs the int mode and the number of enabled msix vector */
  348. static int qed_set_int_mode(struct qed_dev *cdev, bool force_mode)
  349. {
  350. struct qed_int_params *int_params = &cdev->int_params;
  351. struct msix_entry *tbl;
  352. int rc = 0, cnt;
  353. switch (int_params->in.int_mode) {
  354. case QED_INT_MODE_MSIX:
  355. /* Allocate MSIX table */
  356. cnt = int_params->in.num_vectors;
  357. int_params->msix_table = kcalloc(cnt, sizeof(*tbl), GFP_KERNEL);
  358. if (!int_params->msix_table) {
  359. rc = -ENOMEM;
  360. goto out;
  361. }
  362. /* Enable MSIX */
  363. rc = qed_enable_msix(cdev, int_params);
  364. if (!rc)
  365. goto out;
  366. DP_NOTICE(cdev, "Failed to enable MSI-X\n");
  367. kfree(int_params->msix_table);
  368. if (force_mode)
  369. goto out;
  370. /* Fallthrough */
  371. case QED_INT_MODE_MSI:
  372. if (cdev->num_hwfns == 1) {
  373. rc = pci_enable_msi(cdev->pdev);
  374. if (!rc) {
  375. int_params->out.int_mode = QED_INT_MODE_MSI;
  376. goto out;
  377. }
  378. DP_NOTICE(cdev, "Failed to enable MSI\n");
  379. if (force_mode)
  380. goto out;
  381. }
  382. /* Fallthrough */
  383. case QED_INT_MODE_INTA:
  384. int_params->out.int_mode = QED_INT_MODE_INTA;
  385. rc = 0;
  386. goto out;
  387. default:
  388. DP_NOTICE(cdev, "Unknown int_mode value %d\n",
  389. int_params->in.int_mode);
  390. rc = -EINVAL;
  391. }
  392. out:
  393. if (!rc)
  394. DP_INFO(cdev, "Using %s interrupts\n",
  395. int_params->out.int_mode == QED_INT_MODE_INTA ?
  396. "INTa" : int_params->out.int_mode == QED_INT_MODE_MSI ?
  397. "MSI" : "MSIX");
  398. cdev->int_coalescing_mode = QED_COAL_MODE_ENABLE;
  399. return rc;
  400. }
  401. static void qed_simd_handler_config(struct qed_dev *cdev, void *token,
  402. int index, void(*handler)(void *))
  403. {
  404. struct qed_hwfn *hwfn = &cdev->hwfns[index % cdev->num_hwfns];
  405. int relative_idx = index / cdev->num_hwfns;
  406. hwfn->simd_proto_handler[relative_idx].func = handler;
  407. hwfn->simd_proto_handler[relative_idx].token = token;
  408. }
  409. static void qed_simd_handler_clean(struct qed_dev *cdev, int index)
  410. {
  411. struct qed_hwfn *hwfn = &cdev->hwfns[index % cdev->num_hwfns];
  412. int relative_idx = index / cdev->num_hwfns;
  413. memset(&hwfn->simd_proto_handler[relative_idx], 0,
  414. sizeof(struct qed_simd_fp_handler));
  415. }
  416. static irqreturn_t qed_msix_sp_int(int irq, void *tasklet)
  417. {
  418. tasklet_schedule((struct tasklet_struct *)tasklet);
  419. return IRQ_HANDLED;
  420. }
  421. static irqreturn_t qed_single_int(int irq, void *dev_instance)
  422. {
  423. struct qed_dev *cdev = (struct qed_dev *)dev_instance;
  424. struct qed_hwfn *hwfn;
  425. irqreturn_t rc = IRQ_NONE;
  426. u64 status;
  427. int i, j;
  428. for (i = 0; i < cdev->num_hwfns; i++) {
  429. status = qed_int_igu_read_sisr_reg(&cdev->hwfns[i]);
  430. if (!status)
  431. continue;
  432. hwfn = &cdev->hwfns[i];
  433. /* Slowpath interrupt */
  434. if (unlikely(status & 0x1)) {
  435. tasklet_schedule(hwfn->sp_dpc);
  436. status &= ~0x1;
  437. rc = IRQ_HANDLED;
  438. }
  439. /* Fastpath interrupts */
  440. for (j = 0; j < 64; j++) {
  441. if ((0x2ULL << j) & status) {
  442. hwfn->simd_proto_handler[j].func(
  443. hwfn->simd_proto_handler[j].token);
  444. status &= ~(0x2ULL << j);
  445. rc = IRQ_HANDLED;
  446. }
  447. }
  448. if (unlikely(status))
  449. DP_VERBOSE(hwfn, NETIF_MSG_INTR,
  450. "got an unknown interrupt status 0x%llx\n",
  451. status);
  452. }
  453. return rc;
  454. }
  455. int qed_slowpath_irq_req(struct qed_hwfn *hwfn)
  456. {
  457. struct qed_dev *cdev = hwfn->cdev;
  458. u32 int_mode;
  459. int rc = 0;
  460. u8 id;
  461. int_mode = cdev->int_params.out.int_mode;
  462. if (int_mode == QED_INT_MODE_MSIX) {
  463. id = hwfn->my_id;
  464. snprintf(hwfn->name, NAME_SIZE, "sp-%d-%02x:%02x.%02x",
  465. id, cdev->pdev->bus->number,
  466. PCI_SLOT(cdev->pdev->devfn), hwfn->abs_pf_id);
  467. rc = request_irq(cdev->int_params.msix_table[id].vector,
  468. qed_msix_sp_int, 0, hwfn->name, hwfn->sp_dpc);
  469. } else {
  470. unsigned long flags = 0;
  471. snprintf(cdev->name, NAME_SIZE, "%02x:%02x.%02x",
  472. cdev->pdev->bus->number, PCI_SLOT(cdev->pdev->devfn),
  473. PCI_FUNC(cdev->pdev->devfn));
  474. if (cdev->int_params.out.int_mode == QED_INT_MODE_INTA)
  475. flags |= IRQF_SHARED;
  476. rc = request_irq(cdev->pdev->irq, qed_single_int,
  477. flags, cdev->name, cdev);
  478. }
  479. if (rc)
  480. DP_NOTICE(cdev, "request_irq failed, rc = %d\n", rc);
  481. else
  482. DP_VERBOSE(hwfn, (NETIF_MSG_INTR | QED_MSG_SP),
  483. "Requested slowpath %s\n",
  484. (int_mode == QED_INT_MODE_MSIX) ? "MSI-X" : "IRQ");
  485. return rc;
  486. }
  487. static void qed_slowpath_irq_free(struct qed_dev *cdev)
  488. {
  489. int i;
  490. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  491. for_each_hwfn(cdev, i) {
  492. if (!cdev->hwfns[i].b_int_requested)
  493. break;
  494. synchronize_irq(cdev->int_params.msix_table[i].vector);
  495. free_irq(cdev->int_params.msix_table[i].vector,
  496. cdev->hwfns[i].sp_dpc);
  497. }
  498. } else {
  499. if (QED_LEADING_HWFN(cdev)->b_int_requested)
  500. free_irq(cdev->pdev->irq, cdev);
  501. }
  502. qed_int_disable_post_isr_release(cdev);
  503. }
  504. static int qed_nic_stop(struct qed_dev *cdev)
  505. {
  506. int i, rc;
  507. rc = qed_hw_stop(cdev);
  508. for (i = 0; i < cdev->num_hwfns; i++) {
  509. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  510. if (p_hwfn->b_sp_dpc_enabled) {
  511. tasklet_disable(p_hwfn->sp_dpc);
  512. p_hwfn->b_sp_dpc_enabled = false;
  513. DP_VERBOSE(cdev, NETIF_MSG_IFDOWN,
  514. "Disabled sp taskelt [hwfn %d] at %p\n",
  515. i, p_hwfn->sp_dpc);
  516. }
  517. }
  518. qed_dbg_pf_exit(cdev);
  519. return rc;
  520. }
  521. static int qed_nic_reset(struct qed_dev *cdev)
  522. {
  523. int rc;
  524. rc = qed_hw_reset(cdev);
  525. if (rc)
  526. return rc;
  527. qed_resc_free(cdev);
  528. return 0;
  529. }
  530. static int qed_nic_setup(struct qed_dev *cdev)
  531. {
  532. int rc, i;
  533. /* Determine if interface is going to require LL2 */
  534. if (QED_LEADING_HWFN(cdev)->hw_info.personality != QED_PCI_ETH) {
  535. for (i = 0; i < cdev->num_hwfns; i++) {
  536. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  537. p_hwfn->using_ll2 = true;
  538. }
  539. }
  540. rc = qed_resc_alloc(cdev);
  541. if (rc)
  542. return rc;
  543. DP_INFO(cdev, "Allocated qed resources\n");
  544. qed_resc_setup(cdev);
  545. return rc;
  546. }
  547. static int qed_set_int_fp(struct qed_dev *cdev, u16 cnt)
  548. {
  549. int limit = 0;
  550. /* Mark the fastpath as free/used */
  551. cdev->int_params.fp_initialized = cnt ? true : false;
  552. if (cdev->int_params.out.int_mode != QED_INT_MODE_MSIX)
  553. limit = cdev->num_hwfns * 63;
  554. else if (cdev->int_params.fp_msix_cnt)
  555. limit = cdev->int_params.fp_msix_cnt;
  556. if (!limit)
  557. return -ENOMEM;
  558. return min_t(int, cnt, limit);
  559. }
  560. static int qed_get_int_fp(struct qed_dev *cdev, struct qed_int_info *info)
  561. {
  562. memset(info, 0, sizeof(struct qed_int_info));
  563. if (!cdev->int_params.fp_initialized) {
  564. DP_INFO(cdev,
  565. "Protocol driver requested interrupt information, but its support is not yet configured\n");
  566. return -EINVAL;
  567. }
  568. /* Need to expose only MSI-X information; Single IRQ is handled solely
  569. * by qed.
  570. */
  571. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  572. int msix_base = cdev->int_params.fp_msix_base;
  573. info->msix_cnt = cdev->int_params.fp_msix_cnt;
  574. info->msix = &cdev->int_params.msix_table[msix_base];
  575. }
  576. return 0;
  577. }
  578. static int qed_slowpath_setup_int(struct qed_dev *cdev,
  579. enum qed_int_mode int_mode)
  580. {
  581. struct qed_sb_cnt_info sb_cnt_info;
  582. int num_l2_queues = 0;
  583. int rc;
  584. int i;
  585. if ((int_mode == QED_INT_MODE_MSI) && (cdev->num_hwfns > 1)) {
  586. DP_NOTICE(cdev, "MSI mode is not supported for CMT devices\n");
  587. return -EINVAL;
  588. }
  589. memset(&cdev->int_params, 0, sizeof(struct qed_int_params));
  590. cdev->int_params.in.int_mode = int_mode;
  591. for_each_hwfn(cdev, i) {
  592. memset(&sb_cnt_info, 0, sizeof(sb_cnt_info));
  593. qed_int_get_num_sbs(&cdev->hwfns[i], &sb_cnt_info);
  594. cdev->int_params.in.num_vectors += sb_cnt_info.sb_cnt;
  595. cdev->int_params.in.num_vectors++; /* slowpath */
  596. }
  597. /* We want a minimum of one slowpath and one fastpath vector per hwfn */
  598. cdev->int_params.in.min_msix_cnt = cdev->num_hwfns * 2;
  599. rc = qed_set_int_mode(cdev, false);
  600. if (rc) {
  601. DP_ERR(cdev, "qed_slowpath_setup_int ERR\n");
  602. return rc;
  603. }
  604. cdev->int_params.fp_msix_base = cdev->num_hwfns;
  605. cdev->int_params.fp_msix_cnt = cdev->int_params.out.num_vectors -
  606. cdev->num_hwfns;
  607. if (!IS_ENABLED(CONFIG_QED_RDMA))
  608. return 0;
  609. for_each_hwfn(cdev, i)
  610. num_l2_queues += FEAT_NUM(&cdev->hwfns[i], QED_PF_L2_QUE);
  611. DP_VERBOSE(cdev, QED_MSG_RDMA,
  612. "cdev->int_params.fp_msix_cnt=%d num_l2_queues=%d\n",
  613. cdev->int_params.fp_msix_cnt, num_l2_queues);
  614. if (cdev->int_params.fp_msix_cnt > num_l2_queues) {
  615. cdev->int_params.rdma_msix_cnt =
  616. (cdev->int_params.fp_msix_cnt - num_l2_queues)
  617. / cdev->num_hwfns;
  618. cdev->int_params.rdma_msix_base =
  619. cdev->int_params.fp_msix_base + num_l2_queues;
  620. cdev->int_params.fp_msix_cnt = num_l2_queues;
  621. } else {
  622. cdev->int_params.rdma_msix_cnt = 0;
  623. }
  624. DP_VERBOSE(cdev, QED_MSG_RDMA, "roce_msix_cnt=%d roce_msix_base=%d\n",
  625. cdev->int_params.rdma_msix_cnt,
  626. cdev->int_params.rdma_msix_base);
  627. return 0;
  628. }
  629. static int qed_slowpath_vf_setup_int(struct qed_dev *cdev)
  630. {
  631. int rc;
  632. memset(&cdev->int_params, 0, sizeof(struct qed_int_params));
  633. cdev->int_params.in.int_mode = QED_INT_MODE_MSIX;
  634. qed_vf_get_num_rxqs(QED_LEADING_HWFN(cdev),
  635. &cdev->int_params.in.num_vectors);
  636. if (cdev->num_hwfns > 1) {
  637. u8 vectors = 0;
  638. qed_vf_get_num_rxqs(&cdev->hwfns[1], &vectors);
  639. cdev->int_params.in.num_vectors += vectors;
  640. }
  641. /* We want a minimum of one fastpath vector per vf hwfn */
  642. cdev->int_params.in.min_msix_cnt = cdev->num_hwfns;
  643. rc = qed_set_int_mode(cdev, true);
  644. if (rc)
  645. return rc;
  646. cdev->int_params.fp_msix_base = 0;
  647. cdev->int_params.fp_msix_cnt = cdev->int_params.out.num_vectors;
  648. return 0;
  649. }
  650. u32 qed_unzip_data(struct qed_hwfn *p_hwfn, u32 input_len,
  651. u8 *input_buf, u32 max_size, u8 *unzip_buf)
  652. {
  653. int rc;
  654. p_hwfn->stream->next_in = input_buf;
  655. p_hwfn->stream->avail_in = input_len;
  656. p_hwfn->stream->next_out = unzip_buf;
  657. p_hwfn->stream->avail_out = max_size;
  658. rc = zlib_inflateInit2(p_hwfn->stream, MAX_WBITS);
  659. if (rc != Z_OK) {
  660. DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "zlib init failed, rc = %d\n",
  661. rc);
  662. return 0;
  663. }
  664. rc = zlib_inflate(p_hwfn->stream, Z_FINISH);
  665. zlib_inflateEnd(p_hwfn->stream);
  666. if (rc != Z_OK && rc != Z_STREAM_END) {
  667. DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "FW unzip error: %s, rc=%d\n",
  668. p_hwfn->stream->msg, rc);
  669. return 0;
  670. }
  671. return p_hwfn->stream->total_out / 4;
  672. }
  673. static int qed_alloc_stream_mem(struct qed_dev *cdev)
  674. {
  675. int i;
  676. void *workspace;
  677. for_each_hwfn(cdev, i) {
  678. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  679. p_hwfn->stream = kzalloc(sizeof(*p_hwfn->stream), GFP_KERNEL);
  680. if (!p_hwfn->stream)
  681. return -ENOMEM;
  682. workspace = vzalloc(zlib_inflate_workspacesize());
  683. if (!workspace)
  684. return -ENOMEM;
  685. p_hwfn->stream->workspace = workspace;
  686. }
  687. return 0;
  688. }
  689. static void qed_free_stream_mem(struct qed_dev *cdev)
  690. {
  691. int i;
  692. for_each_hwfn(cdev, i) {
  693. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  694. if (!p_hwfn->stream)
  695. return;
  696. vfree(p_hwfn->stream->workspace);
  697. kfree(p_hwfn->stream);
  698. }
  699. }
  700. static void qed_update_pf_params(struct qed_dev *cdev,
  701. struct qed_pf_params *params)
  702. {
  703. int i;
  704. if (IS_ENABLED(CONFIG_QED_RDMA)) {
  705. params->rdma_pf_params.num_qps = QED_ROCE_QPS;
  706. params->rdma_pf_params.min_dpis = QED_ROCE_DPIS;
  707. /* divide by 3 the MRs to avoid MF ILT overflow */
  708. params->rdma_pf_params.num_mrs = RDMA_MAX_TIDS;
  709. params->rdma_pf_params.gl_pi = QED_ROCE_PROTOCOL_INDEX;
  710. }
  711. /* In case we might support RDMA, don't allow qede to be greedy
  712. * with the L2 contexts. Allow for 64 queues [rx, tx, xdp] per hwfn.
  713. */
  714. if (QED_LEADING_HWFN(cdev)->hw_info.personality ==
  715. QED_PCI_ETH_ROCE) {
  716. u16 *num_cons;
  717. num_cons = &params->eth_pf_params.num_cons;
  718. *num_cons = min_t(u16, *num_cons, 192);
  719. }
  720. for (i = 0; i < cdev->num_hwfns; i++) {
  721. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  722. p_hwfn->pf_params = *params;
  723. }
  724. }
  725. static int qed_slowpath_start(struct qed_dev *cdev,
  726. struct qed_slowpath_params *params)
  727. {
  728. struct qed_tunn_start_params tunn_info;
  729. struct qed_mcp_drv_version drv_version;
  730. const u8 *data = NULL;
  731. struct qed_hwfn *hwfn;
  732. struct qed_ptt *p_ptt;
  733. int rc = -EINVAL;
  734. if (qed_iov_wq_start(cdev))
  735. goto err;
  736. if (IS_PF(cdev)) {
  737. rc = request_firmware(&cdev->firmware, QED_FW_FILE_NAME,
  738. &cdev->pdev->dev);
  739. if (rc) {
  740. DP_NOTICE(cdev,
  741. "Failed to find fw file - /lib/firmware/%s\n",
  742. QED_FW_FILE_NAME);
  743. goto err;
  744. }
  745. p_ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
  746. if (p_ptt) {
  747. QED_LEADING_HWFN(cdev)->p_ptp_ptt = p_ptt;
  748. } else {
  749. DP_NOTICE(cdev, "Failed to acquire PTT for PTP\n");
  750. goto err;
  751. }
  752. }
  753. cdev->rx_coalesce_usecs = QED_DEFAULT_RX_USECS;
  754. rc = qed_nic_setup(cdev);
  755. if (rc)
  756. goto err;
  757. if (IS_PF(cdev))
  758. rc = qed_slowpath_setup_int(cdev, params->int_mode);
  759. else
  760. rc = qed_slowpath_vf_setup_int(cdev);
  761. if (rc)
  762. goto err1;
  763. if (IS_PF(cdev)) {
  764. /* Allocate stream for unzipping */
  765. rc = qed_alloc_stream_mem(cdev);
  766. if (rc)
  767. goto err2;
  768. /* First Dword used to diffrentiate between various sources */
  769. data = cdev->firmware->data + sizeof(u32);
  770. qed_dbg_pf_init(cdev);
  771. }
  772. memset(&tunn_info, 0, sizeof(tunn_info));
  773. tunn_info.tunn_mode |= 1 << QED_MODE_VXLAN_TUNN |
  774. 1 << QED_MODE_L2GRE_TUNN |
  775. 1 << QED_MODE_IPGRE_TUNN |
  776. 1 << QED_MODE_L2GENEVE_TUNN |
  777. 1 << QED_MODE_IPGENEVE_TUNN;
  778. tunn_info.tunn_clss_vxlan = QED_TUNN_CLSS_MAC_VLAN;
  779. tunn_info.tunn_clss_l2gre = QED_TUNN_CLSS_MAC_VLAN;
  780. tunn_info.tunn_clss_ipgre = QED_TUNN_CLSS_MAC_VLAN;
  781. /* Start the slowpath */
  782. rc = qed_hw_init(cdev, &tunn_info, true,
  783. cdev->int_params.out.int_mode,
  784. true, data);
  785. if (rc)
  786. goto err2;
  787. DP_INFO(cdev,
  788. "HW initialization and function start completed successfully\n");
  789. /* Allocate LL2 interface if needed */
  790. if (QED_LEADING_HWFN(cdev)->using_ll2) {
  791. rc = qed_ll2_alloc_if(cdev);
  792. if (rc)
  793. goto err3;
  794. }
  795. if (IS_PF(cdev)) {
  796. hwfn = QED_LEADING_HWFN(cdev);
  797. drv_version.version = (params->drv_major << 24) |
  798. (params->drv_minor << 16) |
  799. (params->drv_rev << 8) |
  800. (params->drv_eng);
  801. strlcpy(drv_version.name, params->name,
  802. MCP_DRV_VER_STR_SIZE - 4);
  803. rc = qed_mcp_send_drv_version(hwfn, hwfn->p_main_ptt,
  804. &drv_version);
  805. if (rc) {
  806. DP_NOTICE(cdev, "Failed sending drv version command\n");
  807. return rc;
  808. }
  809. }
  810. qed_reset_vport_stats(cdev);
  811. return 0;
  812. err3:
  813. qed_hw_stop(cdev);
  814. err2:
  815. qed_hw_timers_stop_all(cdev);
  816. if (IS_PF(cdev))
  817. qed_slowpath_irq_free(cdev);
  818. qed_free_stream_mem(cdev);
  819. qed_disable_msix(cdev);
  820. err1:
  821. qed_resc_free(cdev);
  822. err:
  823. if (IS_PF(cdev))
  824. release_firmware(cdev->firmware);
  825. if (IS_PF(cdev) && QED_LEADING_HWFN(cdev)->p_ptp_ptt)
  826. qed_ptt_release(QED_LEADING_HWFN(cdev),
  827. QED_LEADING_HWFN(cdev)->p_ptp_ptt);
  828. qed_iov_wq_stop(cdev, false);
  829. return rc;
  830. }
  831. static int qed_slowpath_stop(struct qed_dev *cdev)
  832. {
  833. if (!cdev)
  834. return -ENODEV;
  835. qed_ll2_dealloc_if(cdev);
  836. if (IS_PF(cdev)) {
  837. qed_ptt_release(QED_LEADING_HWFN(cdev),
  838. QED_LEADING_HWFN(cdev)->p_ptp_ptt);
  839. qed_free_stream_mem(cdev);
  840. if (IS_QED_ETH_IF(cdev))
  841. qed_sriov_disable(cdev, true);
  842. qed_nic_stop(cdev);
  843. qed_slowpath_irq_free(cdev);
  844. }
  845. qed_disable_msix(cdev);
  846. qed_nic_reset(cdev);
  847. qed_iov_wq_stop(cdev, true);
  848. if (IS_PF(cdev))
  849. release_firmware(cdev->firmware);
  850. return 0;
  851. }
  852. static void qed_set_id(struct qed_dev *cdev, char name[NAME_SIZE],
  853. char ver_str[VER_SIZE])
  854. {
  855. int i;
  856. memcpy(cdev->name, name, NAME_SIZE);
  857. for_each_hwfn(cdev, i)
  858. snprintf(cdev->hwfns[i].name, NAME_SIZE, "%s-%d", name, i);
  859. memcpy(cdev->ver_str, ver_str, VER_SIZE);
  860. cdev->drv_type = DRV_ID_DRV_TYPE_LINUX;
  861. }
  862. static u32 qed_sb_init(struct qed_dev *cdev,
  863. struct qed_sb_info *sb_info,
  864. void *sb_virt_addr,
  865. dma_addr_t sb_phy_addr, u16 sb_id,
  866. enum qed_sb_type type)
  867. {
  868. struct qed_hwfn *p_hwfn;
  869. struct qed_ptt *p_ptt;
  870. int hwfn_index;
  871. u16 rel_sb_id;
  872. u8 n_hwfns;
  873. u32 rc;
  874. /* RoCE uses single engine and CMT uses two engines. When using both
  875. * we force only a single engine. Storage uses only engine 0 too.
  876. */
  877. if (type == QED_SB_TYPE_L2_QUEUE)
  878. n_hwfns = cdev->num_hwfns;
  879. else
  880. n_hwfns = 1;
  881. hwfn_index = sb_id % n_hwfns;
  882. p_hwfn = &cdev->hwfns[hwfn_index];
  883. rel_sb_id = sb_id / n_hwfns;
  884. DP_VERBOSE(cdev, NETIF_MSG_INTR,
  885. "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
  886. hwfn_index, rel_sb_id, sb_id);
  887. if (IS_PF(p_hwfn->cdev)) {
  888. p_ptt = qed_ptt_acquire(p_hwfn);
  889. if (!p_ptt)
  890. return -EBUSY;
  891. rc = qed_int_sb_init(p_hwfn, p_ptt, sb_info, sb_virt_addr,
  892. sb_phy_addr, rel_sb_id);
  893. qed_ptt_release(p_hwfn, p_ptt);
  894. } else {
  895. rc = qed_int_sb_init(p_hwfn, NULL, sb_info, sb_virt_addr,
  896. sb_phy_addr, rel_sb_id);
  897. }
  898. return rc;
  899. }
  900. static u32 qed_sb_release(struct qed_dev *cdev,
  901. struct qed_sb_info *sb_info, u16 sb_id)
  902. {
  903. struct qed_hwfn *p_hwfn;
  904. int hwfn_index;
  905. u16 rel_sb_id;
  906. u32 rc;
  907. hwfn_index = sb_id % cdev->num_hwfns;
  908. p_hwfn = &cdev->hwfns[hwfn_index];
  909. rel_sb_id = sb_id / cdev->num_hwfns;
  910. DP_VERBOSE(cdev, NETIF_MSG_INTR,
  911. "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
  912. hwfn_index, rel_sb_id, sb_id);
  913. rc = qed_int_sb_release(p_hwfn, sb_info, rel_sb_id);
  914. return rc;
  915. }
  916. static bool qed_can_link_change(struct qed_dev *cdev)
  917. {
  918. return true;
  919. }
  920. static int qed_set_link(struct qed_dev *cdev, struct qed_link_params *params)
  921. {
  922. struct qed_hwfn *hwfn;
  923. struct qed_mcp_link_params *link_params;
  924. struct qed_ptt *ptt;
  925. int rc;
  926. if (!cdev)
  927. return -ENODEV;
  928. /* The link should be set only once per PF */
  929. hwfn = &cdev->hwfns[0];
  930. /* When VF wants to set link, force it to read the bulletin instead.
  931. * This mimics the PF behavior, where a noitification [both immediate
  932. * and possible later] would be generated when changing properties.
  933. */
  934. if (IS_VF(cdev)) {
  935. qed_schedule_iov(hwfn, QED_IOV_WQ_VF_FORCE_LINK_QUERY_FLAG);
  936. return 0;
  937. }
  938. ptt = qed_ptt_acquire(hwfn);
  939. if (!ptt)
  940. return -EBUSY;
  941. link_params = qed_mcp_get_link_params(hwfn);
  942. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_AUTONEG)
  943. link_params->speed.autoneg = params->autoneg;
  944. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_ADV_SPEEDS) {
  945. link_params->speed.advertised_speeds = 0;
  946. if ((params->adv_speeds & QED_LM_1000baseT_Half_BIT) ||
  947. (params->adv_speeds & QED_LM_1000baseT_Full_BIT))
  948. link_params->speed.advertised_speeds |=
  949. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G;
  950. if (params->adv_speeds & QED_LM_10000baseKR_Full_BIT)
  951. link_params->speed.advertised_speeds |=
  952. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G;
  953. if (params->adv_speeds & QED_LM_25000baseKR_Full_BIT)
  954. link_params->speed.advertised_speeds |=
  955. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_25G;
  956. if (params->adv_speeds & QED_LM_40000baseLR4_Full_BIT)
  957. link_params->speed.advertised_speeds |=
  958. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G;
  959. if (params->adv_speeds & QED_LM_50000baseKR2_Full_BIT)
  960. link_params->speed.advertised_speeds |=
  961. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G;
  962. if (params->adv_speeds & QED_LM_100000baseKR4_Full_BIT)
  963. link_params->speed.advertised_speeds |=
  964. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_BB_100G;
  965. }
  966. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_FORCED_SPEED)
  967. link_params->speed.forced_speed = params->forced_speed;
  968. if (params->override_flags & QED_LINK_OVERRIDE_PAUSE_CONFIG) {
  969. if (params->pause_config & QED_LINK_PAUSE_AUTONEG_ENABLE)
  970. link_params->pause.autoneg = true;
  971. else
  972. link_params->pause.autoneg = false;
  973. if (params->pause_config & QED_LINK_PAUSE_RX_ENABLE)
  974. link_params->pause.forced_rx = true;
  975. else
  976. link_params->pause.forced_rx = false;
  977. if (params->pause_config & QED_LINK_PAUSE_TX_ENABLE)
  978. link_params->pause.forced_tx = true;
  979. else
  980. link_params->pause.forced_tx = false;
  981. }
  982. if (params->override_flags & QED_LINK_OVERRIDE_LOOPBACK_MODE) {
  983. switch (params->loopback_mode) {
  984. case QED_LINK_LOOPBACK_INT_PHY:
  985. link_params->loopback_mode = ETH_LOOPBACK_INT_PHY;
  986. break;
  987. case QED_LINK_LOOPBACK_EXT_PHY:
  988. link_params->loopback_mode = ETH_LOOPBACK_EXT_PHY;
  989. break;
  990. case QED_LINK_LOOPBACK_EXT:
  991. link_params->loopback_mode = ETH_LOOPBACK_EXT;
  992. break;
  993. case QED_LINK_LOOPBACK_MAC:
  994. link_params->loopback_mode = ETH_LOOPBACK_MAC;
  995. break;
  996. default:
  997. link_params->loopback_mode = ETH_LOOPBACK_NONE;
  998. break;
  999. }
  1000. }
  1001. rc = qed_mcp_set_link(hwfn, ptt, params->link_up);
  1002. qed_ptt_release(hwfn, ptt);
  1003. return rc;
  1004. }
  1005. static int qed_get_port_type(u32 media_type)
  1006. {
  1007. int port_type;
  1008. switch (media_type) {
  1009. case MEDIA_SFPP_10G_FIBER:
  1010. case MEDIA_SFP_1G_FIBER:
  1011. case MEDIA_XFP_FIBER:
  1012. case MEDIA_MODULE_FIBER:
  1013. case MEDIA_KR:
  1014. port_type = PORT_FIBRE;
  1015. break;
  1016. case MEDIA_DA_TWINAX:
  1017. port_type = PORT_DA;
  1018. break;
  1019. case MEDIA_BASE_T:
  1020. port_type = PORT_TP;
  1021. break;
  1022. case MEDIA_NOT_PRESENT:
  1023. port_type = PORT_NONE;
  1024. break;
  1025. case MEDIA_UNSPECIFIED:
  1026. default:
  1027. port_type = PORT_OTHER;
  1028. break;
  1029. }
  1030. return port_type;
  1031. }
  1032. static int qed_get_link_data(struct qed_hwfn *hwfn,
  1033. struct qed_mcp_link_params *params,
  1034. struct qed_mcp_link_state *link,
  1035. struct qed_mcp_link_capabilities *link_caps)
  1036. {
  1037. void *p;
  1038. if (!IS_PF(hwfn->cdev)) {
  1039. qed_vf_get_link_params(hwfn, params);
  1040. qed_vf_get_link_state(hwfn, link);
  1041. qed_vf_get_link_caps(hwfn, link_caps);
  1042. return 0;
  1043. }
  1044. p = qed_mcp_get_link_params(hwfn);
  1045. if (!p)
  1046. return -ENXIO;
  1047. memcpy(params, p, sizeof(*params));
  1048. p = qed_mcp_get_link_state(hwfn);
  1049. if (!p)
  1050. return -ENXIO;
  1051. memcpy(link, p, sizeof(*link));
  1052. p = qed_mcp_get_link_capabilities(hwfn);
  1053. if (!p)
  1054. return -ENXIO;
  1055. memcpy(link_caps, p, sizeof(*link_caps));
  1056. return 0;
  1057. }
  1058. static void qed_fill_link(struct qed_hwfn *hwfn,
  1059. struct qed_link_output *if_link)
  1060. {
  1061. struct qed_mcp_link_params params;
  1062. struct qed_mcp_link_state link;
  1063. struct qed_mcp_link_capabilities link_caps;
  1064. u32 media_type;
  1065. memset(if_link, 0, sizeof(*if_link));
  1066. /* Prepare source inputs */
  1067. if (qed_get_link_data(hwfn, &params, &link, &link_caps)) {
  1068. dev_warn(&hwfn->cdev->pdev->dev, "no link data available\n");
  1069. return;
  1070. }
  1071. /* Set the link parameters to pass to protocol driver */
  1072. if (link.link_up)
  1073. if_link->link_up = true;
  1074. /* TODO - at the moment assume supported and advertised speed equal */
  1075. if_link->supported_caps = QED_LM_FIBRE_BIT;
  1076. if (params.speed.autoneg)
  1077. if_link->supported_caps |= QED_LM_Autoneg_BIT;
  1078. if (params.pause.autoneg ||
  1079. (params.pause.forced_rx && params.pause.forced_tx))
  1080. if_link->supported_caps |= QED_LM_Asym_Pause_BIT;
  1081. if (params.pause.autoneg || params.pause.forced_rx ||
  1082. params.pause.forced_tx)
  1083. if_link->supported_caps |= QED_LM_Pause_BIT;
  1084. if_link->advertised_caps = if_link->supported_caps;
  1085. if (params.speed.advertised_speeds &
  1086. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G)
  1087. if_link->advertised_caps |= QED_LM_1000baseT_Half_BIT |
  1088. QED_LM_1000baseT_Full_BIT;
  1089. if (params.speed.advertised_speeds &
  1090. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G)
  1091. if_link->advertised_caps |= QED_LM_10000baseKR_Full_BIT;
  1092. if (params.speed.advertised_speeds &
  1093. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_25G)
  1094. if_link->advertised_caps |= QED_LM_25000baseKR_Full_BIT;
  1095. if (params.speed.advertised_speeds &
  1096. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G)
  1097. if_link->advertised_caps |= QED_LM_40000baseLR4_Full_BIT;
  1098. if (params.speed.advertised_speeds &
  1099. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G)
  1100. if_link->advertised_caps |= QED_LM_50000baseKR2_Full_BIT;
  1101. if (params.speed.advertised_speeds &
  1102. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_BB_100G)
  1103. if_link->advertised_caps |= QED_LM_100000baseKR4_Full_BIT;
  1104. if (link_caps.speed_capabilities &
  1105. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G)
  1106. if_link->supported_caps |= QED_LM_1000baseT_Half_BIT |
  1107. QED_LM_1000baseT_Full_BIT;
  1108. if (link_caps.speed_capabilities &
  1109. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G)
  1110. if_link->supported_caps |= QED_LM_10000baseKR_Full_BIT;
  1111. if (link_caps.speed_capabilities &
  1112. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_25G)
  1113. if_link->supported_caps |= QED_LM_25000baseKR_Full_BIT;
  1114. if (link_caps.speed_capabilities &
  1115. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G)
  1116. if_link->supported_caps |= QED_LM_40000baseLR4_Full_BIT;
  1117. if (link_caps.speed_capabilities &
  1118. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G)
  1119. if_link->supported_caps |= QED_LM_50000baseKR2_Full_BIT;
  1120. if (link_caps.speed_capabilities &
  1121. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_BB_100G)
  1122. if_link->supported_caps |= QED_LM_100000baseKR4_Full_BIT;
  1123. if (link.link_up)
  1124. if_link->speed = link.speed;
  1125. /* TODO - fill duplex properly */
  1126. if_link->duplex = DUPLEX_FULL;
  1127. qed_mcp_get_media_type(hwfn->cdev, &media_type);
  1128. if_link->port = qed_get_port_type(media_type);
  1129. if_link->autoneg = params.speed.autoneg;
  1130. if (params.pause.autoneg)
  1131. if_link->pause_config |= QED_LINK_PAUSE_AUTONEG_ENABLE;
  1132. if (params.pause.forced_rx)
  1133. if_link->pause_config |= QED_LINK_PAUSE_RX_ENABLE;
  1134. if (params.pause.forced_tx)
  1135. if_link->pause_config |= QED_LINK_PAUSE_TX_ENABLE;
  1136. /* Link partner capabilities */
  1137. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_1G_HD)
  1138. if_link->lp_caps |= QED_LM_1000baseT_Half_BIT;
  1139. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_1G_FD)
  1140. if_link->lp_caps |= QED_LM_1000baseT_Full_BIT;
  1141. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_10G)
  1142. if_link->lp_caps |= QED_LM_10000baseKR_Full_BIT;
  1143. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_25G)
  1144. if_link->lp_caps |= QED_LM_25000baseKR_Full_BIT;
  1145. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_40G)
  1146. if_link->lp_caps |= QED_LM_40000baseLR4_Full_BIT;
  1147. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_50G)
  1148. if_link->lp_caps |= QED_LM_50000baseKR2_Full_BIT;
  1149. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_100G)
  1150. if_link->lp_caps |= QED_LM_100000baseKR4_Full_BIT;
  1151. if (link.an_complete)
  1152. if_link->lp_caps |= QED_LM_Autoneg_BIT;
  1153. if (link.partner_adv_pause)
  1154. if_link->lp_caps |= QED_LM_Pause_BIT;
  1155. if (link.partner_adv_pause == QED_LINK_PARTNER_ASYMMETRIC_PAUSE ||
  1156. link.partner_adv_pause == QED_LINK_PARTNER_BOTH_PAUSE)
  1157. if_link->lp_caps |= QED_LM_Asym_Pause_BIT;
  1158. }
  1159. static void qed_get_current_link(struct qed_dev *cdev,
  1160. struct qed_link_output *if_link)
  1161. {
  1162. int i;
  1163. qed_fill_link(&cdev->hwfns[0], if_link);
  1164. for_each_hwfn(cdev, i)
  1165. qed_inform_vf_link_state(&cdev->hwfns[i]);
  1166. }
  1167. void qed_link_update(struct qed_hwfn *hwfn)
  1168. {
  1169. void *cookie = hwfn->cdev->ops_cookie;
  1170. struct qed_common_cb_ops *op = hwfn->cdev->protocol_ops.common;
  1171. struct qed_link_output if_link;
  1172. qed_fill_link(hwfn, &if_link);
  1173. qed_inform_vf_link_state(hwfn);
  1174. if (IS_LEAD_HWFN(hwfn) && cookie)
  1175. op->link_update(cookie, &if_link);
  1176. }
  1177. static int qed_drain(struct qed_dev *cdev)
  1178. {
  1179. struct qed_hwfn *hwfn;
  1180. struct qed_ptt *ptt;
  1181. int i, rc;
  1182. if (IS_VF(cdev))
  1183. return 0;
  1184. for_each_hwfn(cdev, i) {
  1185. hwfn = &cdev->hwfns[i];
  1186. ptt = qed_ptt_acquire(hwfn);
  1187. if (!ptt) {
  1188. DP_NOTICE(hwfn, "Failed to drain NIG; No PTT\n");
  1189. return -EBUSY;
  1190. }
  1191. rc = qed_mcp_drain(hwfn, ptt);
  1192. if (rc)
  1193. return rc;
  1194. qed_ptt_release(hwfn, ptt);
  1195. }
  1196. return 0;
  1197. }
  1198. static void qed_get_coalesce(struct qed_dev *cdev, u16 *rx_coal, u16 *tx_coal)
  1199. {
  1200. *rx_coal = cdev->rx_coalesce_usecs;
  1201. *tx_coal = cdev->tx_coalesce_usecs;
  1202. }
  1203. static int qed_set_coalesce(struct qed_dev *cdev, u16 rx_coal, u16 tx_coal,
  1204. u8 qid, u16 sb_id)
  1205. {
  1206. struct qed_hwfn *hwfn;
  1207. struct qed_ptt *ptt;
  1208. int hwfn_index;
  1209. int status = 0;
  1210. hwfn_index = qid % cdev->num_hwfns;
  1211. hwfn = &cdev->hwfns[hwfn_index];
  1212. ptt = qed_ptt_acquire(hwfn);
  1213. if (!ptt)
  1214. return -EAGAIN;
  1215. status = qed_set_rxq_coalesce(hwfn, ptt, rx_coal,
  1216. qid / cdev->num_hwfns, sb_id);
  1217. if (status)
  1218. goto out;
  1219. status = qed_set_txq_coalesce(hwfn, ptt, tx_coal,
  1220. qid / cdev->num_hwfns, sb_id);
  1221. out:
  1222. qed_ptt_release(hwfn, ptt);
  1223. return status;
  1224. }
  1225. static int qed_set_led(struct qed_dev *cdev, enum qed_led_mode mode)
  1226. {
  1227. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1228. struct qed_ptt *ptt;
  1229. int status = 0;
  1230. ptt = qed_ptt_acquire(hwfn);
  1231. if (!ptt)
  1232. return -EAGAIN;
  1233. status = qed_mcp_set_led(hwfn, ptt, mode);
  1234. qed_ptt_release(hwfn, ptt);
  1235. return status;
  1236. }
  1237. static int qed_update_wol(struct qed_dev *cdev, bool enabled)
  1238. {
  1239. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1240. struct qed_ptt *ptt;
  1241. int rc = 0;
  1242. if (IS_VF(cdev))
  1243. return 0;
  1244. ptt = qed_ptt_acquire(hwfn);
  1245. if (!ptt)
  1246. return -EAGAIN;
  1247. rc = qed_mcp_ov_update_wol(hwfn, ptt, enabled ? QED_OV_WOL_ENABLED
  1248. : QED_OV_WOL_DISABLED);
  1249. if (rc)
  1250. goto out;
  1251. rc = qed_mcp_ov_update_current_config(hwfn, ptt, QED_OV_CLIENT_DRV);
  1252. out:
  1253. qed_ptt_release(hwfn, ptt);
  1254. return rc;
  1255. }
  1256. static int qed_update_drv_state(struct qed_dev *cdev, bool active)
  1257. {
  1258. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1259. struct qed_ptt *ptt;
  1260. int status = 0;
  1261. if (IS_VF(cdev))
  1262. return 0;
  1263. ptt = qed_ptt_acquire(hwfn);
  1264. if (!ptt)
  1265. return -EAGAIN;
  1266. status = qed_mcp_ov_update_driver_state(hwfn, ptt, active ?
  1267. QED_OV_DRIVER_STATE_ACTIVE :
  1268. QED_OV_DRIVER_STATE_DISABLED);
  1269. qed_ptt_release(hwfn, ptt);
  1270. return status;
  1271. }
  1272. static int qed_update_mac(struct qed_dev *cdev, u8 *mac)
  1273. {
  1274. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1275. struct qed_ptt *ptt;
  1276. int status = 0;
  1277. if (IS_VF(cdev))
  1278. return 0;
  1279. ptt = qed_ptt_acquire(hwfn);
  1280. if (!ptt)
  1281. return -EAGAIN;
  1282. status = qed_mcp_ov_update_mac(hwfn, ptt, mac);
  1283. if (status)
  1284. goto out;
  1285. status = qed_mcp_ov_update_current_config(hwfn, ptt, QED_OV_CLIENT_DRV);
  1286. out:
  1287. qed_ptt_release(hwfn, ptt);
  1288. return status;
  1289. }
  1290. static int qed_update_mtu(struct qed_dev *cdev, u16 mtu)
  1291. {
  1292. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1293. struct qed_ptt *ptt;
  1294. int status = 0;
  1295. if (IS_VF(cdev))
  1296. return 0;
  1297. ptt = qed_ptt_acquire(hwfn);
  1298. if (!ptt)
  1299. return -EAGAIN;
  1300. status = qed_mcp_ov_update_mtu(hwfn, ptt, mtu);
  1301. if (status)
  1302. goto out;
  1303. status = qed_mcp_ov_update_current_config(hwfn, ptt, QED_OV_CLIENT_DRV);
  1304. out:
  1305. qed_ptt_release(hwfn, ptt);
  1306. return status;
  1307. }
  1308. static struct qed_selftest_ops qed_selftest_ops_pass = {
  1309. .selftest_memory = &qed_selftest_memory,
  1310. .selftest_interrupt = &qed_selftest_interrupt,
  1311. .selftest_register = &qed_selftest_register,
  1312. .selftest_clock = &qed_selftest_clock,
  1313. .selftest_nvram = &qed_selftest_nvram,
  1314. };
  1315. const struct qed_common_ops qed_common_ops_pass = {
  1316. .selftest = &qed_selftest_ops_pass,
  1317. .probe = &qed_probe,
  1318. .remove = &qed_remove,
  1319. .set_power_state = &qed_set_power_state,
  1320. .set_id = &qed_set_id,
  1321. .update_pf_params = &qed_update_pf_params,
  1322. .slowpath_start = &qed_slowpath_start,
  1323. .slowpath_stop = &qed_slowpath_stop,
  1324. .set_fp_int = &qed_set_int_fp,
  1325. .get_fp_int = &qed_get_int_fp,
  1326. .sb_init = &qed_sb_init,
  1327. .sb_release = &qed_sb_release,
  1328. .simd_handler_config = &qed_simd_handler_config,
  1329. .simd_handler_clean = &qed_simd_handler_clean,
  1330. .dbg_grc = &qed_dbg_grc,
  1331. .dbg_grc_size = &qed_dbg_grc_size,
  1332. .can_link_change = &qed_can_link_change,
  1333. .set_link = &qed_set_link,
  1334. .get_link = &qed_get_current_link,
  1335. .drain = &qed_drain,
  1336. .update_msglvl = &qed_init_dp,
  1337. .dbg_all_data = &qed_dbg_all_data,
  1338. .dbg_all_data_size = &qed_dbg_all_data_size,
  1339. .chain_alloc = &qed_chain_alloc,
  1340. .chain_free = &qed_chain_free,
  1341. .get_coalesce = &qed_get_coalesce,
  1342. .set_coalesce = &qed_set_coalesce,
  1343. .set_led = &qed_set_led,
  1344. .update_drv_state = &qed_update_drv_state,
  1345. .update_mac = &qed_update_mac,
  1346. .update_mtu = &qed_update_mtu,
  1347. .update_wol = &qed_update_wol,
  1348. };
  1349. void qed_get_protocol_stats(struct qed_dev *cdev,
  1350. enum qed_mcp_protocol_type type,
  1351. union qed_mcp_protocol_stats *stats)
  1352. {
  1353. struct qed_eth_stats eth_stats;
  1354. memset(stats, 0, sizeof(*stats));
  1355. switch (type) {
  1356. case QED_MCP_LAN_STATS:
  1357. qed_get_vport_stats(cdev, &eth_stats);
  1358. stats->lan_stats.ucast_rx_pkts = eth_stats.rx_ucast_pkts;
  1359. stats->lan_stats.ucast_tx_pkts = eth_stats.tx_ucast_pkts;
  1360. stats->lan_stats.fcs_err = -1;
  1361. break;
  1362. case QED_MCP_FCOE_STATS:
  1363. qed_get_protocol_stats_fcoe(cdev, &stats->fcoe_stats);
  1364. break;
  1365. default:
  1366. DP_ERR(cdev, "Invalid protocol type = %d\n", type);
  1367. return;
  1368. }
  1369. }