qed_main.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  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/delay.h>
  37. #include <asm/byteorder.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/string.h>
  40. #include <linux/module.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/workqueue.h>
  43. #include <linux/ethtool.h>
  44. #include <linux/etherdevice.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/crash_dump.h>
  47. #include <linux/crc32.h>
  48. #include <linux/qed/qed_if.h>
  49. #include <linux/qed/qed_ll2_if.h>
  50. #include "qed.h"
  51. #include "qed_sriov.h"
  52. #include "qed_sp.h"
  53. #include "qed_dev_api.h"
  54. #include "qed_ll2.h"
  55. #include "qed_fcoe.h"
  56. #include "qed_iscsi.h"
  57. #include "qed_mcp.h"
  58. #include "qed_hw.h"
  59. #include "qed_selftest.h"
  60. #include "qed_debug.h"
  61. #define QED_ROCE_QPS (8192)
  62. #define QED_ROCE_DPIS (8)
  63. static char version[] =
  64. "QLogic FastLinQ 4xxxx Core Module qed " DRV_MODULE_VERSION "\n";
  65. MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx Core Module");
  66. MODULE_LICENSE("GPL");
  67. MODULE_VERSION(DRV_MODULE_VERSION);
  68. #define FW_FILE_VERSION \
  69. __stringify(FW_MAJOR_VERSION) "." \
  70. __stringify(FW_MINOR_VERSION) "." \
  71. __stringify(FW_REVISION_VERSION) "." \
  72. __stringify(FW_ENGINEERING_VERSION)
  73. #define QED_FW_FILE_NAME \
  74. "qed/qed_init_values_zipped-" FW_FILE_VERSION ".bin"
  75. MODULE_FIRMWARE(QED_FW_FILE_NAME);
  76. static int __init qed_init(void)
  77. {
  78. pr_info("%s", version);
  79. return 0;
  80. }
  81. static void __exit qed_cleanup(void)
  82. {
  83. pr_notice("qed_cleanup called\n");
  84. }
  85. module_init(qed_init);
  86. module_exit(qed_cleanup);
  87. /* Check if the DMA controller on the machine can properly handle the DMA
  88. * addressing required by the device.
  89. */
  90. static int qed_set_coherency_mask(struct qed_dev *cdev)
  91. {
  92. struct device *dev = &cdev->pdev->dev;
  93. if (dma_set_mask(dev, DMA_BIT_MASK(64)) == 0) {
  94. if (dma_set_coherent_mask(dev, DMA_BIT_MASK(64)) != 0) {
  95. DP_NOTICE(cdev,
  96. "Can't request 64-bit consistent allocations\n");
  97. return -EIO;
  98. }
  99. } else if (dma_set_mask(dev, DMA_BIT_MASK(32)) != 0) {
  100. DP_NOTICE(cdev, "Can't request 64b/32b DMA addresses\n");
  101. return -EIO;
  102. }
  103. return 0;
  104. }
  105. static void qed_free_pci(struct qed_dev *cdev)
  106. {
  107. struct pci_dev *pdev = cdev->pdev;
  108. if (cdev->doorbells && cdev->db_size)
  109. iounmap(cdev->doorbells);
  110. if (cdev->regview)
  111. iounmap(cdev->regview);
  112. if (atomic_read(&pdev->enable_cnt) == 1)
  113. pci_release_regions(pdev);
  114. pci_disable_device(pdev);
  115. }
  116. #define PCI_REVISION_ID_ERROR_VAL 0xff
  117. /* Performs PCI initializations as well as initializing PCI-related parameters
  118. * in the device structrue. Returns 0 in case of success.
  119. */
  120. static int qed_init_pci(struct qed_dev *cdev, struct pci_dev *pdev)
  121. {
  122. u8 rev_id;
  123. int rc;
  124. cdev->pdev = pdev;
  125. rc = pci_enable_device(pdev);
  126. if (rc) {
  127. DP_NOTICE(cdev, "Cannot enable PCI device\n");
  128. goto err0;
  129. }
  130. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  131. DP_NOTICE(cdev, "No memory region found in bar #0\n");
  132. rc = -EIO;
  133. goto err1;
  134. }
  135. if (IS_PF(cdev) && !(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  136. DP_NOTICE(cdev, "No memory region found in bar #2\n");
  137. rc = -EIO;
  138. goto err1;
  139. }
  140. if (atomic_read(&pdev->enable_cnt) == 1) {
  141. rc = pci_request_regions(pdev, "qed");
  142. if (rc) {
  143. DP_NOTICE(cdev,
  144. "Failed to request PCI memory resources\n");
  145. goto err1;
  146. }
  147. pci_set_master(pdev);
  148. pci_save_state(pdev);
  149. }
  150. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  151. if (rev_id == PCI_REVISION_ID_ERROR_VAL) {
  152. DP_NOTICE(cdev,
  153. "Detected PCI device error [rev_id 0x%x]. Probably due to prior indication. Aborting.\n",
  154. rev_id);
  155. rc = -ENODEV;
  156. goto err2;
  157. }
  158. if (!pci_is_pcie(pdev)) {
  159. DP_NOTICE(cdev, "The bus is not PCI Express\n");
  160. rc = -EIO;
  161. goto err2;
  162. }
  163. cdev->pci_params.pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  164. if (IS_PF(cdev) && !cdev->pci_params.pm_cap)
  165. DP_NOTICE(cdev, "Cannot find power management capability\n");
  166. rc = qed_set_coherency_mask(cdev);
  167. if (rc)
  168. goto err2;
  169. cdev->pci_params.mem_start = pci_resource_start(pdev, 0);
  170. cdev->pci_params.mem_end = pci_resource_end(pdev, 0);
  171. cdev->pci_params.irq = pdev->irq;
  172. cdev->regview = pci_ioremap_bar(pdev, 0);
  173. if (!cdev->regview) {
  174. DP_NOTICE(cdev, "Cannot map register space, aborting\n");
  175. rc = -ENOMEM;
  176. goto err2;
  177. }
  178. cdev->db_phys_addr = pci_resource_start(cdev->pdev, 2);
  179. cdev->db_size = pci_resource_len(cdev->pdev, 2);
  180. if (!cdev->db_size) {
  181. if (IS_PF(cdev)) {
  182. DP_NOTICE(cdev, "No Doorbell bar available\n");
  183. return -EINVAL;
  184. } else {
  185. return 0;
  186. }
  187. }
  188. cdev->doorbells = ioremap_wc(cdev->db_phys_addr, cdev->db_size);
  189. if (!cdev->doorbells) {
  190. DP_NOTICE(cdev, "Cannot map doorbell space\n");
  191. return -ENOMEM;
  192. }
  193. return 0;
  194. err2:
  195. pci_release_regions(pdev);
  196. err1:
  197. pci_disable_device(pdev);
  198. err0:
  199. return rc;
  200. }
  201. int qed_fill_dev_info(struct qed_dev *cdev,
  202. struct qed_dev_info *dev_info)
  203. {
  204. struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
  205. struct qed_hw_info *hw_info = &p_hwfn->hw_info;
  206. struct qed_tunnel_info *tun = &cdev->tunnel;
  207. struct qed_ptt *ptt;
  208. memset(dev_info, 0, sizeof(struct qed_dev_info));
  209. if (tun->vxlan.tun_cls == QED_TUNN_CLSS_MAC_VLAN &&
  210. tun->vxlan.b_mode_enabled)
  211. dev_info->vxlan_enable = true;
  212. if (tun->l2_gre.b_mode_enabled && tun->ip_gre.b_mode_enabled &&
  213. tun->l2_gre.tun_cls == QED_TUNN_CLSS_MAC_VLAN &&
  214. tun->ip_gre.tun_cls == QED_TUNN_CLSS_MAC_VLAN)
  215. dev_info->gre_enable = true;
  216. if (tun->l2_geneve.b_mode_enabled && tun->ip_geneve.b_mode_enabled &&
  217. tun->l2_geneve.tun_cls == QED_TUNN_CLSS_MAC_VLAN &&
  218. tun->ip_geneve.tun_cls == QED_TUNN_CLSS_MAC_VLAN)
  219. dev_info->geneve_enable = true;
  220. dev_info->num_hwfns = cdev->num_hwfns;
  221. dev_info->pci_mem_start = cdev->pci_params.mem_start;
  222. dev_info->pci_mem_end = cdev->pci_params.mem_end;
  223. dev_info->pci_irq = cdev->pci_params.irq;
  224. dev_info->rdma_supported = QED_IS_RDMA_PERSONALITY(p_hwfn);
  225. dev_info->is_mf_default = IS_MF_DEFAULT(&cdev->hwfns[0]);
  226. dev_info->dev_type = cdev->type;
  227. ether_addr_copy(dev_info->hw_mac, hw_info->hw_mac_addr);
  228. if (IS_PF(cdev)) {
  229. dev_info->fw_major = FW_MAJOR_VERSION;
  230. dev_info->fw_minor = FW_MINOR_VERSION;
  231. dev_info->fw_rev = FW_REVISION_VERSION;
  232. dev_info->fw_eng = FW_ENGINEERING_VERSION;
  233. dev_info->mf_mode = cdev->mf_mode;
  234. dev_info->tx_switching = true;
  235. if (hw_info->b_wol_support == QED_WOL_SUPPORT_PME)
  236. dev_info->wol_support = true;
  237. dev_info->abs_pf_id = QED_LEADING_HWFN(cdev)->abs_pf_id;
  238. } else {
  239. qed_vf_get_fw_version(&cdev->hwfns[0], &dev_info->fw_major,
  240. &dev_info->fw_minor, &dev_info->fw_rev,
  241. &dev_info->fw_eng);
  242. }
  243. if (IS_PF(cdev)) {
  244. ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
  245. if (ptt) {
  246. qed_mcp_get_mfw_ver(QED_LEADING_HWFN(cdev), ptt,
  247. &dev_info->mfw_rev, NULL);
  248. qed_mcp_get_mbi_ver(QED_LEADING_HWFN(cdev), ptt,
  249. &dev_info->mbi_version);
  250. qed_mcp_get_flash_size(QED_LEADING_HWFN(cdev), ptt,
  251. &dev_info->flash_size);
  252. qed_ptt_release(QED_LEADING_HWFN(cdev), ptt);
  253. }
  254. } else {
  255. qed_mcp_get_mfw_ver(QED_LEADING_HWFN(cdev), NULL,
  256. &dev_info->mfw_rev, NULL);
  257. }
  258. dev_info->mtu = hw_info->mtu;
  259. return 0;
  260. }
  261. static void qed_free_cdev(struct qed_dev *cdev)
  262. {
  263. kfree((void *)cdev);
  264. }
  265. static struct qed_dev *qed_alloc_cdev(struct pci_dev *pdev)
  266. {
  267. struct qed_dev *cdev;
  268. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  269. if (!cdev)
  270. return cdev;
  271. qed_init_struct(cdev);
  272. return cdev;
  273. }
  274. /* Sets the requested power state */
  275. static int qed_set_power_state(struct qed_dev *cdev, pci_power_t state)
  276. {
  277. if (!cdev)
  278. return -ENODEV;
  279. DP_VERBOSE(cdev, NETIF_MSG_DRV, "Omitting Power state change\n");
  280. return 0;
  281. }
  282. /* probing */
  283. static struct qed_dev *qed_probe(struct pci_dev *pdev,
  284. struct qed_probe_params *params)
  285. {
  286. struct qed_dev *cdev;
  287. int rc;
  288. cdev = qed_alloc_cdev(pdev);
  289. if (!cdev)
  290. goto err0;
  291. cdev->drv_type = DRV_ID_DRV_TYPE_LINUX;
  292. cdev->protocol = params->protocol;
  293. if (params->is_vf)
  294. cdev->b_is_vf = true;
  295. qed_init_dp(cdev, params->dp_module, params->dp_level);
  296. rc = qed_init_pci(cdev, pdev);
  297. if (rc) {
  298. DP_ERR(cdev, "init pci failed\n");
  299. goto err1;
  300. }
  301. DP_INFO(cdev, "PCI init completed successfully\n");
  302. rc = qed_hw_prepare(cdev, QED_PCI_DEFAULT);
  303. if (rc) {
  304. DP_ERR(cdev, "hw prepare failed\n");
  305. goto err2;
  306. }
  307. DP_INFO(cdev, "qed_probe completed successffuly\n");
  308. return cdev;
  309. err2:
  310. qed_free_pci(cdev);
  311. err1:
  312. qed_free_cdev(cdev);
  313. err0:
  314. return NULL;
  315. }
  316. static void qed_remove(struct qed_dev *cdev)
  317. {
  318. if (!cdev)
  319. return;
  320. qed_hw_remove(cdev);
  321. qed_free_pci(cdev);
  322. qed_set_power_state(cdev, PCI_D3hot);
  323. qed_free_cdev(cdev);
  324. }
  325. static void qed_disable_msix(struct qed_dev *cdev)
  326. {
  327. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  328. pci_disable_msix(cdev->pdev);
  329. kfree(cdev->int_params.msix_table);
  330. } else if (cdev->int_params.out.int_mode == QED_INT_MODE_MSI) {
  331. pci_disable_msi(cdev->pdev);
  332. }
  333. memset(&cdev->int_params.out, 0, sizeof(struct qed_int_param));
  334. }
  335. static int qed_enable_msix(struct qed_dev *cdev,
  336. struct qed_int_params *int_params)
  337. {
  338. int i, rc, cnt;
  339. cnt = int_params->in.num_vectors;
  340. for (i = 0; i < cnt; i++)
  341. int_params->msix_table[i].entry = i;
  342. rc = pci_enable_msix_range(cdev->pdev, int_params->msix_table,
  343. int_params->in.min_msix_cnt, cnt);
  344. if (rc < cnt && rc >= int_params->in.min_msix_cnt &&
  345. (rc % cdev->num_hwfns)) {
  346. pci_disable_msix(cdev->pdev);
  347. /* If fastpath is initialized, we need at least one interrupt
  348. * per hwfn [and the slow path interrupts]. New requested number
  349. * should be a multiple of the number of hwfns.
  350. */
  351. cnt = (rc / cdev->num_hwfns) * cdev->num_hwfns;
  352. DP_NOTICE(cdev,
  353. "Trying to enable MSI-X with less vectors (%d out of %d)\n",
  354. cnt, int_params->in.num_vectors);
  355. rc = pci_enable_msix_exact(cdev->pdev, int_params->msix_table,
  356. cnt);
  357. if (!rc)
  358. rc = cnt;
  359. }
  360. if (rc > 0) {
  361. /* MSI-x configuration was achieved */
  362. int_params->out.int_mode = QED_INT_MODE_MSIX;
  363. int_params->out.num_vectors = rc;
  364. rc = 0;
  365. } else {
  366. DP_NOTICE(cdev,
  367. "Failed to enable MSI-X [Requested %d vectors][rc %d]\n",
  368. cnt, rc);
  369. }
  370. return rc;
  371. }
  372. /* This function outputs the int mode and the number of enabled msix vector */
  373. static int qed_set_int_mode(struct qed_dev *cdev, bool force_mode)
  374. {
  375. struct qed_int_params *int_params = &cdev->int_params;
  376. struct msix_entry *tbl;
  377. int rc = 0, cnt;
  378. switch (int_params->in.int_mode) {
  379. case QED_INT_MODE_MSIX:
  380. /* Allocate MSIX table */
  381. cnt = int_params->in.num_vectors;
  382. int_params->msix_table = kcalloc(cnt, sizeof(*tbl), GFP_KERNEL);
  383. if (!int_params->msix_table) {
  384. rc = -ENOMEM;
  385. goto out;
  386. }
  387. /* Enable MSIX */
  388. rc = qed_enable_msix(cdev, int_params);
  389. if (!rc)
  390. goto out;
  391. DP_NOTICE(cdev, "Failed to enable MSI-X\n");
  392. kfree(int_params->msix_table);
  393. if (force_mode)
  394. goto out;
  395. /* Fallthrough */
  396. case QED_INT_MODE_MSI:
  397. if (cdev->num_hwfns == 1) {
  398. rc = pci_enable_msi(cdev->pdev);
  399. if (!rc) {
  400. int_params->out.int_mode = QED_INT_MODE_MSI;
  401. goto out;
  402. }
  403. DP_NOTICE(cdev, "Failed to enable MSI\n");
  404. if (force_mode)
  405. goto out;
  406. }
  407. /* Fallthrough */
  408. case QED_INT_MODE_INTA:
  409. int_params->out.int_mode = QED_INT_MODE_INTA;
  410. rc = 0;
  411. goto out;
  412. default:
  413. DP_NOTICE(cdev, "Unknown int_mode value %d\n",
  414. int_params->in.int_mode);
  415. rc = -EINVAL;
  416. }
  417. out:
  418. if (!rc)
  419. DP_INFO(cdev, "Using %s interrupts\n",
  420. int_params->out.int_mode == QED_INT_MODE_INTA ?
  421. "INTa" : int_params->out.int_mode == QED_INT_MODE_MSI ?
  422. "MSI" : "MSIX");
  423. cdev->int_coalescing_mode = QED_COAL_MODE_ENABLE;
  424. return rc;
  425. }
  426. static void qed_simd_handler_config(struct qed_dev *cdev, void *token,
  427. int index, void(*handler)(void *))
  428. {
  429. struct qed_hwfn *hwfn = &cdev->hwfns[index % cdev->num_hwfns];
  430. int relative_idx = index / cdev->num_hwfns;
  431. hwfn->simd_proto_handler[relative_idx].func = handler;
  432. hwfn->simd_proto_handler[relative_idx].token = token;
  433. }
  434. static void qed_simd_handler_clean(struct qed_dev *cdev, int index)
  435. {
  436. struct qed_hwfn *hwfn = &cdev->hwfns[index % cdev->num_hwfns];
  437. int relative_idx = index / cdev->num_hwfns;
  438. memset(&hwfn->simd_proto_handler[relative_idx], 0,
  439. sizeof(struct qed_simd_fp_handler));
  440. }
  441. static irqreturn_t qed_msix_sp_int(int irq, void *tasklet)
  442. {
  443. tasklet_schedule((struct tasklet_struct *)tasklet);
  444. return IRQ_HANDLED;
  445. }
  446. static irqreturn_t qed_single_int(int irq, void *dev_instance)
  447. {
  448. struct qed_dev *cdev = (struct qed_dev *)dev_instance;
  449. struct qed_hwfn *hwfn;
  450. irqreturn_t rc = IRQ_NONE;
  451. u64 status;
  452. int i, j;
  453. for (i = 0; i < cdev->num_hwfns; i++) {
  454. status = qed_int_igu_read_sisr_reg(&cdev->hwfns[i]);
  455. if (!status)
  456. continue;
  457. hwfn = &cdev->hwfns[i];
  458. /* Slowpath interrupt */
  459. if (unlikely(status & 0x1)) {
  460. tasklet_schedule(hwfn->sp_dpc);
  461. status &= ~0x1;
  462. rc = IRQ_HANDLED;
  463. }
  464. /* Fastpath interrupts */
  465. for (j = 0; j < 64; j++) {
  466. if ((0x2ULL << j) & status) {
  467. hwfn->simd_proto_handler[j].func(
  468. hwfn->simd_proto_handler[j].token);
  469. status &= ~(0x2ULL << j);
  470. rc = IRQ_HANDLED;
  471. }
  472. }
  473. if (unlikely(status))
  474. DP_VERBOSE(hwfn, NETIF_MSG_INTR,
  475. "got an unknown interrupt status 0x%llx\n",
  476. status);
  477. }
  478. return rc;
  479. }
  480. int qed_slowpath_irq_req(struct qed_hwfn *hwfn)
  481. {
  482. struct qed_dev *cdev = hwfn->cdev;
  483. u32 int_mode;
  484. int rc = 0;
  485. u8 id;
  486. int_mode = cdev->int_params.out.int_mode;
  487. if (int_mode == QED_INT_MODE_MSIX) {
  488. id = hwfn->my_id;
  489. snprintf(hwfn->name, NAME_SIZE, "sp-%d-%02x:%02x.%02x",
  490. id, cdev->pdev->bus->number,
  491. PCI_SLOT(cdev->pdev->devfn), hwfn->abs_pf_id);
  492. rc = request_irq(cdev->int_params.msix_table[id].vector,
  493. qed_msix_sp_int, 0, hwfn->name, hwfn->sp_dpc);
  494. } else {
  495. unsigned long flags = 0;
  496. snprintf(cdev->name, NAME_SIZE, "%02x:%02x.%02x",
  497. cdev->pdev->bus->number, PCI_SLOT(cdev->pdev->devfn),
  498. PCI_FUNC(cdev->pdev->devfn));
  499. if (cdev->int_params.out.int_mode == QED_INT_MODE_INTA)
  500. flags |= IRQF_SHARED;
  501. rc = request_irq(cdev->pdev->irq, qed_single_int,
  502. flags, cdev->name, cdev);
  503. }
  504. if (rc)
  505. DP_NOTICE(cdev, "request_irq failed, rc = %d\n", rc);
  506. else
  507. DP_VERBOSE(hwfn, (NETIF_MSG_INTR | QED_MSG_SP),
  508. "Requested slowpath %s\n",
  509. (int_mode == QED_INT_MODE_MSIX) ? "MSI-X" : "IRQ");
  510. return rc;
  511. }
  512. static void qed_slowpath_tasklet_flush(struct qed_hwfn *p_hwfn)
  513. {
  514. /* Calling the disable function will make sure that any
  515. * currently-running function is completed. The following call to the
  516. * enable function makes this sequence a flush-like operation.
  517. */
  518. if (p_hwfn->b_sp_dpc_enabled) {
  519. tasklet_disable(p_hwfn->sp_dpc);
  520. tasklet_enable(p_hwfn->sp_dpc);
  521. }
  522. }
  523. void qed_slowpath_irq_sync(struct qed_hwfn *p_hwfn)
  524. {
  525. struct qed_dev *cdev = p_hwfn->cdev;
  526. u8 id = p_hwfn->my_id;
  527. u32 int_mode;
  528. int_mode = cdev->int_params.out.int_mode;
  529. if (int_mode == QED_INT_MODE_MSIX)
  530. synchronize_irq(cdev->int_params.msix_table[id].vector);
  531. else
  532. synchronize_irq(cdev->pdev->irq);
  533. qed_slowpath_tasklet_flush(p_hwfn);
  534. }
  535. static void qed_slowpath_irq_free(struct qed_dev *cdev)
  536. {
  537. int i;
  538. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  539. for_each_hwfn(cdev, i) {
  540. if (!cdev->hwfns[i].b_int_requested)
  541. break;
  542. synchronize_irq(cdev->int_params.msix_table[i].vector);
  543. free_irq(cdev->int_params.msix_table[i].vector,
  544. cdev->hwfns[i].sp_dpc);
  545. }
  546. } else {
  547. if (QED_LEADING_HWFN(cdev)->b_int_requested)
  548. free_irq(cdev->pdev->irq, cdev);
  549. }
  550. qed_int_disable_post_isr_release(cdev);
  551. }
  552. static int qed_nic_stop(struct qed_dev *cdev)
  553. {
  554. int i, rc;
  555. rc = qed_hw_stop(cdev);
  556. for (i = 0; i < cdev->num_hwfns; i++) {
  557. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  558. if (p_hwfn->b_sp_dpc_enabled) {
  559. tasklet_disable(p_hwfn->sp_dpc);
  560. p_hwfn->b_sp_dpc_enabled = false;
  561. DP_VERBOSE(cdev, NETIF_MSG_IFDOWN,
  562. "Disabled sp taskelt [hwfn %d] at %p\n",
  563. i, p_hwfn->sp_dpc);
  564. }
  565. }
  566. qed_dbg_pf_exit(cdev);
  567. return rc;
  568. }
  569. static int qed_nic_setup(struct qed_dev *cdev)
  570. {
  571. int rc, i;
  572. /* Determine if interface is going to require LL2 */
  573. if (QED_LEADING_HWFN(cdev)->hw_info.personality != QED_PCI_ETH) {
  574. for (i = 0; i < cdev->num_hwfns; i++) {
  575. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  576. p_hwfn->using_ll2 = true;
  577. }
  578. }
  579. rc = qed_resc_alloc(cdev);
  580. if (rc)
  581. return rc;
  582. DP_INFO(cdev, "Allocated qed resources\n");
  583. qed_resc_setup(cdev);
  584. return rc;
  585. }
  586. static int qed_set_int_fp(struct qed_dev *cdev, u16 cnt)
  587. {
  588. int limit = 0;
  589. /* Mark the fastpath as free/used */
  590. cdev->int_params.fp_initialized = cnt ? true : false;
  591. if (cdev->int_params.out.int_mode != QED_INT_MODE_MSIX)
  592. limit = cdev->num_hwfns * 63;
  593. else if (cdev->int_params.fp_msix_cnt)
  594. limit = cdev->int_params.fp_msix_cnt;
  595. if (!limit)
  596. return -ENOMEM;
  597. return min_t(int, cnt, limit);
  598. }
  599. static int qed_get_int_fp(struct qed_dev *cdev, struct qed_int_info *info)
  600. {
  601. memset(info, 0, sizeof(struct qed_int_info));
  602. if (!cdev->int_params.fp_initialized) {
  603. DP_INFO(cdev,
  604. "Protocol driver requested interrupt information, but its support is not yet configured\n");
  605. return -EINVAL;
  606. }
  607. /* Need to expose only MSI-X information; Single IRQ is handled solely
  608. * by qed.
  609. */
  610. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  611. int msix_base = cdev->int_params.fp_msix_base;
  612. info->msix_cnt = cdev->int_params.fp_msix_cnt;
  613. info->msix = &cdev->int_params.msix_table[msix_base];
  614. }
  615. return 0;
  616. }
  617. static int qed_slowpath_setup_int(struct qed_dev *cdev,
  618. enum qed_int_mode int_mode)
  619. {
  620. struct qed_sb_cnt_info sb_cnt_info;
  621. int num_l2_queues = 0;
  622. int rc;
  623. int i;
  624. if ((int_mode == QED_INT_MODE_MSI) && (cdev->num_hwfns > 1)) {
  625. DP_NOTICE(cdev, "MSI mode is not supported for CMT devices\n");
  626. return -EINVAL;
  627. }
  628. memset(&cdev->int_params, 0, sizeof(struct qed_int_params));
  629. cdev->int_params.in.int_mode = int_mode;
  630. for_each_hwfn(cdev, i) {
  631. memset(&sb_cnt_info, 0, sizeof(sb_cnt_info));
  632. qed_int_get_num_sbs(&cdev->hwfns[i], &sb_cnt_info);
  633. cdev->int_params.in.num_vectors += sb_cnt_info.cnt;
  634. cdev->int_params.in.num_vectors++; /* slowpath */
  635. }
  636. /* We want a minimum of one slowpath and one fastpath vector per hwfn */
  637. cdev->int_params.in.min_msix_cnt = cdev->num_hwfns * 2;
  638. rc = qed_set_int_mode(cdev, false);
  639. if (rc) {
  640. DP_ERR(cdev, "qed_slowpath_setup_int ERR\n");
  641. return rc;
  642. }
  643. cdev->int_params.fp_msix_base = cdev->num_hwfns;
  644. cdev->int_params.fp_msix_cnt = cdev->int_params.out.num_vectors -
  645. cdev->num_hwfns;
  646. if (!IS_ENABLED(CONFIG_QED_RDMA) ||
  647. !QED_IS_RDMA_PERSONALITY(QED_LEADING_HWFN(cdev)))
  648. return 0;
  649. for_each_hwfn(cdev, i)
  650. num_l2_queues += FEAT_NUM(&cdev->hwfns[i], QED_PF_L2_QUE);
  651. DP_VERBOSE(cdev, QED_MSG_RDMA,
  652. "cdev->int_params.fp_msix_cnt=%d num_l2_queues=%d\n",
  653. cdev->int_params.fp_msix_cnt, num_l2_queues);
  654. if (cdev->int_params.fp_msix_cnt > num_l2_queues) {
  655. cdev->int_params.rdma_msix_cnt =
  656. (cdev->int_params.fp_msix_cnt - num_l2_queues)
  657. / cdev->num_hwfns;
  658. cdev->int_params.rdma_msix_base =
  659. cdev->int_params.fp_msix_base + num_l2_queues;
  660. cdev->int_params.fp_msix_cnt = num_l2_queues;
  661. } else {
  662. cdev->int_params.rdma_msix_cnt = 0;
  663. }
  664. DP_VERBOSE(cdev, QED_MSG_RDMA, "roce_msix_cnt=%d roce_msix_base=%d\n",
  665. cdev->int_params.rdma_msix_cnt,
  666. cdev->int_params.rdma_msix_base);
  667. return 0;
  668. }
  669. static int qed_slowpath_vf_setup_int(struct qed_dev *cdev)
  670. {
  671. int rc;
  672. memset(&cdev->int_params, 0, sizeof(struct qed_int_params));
  673. cdev->int_params.in.int_mode = QED_INT_MODE_MSIX;
  674. qed_vf_get_num_rxqs(QED_LEADING_HWFN(cdev),
  675. &cdev->int_params.in.num_vectors);
  676. if (cdev->num_hwfns > 1) {
  677. u8 vectors = 0;
  678. qed_vf_get_num_rxqs(&cdev->hwfns[1], &vectors);
  679. cdev->int_params.in.num_vectors += vectors;
  680. }
  681. /* We want a minimum of one fastpath vector per vf hwfn */
  682. cdev->int_params.in.min_msix_cnt = cdev->num_hwfns;
  683. rc = qed_set_int_mode(cdev, true);
  684. if (rc)
  685. return rc;
  686. cdev->int_params.fp_msix_base = 0;
  687. cdev->int_params.fp_msix_cnt = cdev->int_params.out.num_vectors;
  688. return 0;
  689. }
  690. u32 qed_unzip_data(struct qed_hwfn *p_hwfn, u32 input_len,
  691. u8 *input_buf, u32 max_size, u8 *unzip_buf)
  692. {
  693. int rc;
  694. p_hwfn->stream->next_in = input_buf;
  695. p_hwfn->stream->avail_in = input_len;
  696. p_hwfn->stream->next_out = unzip_buf;
  697. p_hwfn->stream->avail_out = max_size;
  698. rc = zlib_inflateInit2(p_hwfn->stream, MAX_WBITS);
  699. if (rc != Z_OK) {
  700. DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "zlib init failed, rc = %d\n",
  701. rc);
  702. return 0;
  703. }
  704. rc = zlib_inflate(p_hwfn->stream, Z_FINISH);
  705. zlib_inflateEnd(p_hwfn->stream);
  706. if (rc != Z_OK && rc != Z_STREAM_END) {
  707. DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "FW unzip error: %s, rc=%d\n",
  708. p_hwfn->stream->msg, rc);
  709. return 0;
  710. }
  711. return p_hwfn->stream->total_out / 4;
  712. }
  713. static int qed_alloc_stream_mem(struct qed_dev *cdev)
  714. {
  715. int i;
  716. void *workspace;
  717. for_each_hwfn(cdev, i) {
  718. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  719. p_hwfn->stream = kzalloc(sizeof(*p_hwfn->stream), GFP_KERNEL);
  720. if (!p_hwfn->stream)
  721. return -ENOMEM;
  722. workspace = vzalloc(zlib_inflate_workspacesize());
  723. if (!workspace)
  724. return -ENOMEM;
  725. p_hwfn->stream->workspace = workspace;
  726. }
  727. return 0;
  728. }
  729. static void qed_free_stream_mem(struct qed_dev *cdev)
  730. {
  731. int i;
  732. for_each_hwfn(cdev, i) {
  733. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  734. if (!p_hwfn->stream)
  735. return;
  736. vfree(p_hwfn->stream->workspace);
  737. kfree(p_hwfn->stream);
  738. }
  739. }
  740. static void qed_update_pf_params(struct qed_dev *cdev,
  741. struct qed_pf_params *params)
  742. {
  743. int i;
  744. if (IS_ENABLED(CONFIG_QED_RDMA)) {
  745. params->rdma_pf_params.num_qps = QED_ROCE_QPS;
  746. params->rdma_pf_params.min_dpis = QED_ROCE_DPIS;
  747. /* divide by 3 the MRs to avoid MF ILT overflow */
  748. params->rdma_pf_params.gl_pi = QED_ROCE_PROTOCOL_INDEX;
  749. }
  750. if (cdev->num_hwfns > 1 || IS_VF(cdev))
  751. params->eth_pf_params.num_arfs_filters = 0;
  752. /* In case we might support RDMA, don't allow qede to be greedy
  753. * with the L2 contexts. Allow for 64 queues [rx, tx, xdp] per hwfn.
  754. */
  755. if (QED_IS_RDMA_PERSONALITY(QED_LEADING_HWFN(cdev))) {
  756. u16 *num_cons;
  757. num_cons = &params->eth_pf_params.num_cons;
  758. *num_cons = min_t(u16, *num_cons, 192);
  759. }
  760. for (i = 0; i < cdev->num_hwfns; i++) {
  761. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  762. p_hwfn->pf_params = *params;
  763. }
  764. }
  765. static int qed_slowpath_start(struct qed_dev *cdev,
  766. struct qed_slowpath_params *params)
  767. {
  768. struct qed_drv_load_params drv_load_params;
  769. struct qed_hw_init_params hw_init_params;
  770. struct qed_mcp_drv_version drv_version;
  771. struct qed_tunnel_info tunn_info;
  772. const u8 *data = NULL;
  773. struct qed_hwfn *hwfn;
  774. struct qed_ptt *p_ptt;
  775. int rc = -EINVAL;
  776. if (qed_iov_wq_start(cdev))
  777. goto err;
  778. if (IS_PF(cdev)) {
  779. rc = request_firmware(&cdev->firmware, QED_FW_FILE_NAME,
  780. &cdev->pdev->dev);
  781. if (rc) {
  782. DP_NOTICE(cdev,
  783. "Failed to find fw file - /lib/firmware/%s\n",
  784. QED_FW_FILE_NAME);
  785. goto err;
  786. }
  787. if (cdev->num_hwfns == 1) {
  788. p_ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
  789. if (p_ptt) {
  790. QED_LEADING_HWFN(cdev)->p_arfs_ptt = p_ptt;
  791. } else {
  792. DP_NOTICE(cdev,
  793. "Failed to acquire PTT for aRFS\n");
  794. goto err;
  795. }
  796. }
  797. }
  798. cdev->rx_coalesce_usecs = QED_DEFAULT_RX_USECS;
  799. rc = qed_nic_setup(cdev);
  800. if (rc)
  801. goto err;
  802. if (IS_PF(cdev))
  803. rc = qed_slowpath_setup_int(cdev, params->int_mode);
  804. else
  805. rc = qed_slowpath_vf_setup_int(cdev);
  806. if (rc)
  807. goto err1;
  808. if (IS_PF(cdev)) {
  809. /* Allocate stream for unzipping */
  810. rc = qed_alloc_stream_mem(cdev);
  811. if (rc)
  812. goto err2;
  813. /* First Dword used to differentiate between various sources */
  814. data = cdev->firmware->data + sizeof(u32);
  815. qed_dbg_pf_init(cdev);
  816. }
  817. /* Start the slowpath */
  818. memset(&hw_init_params, 0, sizeof(hw_init_params));
  819. memset(&tunn_info, 0, sizeof(tunn_info));
  820. tunn_info.vxlan.b_mode_enabled = true;
  821. tunn_info.l2_gre.b_mode_enabled = true;
  822. tunn_info.ip_gre.b_mode_enabled = true;
  823. tunn_info.l2_geneve.b_mode_enabled = true;
  824. tunn_info.ip_geneve.b_mode_enabled = true;
  825. tunn_info.vxlan.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  826. tunn_info.l2_gre.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  827. tunn_info.ip_gre.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  828. tunn_info.l2_geneve.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  829. tunn_info.ip_geneve.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  830. hw_init_params.p_tunn = &tunn_info;
  831. hw_init_params.b_hw_start = true;
  832. hw_init_params.int_mode = cdev->int_params.out.int_mode;
  833. hw_init_params.allow_npar_tx_switch = true;
  834. hw_init_params.bin_fw_data = data;
  835. memset(&drv_load_params, 0, sizeof(drv_load_params));
  836. drv_load_params.is_crash_kernel = is_kdump_kernel();
  837. drv_load_params.mfw_timeout_val = QED_LOAD_REQ_LOCK_TO_DEFAULT;
  838. drv_load_params.avoid_eng_reset = false;
  839. drv_load_params.override_force_load = QED_OVERRIDE_FORCE_LOAD_NONE;
  840. hw_init_params.p_drv_load_params = &drv_load_params;
  841. rc = qed_hw_init(cdev, &hw_init_params);
  842. if (rc)
  843. goto err2;
  844. DP_INFO(cdev,
  845. "HW initialization and function start completed successfully\n");
  846. if (IS_PF(cdev)) {
  847. cdev->tunn_feature_mask = (BIT(QED_MODE_VXLAN_TUNN) |
  848. BIT(QED_MODE_L2GENEVE_TUNN) |
  849. BIT(QED_MODE_IPGENEVE_TUNN) |
  850. BIT(QED_MODE_L2GRE_TUNN) |
  851. BIT(QED_MODE_IPGRE_TUNN));
  852. }
  853. /* Allocate LL2 interface if needed */
  854. if (QED_LEADING_HWFN(cdev)->using_ll2) {
  855. rc = qed_ll2_alloc_if(cdev);
  856. if (rc)
  857. goto err3;
  858. }
  859. if (IS_PF(cdev)) {
  860. hwfn = QED_LEADING_HWFN(cdev);
  861. drv_version.version = (params->drv_major << 24) |
  862. (params->drv_minor << 16) |
  863. (params->drv_rev << 8) |
  864. (params->drv_eng);
  865. strlcpy(drv_version.name, params->name,
  866. MCP_DRV_VER_STR_SIZE - 4);
  867. rc = qed_mcp_send_drv_version(hwfn, hwfn->p_main_ptt,
  868. &drv_version);
  869. if (rc) {
  870. DP_NOTICE(cdev, "Failed sending drv version command\n");
  871. return rc;
  872. }
  873. }
  874. qed_reset_vport_stats(cdev);
  875. return 0;
  876. err3:
  877. qed_hw_stop(cdev);
  878. err2:
  879. qed_hw_timers_stop_all(cdev);
  880. if (IS_PF(cdev))
  881. qed_slowpath_irq_free(cdev);
  882. qed_free_stream_mem(cdev);
  883. qed_disable_msix(cdev);
  884. err1:
  885. qed_resc_free(cdev);
  886. err:
  887. if (IS_PF(cdev))
  888. release_firmware(cdev->firmware);
  889. if (IS_PF(cdev) && (cdev->num_hwfns == 1) &&
  890. QED_LEADING_HWFN(cdev)->p_arfs_ptt)
  891. qed_ptt_release(QED_LEADING_HWFN(cdev),
  892. QED_LEADING_HWFN(cdev)->p_arfs_ptt);
  893. qed_iov_wq_stop(cdev, false);
  894. return rc;
  895. }
  896. static int qed_slowpath_stop(struct qed_dev *cdev)
  897. {
  898. if (!cdev)
  899. return -ENODEV;
  900. qed_ll2_dealloc_if(cdev);
  901. if (IS_PF(cdev)) {
  902. if (cdev->num_hwfns == 1)
  903. qed_ptt_release(QED_LEADING_HWFN(cdev),
  904. QED_LEADING_HWFN(cdev)->p_arfs_ptt);
  905. qed_free_stream_mem(cdev);
  906. if (IS_QED_ETH_IF(cdev))
  907. qed_sriov_disable(cdev, true);
  908. }
  909. qed_nic_stop(cdev);
  910. if (IS_PF(cdev))
  911. qed_slowpath_irq_free(cdev);
  912. qed_disable_msix(cdev);
  913. qed_resc_free(cdev);
  914. qed_iov_wq_stop(cdev, true);
  915. if (IS_PF(cdev))
  916. release_firmware(cdev->firmware);
  917. return 0;
  918. }
  919. static void qed_set_name(struct qed_dev *cdev, char name[NAME_SIZE])
  920. {
  921. int i;
  922. memcpy(cdev->name, name, NAME_SIZE);
  923. for_each_hwfn(cdev, i)
  924. snprintf(cdev->hwfns[i].name, NAME_SIZE, "%s-%d", name, i);
  925. }
  926. static u32 qed_sb_init(struct qed_dev *cdev,
  927. struct qed_sb_info *sb_info,
  928. void *sb_virt_addr,
  929. dma_addr_t sb_phy_addr, u16 sb_id,
  930. enum qed_sb_type type)
  931. {
  932. struct qed_hwfn *p_hwfn;
  933. struct qed_ptt *p_ptt;
  934. int hwfn_index;
  935. u16 rel_sb_id;
  936. u8 n_hwfns;
  937. u32 rc;
  938. /* RoCE uses single engine and CMT uses two engines. When using both
  939. * we force only a single engine. Storage uses only engine 0 too.
  940. */
  941. if (type == QED_SB_TYPE_L2_QUEUE)
  942. n_hwfns = cdev->num_hwfns;
  943. else
  944. n_hwfns = 1;
  945. hwfn_index = sb_id % n_hwfns;
  946. p_hwfn = &cdev->hwfns[hwfn_index];
  947. rel_sb_id = sb_id / n_hwfns;
  948. DP_VERBOSE(cdev, NETIF_MSG_INTR,
  949. "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
  950. hwfn_index, rel_sb_id, sb_id);
  951. if (IS_PF(p_hwfn->cdev)) {
  952. p_ptt = qed_ptt_acquire(p_hwfn);
  953. if (!p_ptt)
  954. return -EBUSY;
  955. rc = qed_int_sb_init(p_hwfn, p_ptt, sb_info, sb_virt_addr,
  956. sb_phy_addr, rel_sb_id);
  957. qed_ptt_release(p_hwfn, p_ptt);
  958. } else {
  959. rc = qed_int_sb_init(p_hwfn, NULL, sb_info, sb_virt_addr,
  960. sb_phy_addr, rel_sb_id);
  961. }
  962. return rc;
  963. }
  964. static u32 qed_sb_release(struct qed_dev *cdev,
  965. struct qed_sb_info *sb_info, u16 sb_id)
  966. {
  967. struct qed_hwfn *p_hwfn;
  968. int hwfn_index;
  969. u16 rel_sb_id;
  970. u32 rc;
  971. hwfn_index = sb_id % cdev->num_hwfns;
  972. p_hwfn = &cdev->hwfns[hwfn_index];
  973. rel_sb_id = sb_id / cdev->num_hwfns;
  974. DP_VERBOSE(cdev, NETIF_MSG_INTR,
  975. "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
  976. hwfn_index, rel_sb_id, sb_id);
  977. rc = qed_int_sb_release(p_hwfn, sb_info, rel_sb_id);
  978. return rc;
  979. }
  980. static bool qed_can_link_change(struct qed_dev *cdev)
  981. {
  982. return true;
  983. }
  984. static int qed_set_link(struct qed_dev *cdev, struct qed_link_params *params)
  985. {
  986. struct qed_hwfn *hwfn;
  987. struct qed_mcp_link_params *link_params;
  988. struct qed_ptt *ptt;
  989. int rc;
  990. if (!cdev)
  991. return -ENODEV;
  992. /* The link should be set only once per PF */
  993. hwfn = &cdev->hwfns[0];
  994. /* When VF wants to set link, force it to read the bulletin instead.
  995. * This mimics the PF behavior, where a noitification [both immediate
  996. * and possible later] would be generated when changing properties.
  997. */
  998. if (IS_VF(cdev)) {
  999. qed_schedule_iov(hwfn, QED_IOV_WQ_VF_FORCE_LINK_QUERY_FLAG);
  1000. return 0;
  1001. }
  1002. ptt = qed_ptt_acquire(hwfn);
  1003. if (!ptt)
  1004. return -EBUSY;
  1005. link_params = qed_mcp_get_link_params(hwfn);
  1006. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_AUTONEG)
  1007. link_params->speed.autoneg = params->autoneg;
  1008. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_ADV_SPEEDS) {
  1009. link_params->speed.advertised_speeds = 0;
  1010. if ((params->adv_speeds & QED_LM_1000baseT_Half_BIT) ||
  1011. (params->adv_speeds & QED_LM_1000baseT_Full_BIT))
  1012. link_params->speed.advertised_speeds |=
  1013. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G;
  1014. if (params->adv_speeds & QED_LM_10000baseKR_Full_BIT)
  1015. link_params->speed.advertised_speeds |=
  1016. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G;
  1017. if (params->adv_speeds & QED_LM_25000baseKR_Full_BIT)
  1018. link_params->speed.advertised_speeds |=
  1019. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_25G;
  1020. if (params->adv_speeds & QED_LM_40000baseLR4_Full_BIT)
  1021. link_params->speed.advertised_speeds |=
  1022. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G;
  1023. if (params->adv_speeds & QED_LM_50000baseKR2_Full_BIT)
  1024. link_params->speed.advertised_speeds |=
  1025. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G;
  1026. if (params->adv_speeds & QED_LM_100000baseKR4_Full_BIT)
  1027. link_params->speed.advertised_speeds |=
  1028. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_BB_100G;
  1029. }
  1030. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_FORCED_SPEED)
  1031. link_params->speed.forced_speed = params->forced_speed;
  1032. if (params->override_flags & QED_LINK_OVERRIDE_PAUSE_CONFIG) {
  1033. if (params->pause_config & QED_LINK_PAUSE_AUTONEG_ENABLE)
  1034. link_params->pause.autoneg = true;
  1035. else
  1036. link_params->pause.autoneg = false;
  1037. if (params->pause_config & QED_LINK_PAUSE_RX_ENABLE)
  1038. link_params->pause.forced_rx = true;
  1039. else
  1040. link_params->pause.forced_rx = false;
  1041. if (params->pause_config & QED_LINK_PAUSE_TX_ENABLE)
  1042. link_params->pause.forced_tx = true;
  1043. else
  1044. link_params->pause.forced_tx = false;
  1045. }
  1046. if (params->override_flags & QED_LINK_OVERRIDE_LOOPBACK_MODE) {
  1047. switch (params->loopback_mode) {
  1048. case QED_LINK_LOOPBACK_INT_PHY:
  1049. link_params->loopback_mode = ETH_LOOPBACK_INT_PHY;
  1050. break;
  1051. case QED_LINK_LOOPBACK_EXT_PHY:
  1052. link_params->loopback_mode = ETH_LOOPBACK_EXT_PHY;
  1053. break;
  1054. case QED_LINK_LOOPBACK_EXT:
  1055. link_params->loopback_mode = ETH_LOOPBACK_EXT;
  1056. break;
  1057. case QED_LINK_LOOPBACK_MAC:
  1058. link_params->loopback_mode = ETH_LOOPBACK_MAC;
  1059. break;
  1060. default:
  1061. link_params->loopback_mode = ETH_LOOPBACK_NONE;
  1062. break;
  1063. }
  1064. }
  1065. if (params->override_flags & QED_LINK_OVERRIDE_EEE_CONFIG)
  1066. memcpy(&link_params->eee, &params->eee,
  1067. sizeof(link_params->eee));
  1068. rc = qed_mcp_set_link(hwfn, ptt, params->link_up);
  1069. qed_ptt_release(hwfn, ptt);
  1070. return rc;
  1071. }
  1072. static int qed_get_port_type(u32 media_type)
  1073. {
  1074. int port_type;
  1075. switch (media_type) {
  1076. case MEDIA_SFPP_10G_FIBER:
  1077. case MEDIA_SFP_1G_FIBER:
  1078. case MEDIA_XFP_FIBER:
  1079. case MEDIA_MODULE_FIBER:
  1080. case MEDIA_KR:
  1081. port_type = PORT_FIBRE;
  1082. break;
  1083. case MEDIA_DA_TWINAX:
  1084. port_type = PORT_DA;
  1085. break;
  1086. case MEDIA_BASE_T:
  1087. port_type = PORT_TP;
  1088. break;
  1089. case MEDIA_NOT_PRESENT:
  1090. port_type = PORT_NONE;
  1091. break;
  1092. case MEDIA_UNSPECIFIED:
  1093. default:
  1094. port_type = PORT_OTHER;
  1095. break;
  1096. }
  1097. return port_type;
  1098. }
  1099. static int qed_get_link_data(struct qed_hwfn *hwfn,
  1100. struct qed_mcp_link_params *params,
  1101. struct qed_mcp_link_state *link,
  1102. struct qed_mcp_link_capabilities *link_caps)
  1103. {
  1104. void *p;
  1105. if (!IS_PF(hwfn->cdev)) {
  1106. qed_vf_get_link_params(hwfn, params);
  1107. qed_vf_get_link_state(hwfn, link);
  1108. qed_vf_get_link_caps(hwfn, link_caps);
  1109. return 0;
  1110. }
  1111. p = qed_mcp_get_link_params(hwfn);
  1112. if (!p)
  1113. return -ENXIO;
  1114. memcpy(params, p, sizeof(*params));
  1115. p = qed_mcp_get_link_state(hwfn);
  1116. if (!p)
  1117. return -ENXIO;
  1118. memcpy(link, p, sizeof(*link));
  1119. p = qed_mcp_get_link_capabilities(hwfn);
  1120. if (!p)
  1121. return -ENXIO;
  1122. memcpy(link_caps, p, sizeof(*link_caps));
  1123. return 0;
  1124. }
  1125. static void qed_fill_link(struct qed_hwfn *hwfn,
  1126. struct qed_link_output *if_link)
  1127. {
  1128. struct qed_mcp_link_params params;
  1129. struct qed_mcp_link_state link;
  1130. struct qed_mcp_link_capabilities link_caps;
  1131. u32 media_type;
  1132. memset(if_link, 0, sizeof(*if_link));
  1133. /* Prepare source inputs */
  1134. if (qed_get_link_data(hwfn, &params, &link, &link_caps)) {
  1135. dev_warn(&hwfn->cdev->pdev->dev, "no link data available\n");
  1136. return;
  1137. }
  1138. /* Set the link parameters to pass to protocol driver */
  1139. if (link.link_up)
  1140. if_link->link_up = true;
  1141. /* TODO - at the moment assume supported and advertised speed equal */
  1142. if_link->supported_caps = QED_LM_FIBRE_BIT;
  1143. if (link_caps.default_speed_autoneg)
  1144. if_link->supported_caps |= QED_LM_Autoneg_BIT;
  1145. if (params.pause.autoneg ||
  1146. (params.pause.forced_rx && params.pause.forced_tx))
  1147. if_link->supported_caps |= QED_LM_Asym_Pause_BIT;
  1148. if (params.pause.autoneg || params.pause.forced_rx ||
  1149. params.pause.forced_tx)
  1150. if_link->supported_caps |= QED_LM_Pause_BIT;
  1151. if_link->advertised_caps = if_link->supported_caps;
  1152. if (params.speed.autoneg)
  1153. if_link->advertised_caps |= QED_LM_Autoneg_BIT;
  1154. else
  1155. if_link->advertised_caps &= ~QED_LM_Autoneg_BIT;
  1156. if (params.speed.advertised_speeds &
  1157. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G)
  1158. if_link->advertised_caps |= QED_LM_1000baseT_Half_BIT |
  1159. QED_LM_1000baseT_Full_BIT;
  1160. if (params.speed.advertised_speeds &
  1161. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G)
  1162. if_link->advertised_caps |= QED_LM_10000baseKR_Full_BIT;
  1163. if (params.speed.advertised_speeds &
  1164. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_25G)
  1165. if_link->advertised_caps |= QED_LM_25000baseKR_Full_BIT;
  1166. if (params.speed.advertised_speeds &
  1167. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G)
  1168. if_link->advertised_caps |= QED_LM_40000baseLR4_Full_BIT;
  1169. if (params.speed.advertised_speeds &
  1170. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G)
  1171. if_link->advertised_caps |= QED_LM_50000baseKR2_Full_BIT;
  1172. if (params.speed.advertised_speeds &
  1173. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_BB_100G)
  1174. if_link->advertised_caps |= QED_LM_100000baseKR4_Full_BIT;
  1175. if (link_caps.speed_capabilities &
  1176. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G)
  1177. if_link->supported_caps |= QED_LM_1000baseT_Half_BIT |
  1178. QED_LM_1000baseT_Full_BIT;
  1179. if (link_caps.speed_capabilities &
  1180. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G)
  1181. if_link->supported_caps |= QED_LM_10000baseKR_Full_BIT;
  1182. if (link_caps.speed_capabilities &
  1183. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_25G)
  1184. if_link->supported_caps |= QED_LM_25000baseKR_Full_BIT;
  1185. if (link_caps.speed_capabilities &
  1186. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G)
  1187. if_link->supported_caps |= QED_LM_40000baseLR4_Full_BIT;
  1188. if (link_caps.speed_capabilities &
  1189. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G)
  1190. if_link->supported_caps |= QED_LM_50000baseKR2_Full_BIT;
  1191. if (link_caps.speed_capabilities &
  1192. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_BB_100G)
  1193. if_link->supported_caps |= QED_LM_100000baseKR4_Full_BIT;
  1194. if (link.link_up)
  1195. if_link->speed = link.speed;
  1196. /* TODO - fill duplex properly */
  1197. if_link->duplex = DUPLEX_FULL;
  1198. qed_mcp_get_media_type(hwfn->cdev, &media_type);
  1199. if_link->port = qed_get_port_type(media_type);
  1200. if_link->autoneg = params.speed.autoneg;
  1201. if (params.pause.autoneg)
  1202. if_link->pause_config |= QED_LINK_PAUSE_AUTONEG_ENABLE;
  1203. if (params.pause.forced_rx)
  1204. if_link->pause_config |= QED_LINK_PAUSE_RX_ENABLE;
  1205. if (params.pause.forced_tx)
  1206. if_link->pause_config |= QED_LINK_PAUSE_TX_ENABLE;
  1207. /* Link partner capabilities */
  1208. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_1G_HD)
  1209. if_link->lp_caps |= QED_LM_1000baseT_Half_BIT;
  1210. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_1G_FD)
  1211. if_link->lp_caps |= QED_LM_1000baseT_Full_BIT;
  1212. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_10G)
  1213. if_link->lp_caps |= QED_LM_10000baseKR_Full_BIT;
  1214. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_25G)
  1215. if_link->lp_caps |= QED_LM_25000baseKR_Full_BIT;
  1216. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_40G)
  1217. if_link->lp_caps |= QED_LM_40000baseLR4_Full_BIT;
  1218. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_50G)
  1219. if_link->lp_caps |= QED_LM_50000baseKR2_Full_BIT;
  1220. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_100G)
  1221. if_link->lp_caps |= QED_LM_100000baseKR4_Full_BIT;
  1222. if (link.an_complete)
  1223. if_link->lp_caps |= QED_LM_Autoneg_BIT;
  1224. if (link.partner_adv_pause)
  1225. if_link->lp_caps |= QED_LM_Pause_BIT;
  1226. if (link.partner_adv_pause == QED_LINK_PARTNER_ASYMMETRIC_PAUSE ||
  1227. link.partner_adv_pause == QED_LINK_PARTNER_BOTH_PAUSE)
  1228. if_link->lp_caps |= QED_LM_Asym_Pause_BIT;
  1229. if (link_caps.default_eee == QED_MCP_EEE_UNSUPPORTED) {
  1230. if_link->eee_supported = false;
  1231. } else {
  1232. if_link->eee_supported = true;
  1233. if_link->eee_active = link.eee_active;
  1234. if_link->sup_caps = link_caps.eee_speed_caps;
  1235. /* MFW clears adv_caps on eee disable; use configured value */
  1236. if_link->eee.adv_caps = link.eee_adv_caps ? link.eee_adv_caps :
  1237. params.eee.adv_caps;
  1238. if_link->eee.lp_adv_caps = link.eee_lp_adv_caps;
  1239. if_link->eee.enable = params.eee.enable;
  1240. if_link->eee.tx_lpi_enable = params.eee.tx_lpi_enable;
  1241. if_link->eee.tx_lpi_timer = params.eee.tx_lpi_timer;
  1242. }
  1243. }
  1244. static void qed_get_current_link(struct qed_dev *cdev,
  1245. struct qed_link_output *if_link)
  1246. {
  1247. int i;
  1248. qed_fill_link(&cdev->hwfns[0], if_link);
  1249. for_each_hwfn(cdev, i)
  1250. qed_inform_vf_link_state(&cdev->hwfns[i]);
  1251. }
  1252. void qed_link_update(struct qed_hwfn *hwfn)
  1253. {
  1254. void *cookie = hwfn->cdev->ops_cookie;
  1255. struct qed_common_cb_ops *op = hwfn->cdev->protocol_ops.common;
  1256. struct qed_link_output if_link;
  1257. qed_fill_link(hwfn, &if_link);
  1258. qed_inform_vf_link_state(hwfn);
  1259. if (IS_LEAD_HWFN(hwfn) && cookie)
  1260. op->link_update(cookie, &if_link);
  1261. }
  1262. static int qed_drain(struct qed_dev *cdev)
  1263. {
  1264. struct qed_hwfn *hwfn;
  1265. struct qed_ptt *ptt;
  1266. int i, rc;
  1267. if (IS_VF(cdev))
  1268. return 0;
  1269. for_each_hwfn(cdev, i) {
  1270. hwfn = &cdev->hwfns[i];
  1271. ptt = qed_ptt_acquire(hwfn);
  1272. if (!ptt) {
  1273. DP_NOTICE(hwfn, "Failed to drain NIG; No PTT\n");
  1274. return -EBUSY;
  1275. }
  1276. rc = qed_mcp_drain(hwfn, ptt);
  1277. if (rc)
  1278. return rc;
  1279. qed_ptt_release(hwfn, ptt);
  1280. }
  1281. return 0;
  1282. }
  1283. static u32 qed_nvm_flash_image_access_crc(struct qed_dev *cdev,
  1284. struct qed_nvm_image_att *nvm_image,
  1285. u32 *crc)
  1286. {
  1287. u8 *buf = NULL;
  1288. int rc, j;
  1289. u32 val;
  1290. /* Allocate a buffer for holding the nvram image */
  1291. buf = kzalloc(nvm_image->length, GFP_KERNEL);
  1292. if (!buf)
  1293. return -ENOMEM;
  1294. /* Read image into buffer */
  1295. rc = qed_mcp_nvm_read(cdev, nvm_image->start_addr,
  1296. buf, nvm_image->length);
  1297. if (rc) {
  1298. DP_ERR(cdev, "Failed reading image from nvm\n");
  1299. goto out;
  1300. }
  1301. /* Convert the buffer into big-endian format (excluding the
  1302. * closing 4 bytes of CRC).
  1303. */
  1304. for (j = 0; j < nvm_image->length - 4; j += 4) {
  1305. val = cpu_to_be32(*(u32 *)&buf[j]);
  1306. *(u32 *)&buf[j] = val;
  1307. }
  1308. /* Calc CRC for the "actual" image buffer, i.e. not including
  1309. * the last 4 CRC bytes.
  1310. */
  1311. *crc = (~cpu_to_be32(crc32(0xffffffff, buf, nvm_image->length - 4)));
  1312. out:
  1313. kfree(buf);
  1314. return rc;
  1315. }
  1316. /* Binary file format -
  1317. * /----------------------------------------------------------------------\
  1318. * 0B | 0x4 [command index] |
  1319. * 4B | image_type | Options | Number of register settings |
  1320. * 8B | Value |
  1321. * 12B | Mask |
  1322. * 16B | Offset |
  1323. * \----------------------------------------------------------------------/
  1324. * There can be several Value-Mask-Offset sets as specified by 'Number of...'.
  1325. * Options - 0'b - Calculate & Update CRC for image
  1326. */
  1327. static int qed_nvm_flash_image_access(struct qed_dev *cdev, const u8 **data,
  1328. bool *check_resp)
  1329. {
  1330. struct qed_nvm_image_att nvm_image;
  1331. struct qed_hwfn *p_hwfn;
  1332. bool is_crc = false;
  1333. u32 image_type;
  1334. int rc = 0, i;
  1335. u16 len;
  1336. *data += 4;
  1337. image_type = **data;
  1338. p_hwfn = QED_LEADING_HWFN(cdev);
  1339. for (i = 0; i < p_hwfn->nvm_info.num_images; i++)
  1340. if (image_type == p_hwfn->nvm_info.image_att[i].image_type)
  1341. break;
  1342. if (i == p_hwfn->nvm_info.num_images) {
  1343. DP_ERR(cdev, "Failed to find nvram image of type %08x\n",
  1344. image_type);
  1345. return -ENOENT;
  1346. }
  1347. nvm_image.start_addr = p_hwfn->nvm_info.image_att[i].nvm_start_addr;
  1348. nvm_image.length = p_hwfn->nvm_info.image_att[i].len;
  1349. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1350. "Read image %02x; type = %08x; NVM [%08x,...,%08x]\n",
  1351. **data, image_type, nvm_image.start_addr,
  1352. nvm_image.start_addr + nvm_image.length - 1);
  1353. (*data)++;
  1354. is_crc = !!(**data & BIT(0));
  1355. (*data)++;
  1356. len = *((u16 *)*data);
  1357. *data += 2;
  1358. if (is_crc) {
  1359. u32 crc = 0;
  1360. rc = qed_nvm_flash_image_access_crc(cdev, &nvm_image, &crc);
  1361. if (rc) {
  1362. DP_ERR(cdev, "Failed calculating CRC, rc = %d\n", rc);
  1363. goto exit;
  1364. }
  1365. rc = qed_mcp_nvm_write(cdev, QED_NVM_WRITE_NVRAM,
  1366. (nvm_image.start_addr +
  1367. nvm_image.length - 4), (u8 *)&crc, 4);
  1368. if (rc)
  1369. DP_ERR(cdev, "Failed writing to %08x, rc = %d\n",
  1370. nvm_image.start_addr + nvm_image.length - 4, rc);
  1371. goto exit;
  1372. }
  1373. /* Iterate over the values for setting */
  1374. while (len) {
  1375. u32 offset, mask, value, cur_value;
  1376. u8 buf[4];
  1377. value = *((u32 *)*data);
  1378. *data += 4;
  1379. mask = *((u32 *)*data);
  1380. *data += 4;
  1381. offset = *((u32 *)*data);
  1382. *data += 4;
  1383. rc = qed_mcp_nvm_read(cdev, nvm_image.start_addr + offset, buf,
  1384. 4);
  1385. if (rc) {
  1386. DP_ERR(cdev, "Failed reading from %08x\n",
  1387. nvm_image.start_addr + offset);
  1388. goto exit;
  1389. }
  1390. cur_value = le32_to_cpu(*((__le32 *)buf));
  1391. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1392. "NVM %08x: %08x -> %08x [Value %08x Mask %08x]\n",
  1393. nvm_image.start_addr + offset, cur_value,
  1394. (cur_value & ~mask) | (value & mask), value, mask);
  1395. value = (value & mask) | (cur_value & ~mask);
  1396. rc = qed_mcp_nvm_write(cdev, QED_NVM_WRITE_NVRAM,
  1397. nvm_image.start_addr + offset,
  1398. (u8 *)&value, 4);
  1399. if (rc) {
  1400. DP_ERR(cdev, "Failed writing to %08x\n",
  1401. nvm_image.start_addr + offset);
  1402. goto exit;
  1403. }
  1404. len--;
  1405. }
  1406. exit:
  1407. return rc;
  1408. }
  1409. /* Binary file format -
  1410. * /----------------------------------------------------------------------\
  1411. * 0B | 0x3 [command index] |
  1412. * 4B | b'0: check_response? | b'1-31 reserved |
  1413. * 8B | File-type | reserved |
  1414. * \----------------------------------------------------------------------/
  1415. * Start a new file of the provided type
  1416. */
  1417. static int qed_nvm_flash_image_file_start(struct qed_dev *cdev,
  1418. const u8 **data, bool *check_resp)
  1419. {
  1420. int rc;
  1421. *data += 4;
  1422. *check_resp = !!(**data & BIT(0));
  1423. *data += 4;
  1424. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1425. "About to start a new file of type %02x\n", **data);
  1426. rc = qed_mcp_nvm_put_file_begin(cdev, **data);
  1427. *data += 4;
  1428. return rc;
  1429. }
  1430. /* Binary file format -
  1431. * /----------------------------------------------------------------------\
  1432. * 0B | 0x2 [command index] |
  1433. * 4B | Length in bytes |
  1434. * 8B | b'0: check_response? | b'1-31 reserved |
  1435. * 12B | Offset in bytes |
  1436. * 16B | Data ... |
  1437. * \----------------------------------------------------------------------/
  1438. * Write data as part of a file that was previously started. Data should be
  1439. * of length equal to that provided in the message
  1440. */
  1441. static int qed_nvm_flash_image_file_data(struct qed_dev *cdev,
  1442. const u8 **data, bool *check_resp)
  1443. {
  1444. u32 offset, len;
  1445. int rc;
  1446. *data += 4;
  1447. len = *((u32 *)(*data));
  1448. *data += 4;
  1449. *check_resp = !!(**data & BIT(0));
  1450. *data += 4;
  1451. offset = *((u32 *)(*data));
  1452. *data += 4;
  1453. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1454. "About to write File-data: %08x bytes to offset %08x\n",
  1455. len, offset);
  1456. rc = qed_mcp_nvm_write(cdev, QED_PUT_FILE_DATA, offset,
  1457. (char *)(*data), len);
  1458. *data += len;
  1459. return rc;
  1460. }
  1461. /* Binary file format [General header] -
  1462. * /----------------------------------------------------------------------\
  1463. * 0B | QED_NVM_SIGNATURE |
  1464. * 4B | Length in bytes |
  1465. * 8B | Highest command in this batchfile | Reserved |
  1466. * \----------------------------------------------------------------------/
  1467. */
  1468. static int qed_nvm_flash_image_validate(struct qed_dev *cdev,
  1469. const struct firmware *image,
  1470. const u8 **data)
  1471. {
  1472. u32 signature, len;
  1473. /* Check minimum size */
  1474. if (image->size < 12) {
  1475. DP_ERR(cdev, "Image is too short [%08x]\n", (u32)image->size);
  1476. return -EINVAL;
  1477. }
  1478. /* Check signature */
  1479. signature = *((u32 *)(*data));
  1480. if (signature != QED_NVM_SIGNATURE) {
  1481. DP_ERR(cdev, "Wrong signature '%08x'\n", signature);
  1482. return -EINVAL;
  1483. }
  1484. *data += 4;
  1485. /* Validate internal size equals the image-size */
  1486. len = *((u32 *)(*data));
  1487. if (len != image->size) {
  1488. DP_ERR(cdev, "Size mismatch: internal = %08x image = %08x\n",
  1489. len, (u32)image->size);
  1490. return -EINVAL;
  1491. }
  1492. *data += 4;
  1493. /* Make sure driver familiar with all commands necessary for this */
  1494. if (*((u16 *)(*data)) >= QED_NVM_FLASH_CMD_NVM_MAX) {
  1495. DP_ERR(cdev, "File contains unsupported commands [Need %04x]\n",
  1496. *((u16 *)(*data)));
  1497. return -EINVAL;
  1498. }
  1499. *data += 4;
  1500. return 0;
  1501. }
  1502. static int qed_nvm_flash(struct qed_dev *cdev, const char *name)
  1503. {
  1504. const struct firmware *image;
  1505. const u8 *data, *data_end;
  1506. u32 cmd_type;
  1507. int rc;
  1508. rc = request_firmware(&image, name, &cdev->pdev->dev);
  1509. if (rc) {
  1510. DP_ERR(cdev, "Failed to find '%s'\n", name);
  1511. return rc;
  1512. }
  1513. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1514. "Flashing '%s' - firmware's data at %p, size is %08x\n",
  1515. name, image->data, (u32)image->size);
  1516. data = image->data;
  1517. data_end = data + image->size;
  1518. rc = qed_nvm_flash_image_validate(cdev, image, &data);
  1519. if (rc)
  1520. goto exit;
  1521. while (data < data_end) {
  1522. bool check_resp = false;
  1523. /* Parse the actual command */
  1524. cmd_type = *((u32 *)data);
  1525. switch (cmd_type) {
  1526. case QED_NVM_FLASH_CMD_FILE_DATA:
  1527. rc = qed_nvm_flash_image_file_data(cdev, &data,
  1528. &check_resp);
  1529. break;
  1530. case QED_NVM_FLASH_CMD_FILE_START:
  1531. rc = qed_nvm_flash_image_file_start(cdev, &data,
  1532. &check_resp);
  1533. break;
  1534. case QED_NVM_FLASH_CMD_NVM_CHANGE:
  1535. rc = qed_nvm_flash_image_access(cdev, &data,
  1536. &check_resp);
  1537. break;
  1538. default:
  1539. DP_ERR(cdev, "Unknown command %08x\n", cmd_type);
  1540. rc = -EINVAL;
  1541. goto exit;
  1542. }
  1543. if (rc) {
  1544. DP_ERR(cdev, "Command %08x failed\n", cmd_type);
  1545. goto exit;
  1546. }
  1547. /* Check response if needed */
  1548. if (check_resp) {
  1549. u32 mcp_response = 0;
  1550. if (qed_mcp_nvm_resp(cdev, (u8 *)&mcp_response)) {
  1551. DP_ERR(cdev, "Failed getting MCP response\n");
  1552. rc = -EINVAL;
  1553. goto exit;
  1554. }
  1555. switch (mcp_response & FW_MSG_CODE_MASK) {
  1556. case FW_MSG_CODE_OK:
  1557. case FW_MSG_CODE_NVM_OK:
  1558. case FW_MSG_CODE_NVM_PUT_FILE_FINISH_OK:
  1559. case FW_MSG_CODE_PHY_OK:
  1560. break;
  1561. default:
  1562. DP_ERR(cdev, "MFW returns error: %08x\n",
  1563. mcp_response);
  1564. rc = -EINVAL;
  1565. goto exit;
  1566. }
  1567. }
  1568. }
  1569. exit:
  1570. release_firmware(image);
  1571. return rc;
  1572. }
  1573. static int qed_nvm_get_image(struct qed_dev *cdev, enum qed_nvm_images type,
  1574. u8 *buf, u16 len)
  1575. {
  1576. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1577. struct qed_ptt *ptt = qed_ptt_acquire(hwfn);
  1578. int rc;
  1579. if (!ptt)
  1580. return -EAGAIN;
  1581. rc = qed_mcp_get_nvm_image(hwfn, ptt, type, buf, len);
  1582. qed_ptt_release(hwfn, ptt);
  1583. return rc;
  1584. }
  1585. static int qed_set_coalesce(struct qed_dev *cdev, u16 rx_coal, u16 tx_coal,
  1586. void *handle)
  1587. {
  1588. return qed_set_queue_coalesce(rx_coal, tx_coal, handle);
  1589. }
  1590. static int qed_set_led(struct qed_dev *cdev, enum qed_led_mode mode)
  1591. {
  1592. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1593. struct qed_ptt *ptt;
  1594. int status = 0;
  1595. ptt = qed_ptt_acquire(hwfn);
  1596. if (!ptt)
  1597. return -EAGAIN;
  1598. status = qed_mcp_set_led(hwfn, ptt, mode);
  1599. qed_ptt_release(hwfn, ptt);
  1600. return status;
  1601. }
  1602. static int qed_update_wol(struct qed_dev *cdev, bool enabled)
  1603. {
  1604. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1605. struct qed_ptt *ptt;
  1606. int rc = 0;
  1607. if (IS_VF(cdev))
  1608. return 0;
  1609. ptt = qed_ptt_acquire(hwfn);
  1610. if (!ptt)
  1611. return -EAGAIN;
  1612. rc = qed_mcp_ov_update_wol(hwfn, ptt, enabled ? QED_OV_WOL_ENABLED
  1613. : QED_OV_WOL_DISABLED);
  1614. if (rc)
  1615. goto out;
  1616. rc = qed_mcp_ov_update_current_config(hwfn, ptt, QED_OV_CLIENT_DRV);
  1617. out:
  1618. qed_ptt_release(hwfn, ptt);
  1619. return rc;
  1620. }
  1621. static int qed_update_drv_state(struct qed_dev *cdev, bool active)
  1622. {
  1623. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1624. struct qed_ptt *ptt;
  1625. int status = 0;
  1626. if (IS_VF(cdev))
  1627. return 0;
  1628. ptt = qed_ptt_acquire(hwfn);
  1629. if (!ptt)
  1630. return -EAGAIN;
  1631. status = qed_mcp_ov_update_driver_state(hwfn, ptt, active ?
  1632. QED_OV_DRIVER_STATE_ACTIVE :
  1633. QED_OV_DRIVER_STATE_DISABLED);
  1634. qed_ptt_release(hwfn, ptt);
  1635. return status;
  1636. }
  1637. static int qed_update_mac(struct qed_dev *cdev, u8 *mac)
  1638. {
  1639. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1640. struct qed_ptt *ptt;
  1641. int status = 0;
  1642. if (IS_VF(cdev))
  1643. return 0;
  1644. ptt = qed_ptt_acquire(hwfn);
  1645. if (!ptt)
  1646. return -EAGAIN;
  1647. status = qed_mcp_ov_update_mac(hwfn, ptt, mac);
  1648. if (status)
  1649. goto out;
  1650. status = qed_mcp_ov_update_current_config(hwfn, ptt, QED_OV_CLIENT_DRV);
  1651. out:
  1652. qed_ptt_release(hwfn, ptt);
  1653. return status;
  1654. }
  1655. static int qed_update_mtu(struct qed_dev *cdev, u16 mtu)
  1656. {
  1657. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1658. struct qed_ptt *ptt;
  1659. int status = 0;
  1660. if (IS_VF(cdev))
  1661. return 0;
  1662. ptt = qed_ptt_acquire(hwfn);
  1663. if (!ptt)
  1664. return -EAGAIN;
  1665. status = qed_mcp_ov_update_mtu(hwfn, ptt, mtu);
  1666. if (status)
  1667. goto out;
  1668. status = qed_mcp_ov_update_current_config(hwfn, ptt, QED_OV_CLIENT_DRV);
  1669. out:
  1670. qed_ptt_release(hwfn, ptt);
  1671. return status;
  1672. }
  1673. static struct qed_selftest_ops qed_selftest_ops_pass = {
  1674. .selftest_memory = &qed_selftest_memory,
  1675. .selftest_interrupt = &qed_selftest_interrupt,
  1676. .selftest_register = &qed_selftest_register,
  1677. .selftest_clock = &qed_selftest_clock,
  1678. .selftest_nvram = &qed_selftest_nvram,
  1679. };
  1680. const struct qed_common_ops qed_common_ops_pass = {
  1681. .selftest = &qed_selftest_ops_pass,
  1682. .probe = &qed_probe,
  1683. .remove = &qed_remove,
  1684. .set_power_state = &qed_set_power_state,
  1685. .set_name = &qed_set_name,
  1686. .update_pf_params = &qed_update_pf_params,
  1687. .slowpath_start = &qed_slowpath_start,
  1688. .slowpath_stop = &qed_slowpath_stop,
  1689. .set_fp_int = &qed_set_int_fp,
  1690. .get_fp_int = &qed_get_int_fp,
  1691. .sb_init = &qed_sb_init,
  1692. .sb_release = &qed_sb_release,
  1693. .simd_handler_config = &qed_simd_handler_config,
  1694. .simd_handler_clean = &qed_simd_handler_clean,
  1695. .dbg_grc = &qed_dbg_grc,
  1696. .dbg_grc_size = &qed_dbg_grc_size,
  1697. .can_link_change = &qed_can_link_change,
  1698. .set_link = &qed_set_link,
  1699. .get_link = &qed_get_current_link,
  1700. .drain = &qed_drain,
  1701. .update_msglvl = &qed_init_dp,
  1702. .dbg_all_data = &qed_dbg_all_data,
  1703. .dbg_all_data_size = &qed_dbg_all_data_size,
  1704. .chain_alloc = &qed_chain_alloc,
  1705. .chain_free = &qed_chain_free,
  1706. .nvm_flash = &qed_nvm_flash,
  1707. .nvm_get_image = &qed_nvm_get_image,
  1708. .set_coalesce = &qed_set_coalesce,
  1709. .set_led = &qed_set_led,
  1710. .update_drv_state = &qed_update_drv_state,
  1711. .update_mac = &qed_update_mac,
  1712. .update_mtu = &qed_update_mtu,
  1713. .update_wol = &qed_update_wol,
  1714. };
  1715. void qed_get_protocol_stats(struct qed_dev *cdev,
  1716. enum qed_mcp_protocol_type type,
  1717. union qed_mcp_protocol_stats *stats)
  1718. {
  1719. struct qed_eth_stats eth_stats;
  1720. memset(stats, 0, sizeof(*stats));
  1721. switch (type) {
  1722. case QED_MCP_LAN_STATS:
  1723. qed_get_vport_stats(cdev, &eth_stats);
  1724. stats->lan_stats.ucast_rx_pkts =
  1725. eth_stats.common.rx_ucast_pkts;
  1726. stats->lan_stats.ucast_tx_pkts =
  1727. eth_stats.common.tx_ucast_pkts;
  1728. stats->lan_stats.fcs_err = -1;
  1729. break;
  1730. case QED_MCP_FCOE_STATS:
  1731. qed_get_protocol_stats_fcoe(cdev, &stats->fcoe_stats);
  1732. break;
  1733. case QED_MCP_ISCSI_STATS:
  1734. qed_get_protocol_stats_iscsi(cdev, &stats->iscsi_stats);
  1735. break;
  1736. default:
  1737. DP_VERBOSE(cdev, QED_MSG_SP,
  1738. "Invalid protocol type = %d\n", type);
  1739. return;
  1740. }
  1741. }