qed_main.c 58 KB

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