qed_main.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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. hwfn->simd_proto_handler[j].func(
  469. hwfn->simd_proto_handler[j].token);
  470. status &= ~(0x2ULL << j);
  471. rc = IRQ_HANDLED;
  472. }
  473. }
  474. if (unlikely(status))
  475. DP_VERBOSE(hwfn, NETIF_MSG_INTR,
  476. "got an unknown interrupt status 0x%llx\n",
  477. status);
  478. }
  479. return rc;
  480. }
  481. int qed_slowpath_irq_req(struct qed_hwfn *hwfn)
  482. {
  483. struct qed_dev *cdev = hwfn->cdev;
  484. u32 int_mode;
  485. int rc = 0;
  486. u8 id;
  487. int_mode = cdev->int_params.out.int_mode;
  488. if (int_mode == QED_INT_MODE_MSIX) {
  489. id = hwfn->my_id;
  490. snprintf(hwfn->name, NAME_SIZE, "sp-%d-%02x:%02x.%02x",
  491. id, cdev->pdev->bus->number,
  492. PCI_SLOT(cdev->pdev->devfn), hwfn->abs_pf_id);
  493. rc = request_irq(cdev->int_params.msix_table[id].vector,
  494. qed_msix_sp_int, 0, hwfn->name, hwfn->sp_dpc);
  495. } else {
  496. unsigned long flags = 0;
  497. snprintf(cdev->name, NAME_SIZE, "%02x:%02x.%02x",
  498. cdev->pdev->bus->number, PCI_SLOT(cdev->pdev->devfn),
  499. PCI_FUNC(cdev->pdev->devfn));
  500. if (cdev->int_params.out.int_mode == QED_INT_MODE_INTA)
  501. flags |= IRQF_SHARED;
  502. rc = request_irq(cdev->pdev->irq, qed_single_int,
  503. flags, cdev->name, cdev);
  504. }
  505. if (rc)
  506. DP_NOTICE(cdev, "request_irq failed, rc = %d\n", rc);
  507. else
  508. DP_VERBOSE(hwfn, (NETIF_MSG_INTR | QED_MSG_SP),
  509. "Requested slowpath %s\n",
  510. (int_mode == QED_INT_MODE_MSIX) ? "MSI-X" : "IRQ");
  511. return rc;
  512. }
  513. static void qed_slowpath_tasklet_flush(struct qed_hwfn *p_hwfn)
  514. {
  515. /* Calling the disable function will make sure that any
  516. * currently-running function is completed. The following call to the
  517. * enable function makes this sequence a flush-like operation.
  518. */
  519. if (p_hwfn->b_sp_dpc_enabled) {
  520. tasklet_disable(p_hwfn->sp_dpc);
  521. tasklet_enable(p_hwfn->sp_dpc);
  522. }
  523. }
  524. void qed_slowpath_irq_sync(struct qed_hwfn *p_hwfn)
  525. {
  526. struct qed_dev *cdev = p_hwfn->cdev;
  527. u8 id = p_hwfn->my_id;
  528. u32 int_mode;
  529. int_mode = cdev->int_params.out.int_mode;
  530. if (int_mode == QED_INT_MODE_MSIX)
  531. synchronize_irq(cdev->int_params.msix_table[id].vector);
  532. else
  533. synchronize_irq(cdev->pdev->irq);
  534. qed_slowpath_tasklet_flush(p_hwfn);
  535. }
  536. static void qed_slowpath_irq_free(struct qed_dev *cdev)
  537. {
  538. int i;
  539. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  540. for_each_hwfn(cdev, i) {
  541. if (!cdev->hwfns[i].b_int_requested)
  542. break;
  543. synchronize_irq(cdev->int_params.msix_table[i].vector);
  544. free_irq(cdev->int_params.msix_table[i].vector,
  545. cdev->hwfns[i].sp_dpc);
  546. }
  547. } else {
  548. if (QED_LEADING_HWFN(cdev)->b_int_requested)
  549. free_irq(cdev->pdev->irq, cdev);
  550. }
  551. qed_int_disable_post_isr_release(cdev);
  552. }
  553. static int qed_nic_stop(struct qed_dev *cdev)
  554. {
  555. int i, rc;
  556. rc = qed_hw_stop(cdev);
  557. for (i = 0; i < cdev->num_hwfns; i++) {
  558. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  559. if (p_hwfn->b_sp_dpc_enabled) {
  560. tasklet_disable(p_hwfn->sp_dpc);
  561. p_hwfn->b_sp_dpc_enabled = false;
  562. DP_VERBOSE(cdev, NETIF_MSG_IFDOWN,
  563. "Disabled sp tasklet [hwfn %d] at %p\n",
  564. i, p_hwfn->sp_dpc);
  565. }
  566. }
  567. qed_dbg_pf_exit(cdev);
  568. return rc;
  569. }
  570. static int qed_nic_setup(struct qed_dev *cdev)
  571. {
  572. int rc, i;
  573. /* Determine if interface is going to require LL2 */
  574. if (QED_LEADING_HWFN(cdev)->hw_info.personality != QED_PCI_ETH) {
  575. for (i = 0; i < cdev->num_hwfns; i++) {
  576. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  577. p_hwfn->using_ll2 = true;
  578. }
  579. }
  580. rc = qed_resc_alloc(cdev);
  581. if (rc)
  582. return rc;
  583. DP_INFO(cdev, "Allocated qed resources\n");
  584. qed_resc_setup(cdev);
  585. return rc;
  586. }
  587. static int qed_set_int_fp(struct qed_dev *cdev, u16 cnt)
  588. {
  589. int limit = 0;
  590. /* Mark the fastpath as free/used */
  591. cdev->int_params.fp_initialized = cnt ? true : false;
  592. if (cdev->int_params.out.int_mode != QED_INT_MODE_MSIX)
  593. limit = cdev->num_hwfns * 63;
  594. else if (cdev->int_params.fp_msix_cnt)
  595. limit = cdev->int_params.fp_msix_cnt;
  596. if (!limit)
  597. return -ENOMEM;
  598. return min_t(int, cnt, limit);
  599. }
  600. static int qed_get_int_fp(struct qed_dev *cdev, struct qed_int_info *info)
  601. {
  602. memset(info, 0, sizeof(struct qed_int_info));
  603. if (!cdev->int_params.fp_initialized) {
  604. DP_INFO(cdev,
  605. "Protocol driver requested interrupt information, but its support is not yet configured\n");
  606. return -EINVAL;
  607. }
  608. /* Need to expose only MSI-X information; Single IRQ is handled solely
  609. * by qed.
  610. */
  611. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  612. int msix_base = cdev->int_params.fp_msix_base;
  613. info->msix_cnt = cdev->int_params.fp_msix_cnt;
  614. info->msix = &cdev->int_params.msix_table[msix_base];
  615. }
  616. return 0;
  617. }
  618. static int qed_slowpath_setup_int(struct qed_dev *cdev,
  619. enum qed_int_mode int_mode)
  620. {
  621. struct qed_sb_cnt_info sb_cnt_info;
  622. int num_l2_queues = 0;
  623. int rc;
  624. int i;
  625. if ((int_mode == QED_INT_MODE_MSI) && (cdev->num_hwfns > 1)) {
  626. DP_NOTICE(cdev, "MSI mode is not supported for CMT devices\n");
  627. return -EINVAL;
  628. }
  629. memset(&cdev->int_params, 0, sizeof(struct qed_int_params));
  630. cdev->int_params.in.int_mode = int_mode;
  631. for_each_hwfn(cdev, i) {
  632. memset(&sb_cnt_info, 0, sizeof(sb_cnt_info));
  633. qed_int_get_num_sbs(&cdev->hwfns[i], &sb_cnt_info);
  634. cdev->int_params.in.num_vectors += sb_cnt_info.cnt;
  635. cdev->int_params.in.num_vectors++; /* slowpath */
  636. }
  637. /* We want a minimum of one slowpath and one fastpath vector per hwfn */
  638. cdev->int_params.in.min_msix_cnt = cdev->num_hwfns * 2;
  639. rc = qed_set_int_mode(cdev, false);
  640. if (rc) {
  641. DP_ERR(cdev, "qed_slowpath_setup_int ERR\n");
  642. return rc;
  643. }
  644. cdev->int_params.fp_msix_base = cdev->num_hwfns;
  645. cdev->int_params.fp_msix_cnt = cdev->int_params.out.num_vectors -
  646. cdev->num_hwfns;
  647. if (!IS_ENABLED(CONFIG_QED_RDMA) ||
  648. !QED_IS_RDMA_PERSONALITY(QED_LEADING_HWFN(cdev)))
  649. return 0;
  650. for_each_hwfn(cdev, i)
  651. num_l2_queues += FEAT_NUM(&cdev->hwfns[i], QED_PF_L2_QUE);
  652. DP_VERBOSE(cdev, QED_MSG_RDMA,
  653. "cdev->int_params.fp_msix_cnt=%d num_l2_queues=%d\n",
  654. cdev->int_params.fp_msix_cnt, num_l2_queues);
  655. if (cdev->int_params.fp_msix_cnt > num_l2_queues) {
  656. cdev->int_params.rdma_msix_cnt =
  657. (cdev->int_params.fp_msix_cnt - num_l2_queues)
  658. / cdev->num_hwfns;
  659. cdev->int_params.rdma_msix_base =
  660. cdev->int_params.fp_msix_base + num_l2_queues;
  661. cdev->int_params.fp_msix_cnt = num_l2_queues;
  662. } else {
  663. cdev->int_params.rdma_msix_cnt = 0;
  664. }
  665. DP_VERBOSE(cdev, QED_MSG_RDMA, "roce_msix_cnt=%d roce_msix_base=%d\n",
  666. cdev->int_params.rdma_msix_cnt,
  667. cdev->int_params.rdma_msix_base);
  668. return 0;
  669. }
  670. static int qed_slowpath_vf_setup_int(struct qed_dev *cdev)
  671. {
  672. int rc;
  673. memset(&cdev->int_params, 0, sizeof(struct qed_int_params));
  674. cdev->int_params.in.int_mode = QED_INT_MODE_MSIX;
  675. qed_vf_get_num_rxqs(QED_LEADING_HWFN(cdev),
  676. &cdev->int_params.in.num_vectors);
  677. if (cdev->num_hwfns > 1) {
  678. u8 vectors = 0;
  679. qed_vf_get_num_rxqs(&cdev->hwfns[1], &vectors);
  680. cdev->int_params.in.num_vectors += vectors;
  681. }
  682. /* We want a minimum of one fastpath vector per vf hwfn */
  683. cdev->int_params.in.min_msix_cnt = cdev->num_hwfns;
  684. rc = qed_set_int_mode(cdev, true);
  685. if (rc)
  686. return rc;
  687. cdev->int_params.fp_msix_base = 0;
  688. cdev->int_params.fp_msix_cnt = cdev->int_params.out.num_vectors;
  689. return 0;
  690. }
  691. u32 qed_unzip_data(struct qed_hwfn *p_hwfn, u32 input_len,
  692. u8 *input_buf, u32 max_size, u8 *unzip_buf)
  693. {
  694. int rc;
  695. p_hwfn->stream->next_in = input_buf;
  696. p_hwfn->stream->avail_in = input_len;
  697. p_hwfn->stream->next_out = unzip_buf;
  698. p_hwfn->stream->avail_out = max_size;
  699. rc = zlib_inflateInit2(p_hwfn->stream, MAX_WBITS);
  700. if (rc != Z_OK) {
  701. DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "zlib init failed, rc = %d\n",
  702. rc);
  703. return 0;
  704. }
  705. rc = zlib_inflate(p_hwfn->stream, Z_FINISH);
  706. zlib_inflateEnd(p_hwfn->stream);
  707. if (rc != Z_OK && rc != Z_STREAM_END) {
  708. DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "FW unzip error: %s, rc=%d\n",
  709. p_hwfn->stream->msg, rc);
  710. return 0;
  711. }
  712. return p_hwfn->stream->total_out / 4;
  713. }
  714. static int qed_alloc_stream_mem(struct qed_dev *cdev)
  715. {
  716. int i;
  717. void *workspace;
  718. for_each_hwfn(cdev, i) {
  719. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  720. p_hwfn->stream = kzalloc(sizeof(*p_hwfn->stream), GFP_KERNEL);
  721. if (!p_hwfn->stream)
  722. return -ENOMEM;
  723. workspace = vzalloc(zlib_inflate_workspacesize());
  724. if (!workspace)
  725. return -ENOMEM;
  726. p_hwfn->stream->workspace = workspace;
  727. }
  728. return 0;
  729. }
  730. static void qed_free_stream_mem(struct qed_dev *cdev)
  731. {
  732. int i;
  733. for_each_hwfn(cdev, i) {
  734. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  735. if (!p_hwfn->stream)
  736. return;
  737. vfree(p_hwfn->stream->workspace);
  738. kfree(p_hwfn->stream);
  739. }
  740. }
  741. static void qed_update_pf_params(struct qed_dev *cdev,
  742. struct qed_pf_params *params)
  743. {
  744. int i;
  745. if (IS_ENABLED(CONFIG_QED_RDMA)) {
  746. params->rdma_pf_params.num_qps = QED_ROCE_QPS;
  747. params->rdma_pf_params.min_dpis = QED_ROCE_DPIS;
  748. params->rdma_pf_params.num_srqs = QED_RDMA_SRQS;
  749. /* divide by 3 the MRs to avoid MF ILT overflow */
  750. params->rdma_pf_params.gl_pi = QED_ROCE_PROTOCOL_INDEX;
  751. }
  752. if (cdev->num_hwfns > 1 || IS_VF(cdev))
  753. params->eth_pf_params.num_arfs_filters = 0;
  754. /* In case we might support RDMA, don't allow qede to be greedy
  755. * with the L2 contexts. Allow for 64 queues [rx, tx, xdp] per hwfn.
  756. */
  757. if (QED_IS_RDMA_PERSONALITY(QED_LEADING_HWFN(cdev))) {
  758. u16 *num_cons;
  759. num_cons = &params->eth_pf_params.num_cons;
  760. *num_cons = min_t(u16, *num_cons, 192);
  761. }
  762. for (i = 0; i < cdev->num_hwfns; i++) {
  763. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  764. p_hwfn->pf_params = *params;
  765. }
  766. }
  767. static void qed_slowpath_wq_stop(struct qed_dev *cdev)
  768. {
  769. int i;
  770. if (IS_VF(cdev))
  771. return;
  772. for_each_hwfn(cdev, i) {
  773. if (!cdev->hwfns[i].slowpath_wq)
  774. continue;
  775. flush_workqueue(cdev->hwfns[i].slowpath_wq);
  776. destroy_workqueue(cdev->hwfns[i].slowpath_wq);
  777. }
  778. }
  779. static void qed_slowpath_task(struct work_struct *work)
  780. {
  781. struct qed_hwfn *hwfn = container_of(work, struct qed_hwfn,
  782. slowpath_task.work);
  783. struct qed_ptt *ptt = qed_ptt_acquire(hwfn);
  784. if (!ptt) {
  785. queue_delayed_work(hwfn->slowpath_wq, &hwfn->slowpath_task, 0);
  786. return;
  787. }
  788. if (test_and_clear_bit(QED_SLOWPATH_MFW_TLV_REQ,
  789. &hwfn->slowpath_task_flags))
  790. qed_mfw_process_tlv_req(hwfn, ptt);
  791. qed_ptt_release(hwfn, ptt);
  792. }
  793. static int qed_slowpath_wq_start(struct qed_dev *cdev)
  794. {
  795. struct qed_hwfn *hwfn;
  796. char name[NAME_SIZE];
  797. int i;
  798. if (IS_VF(cdev))
  799. return 0;
  800. for_each_hwfn(cdev, i) {
  801. hwfn = &cdev->hwfns[i];
  802. snprintf(name, NAME_SIZE, "slowpath-%02x:%02x.%02x",
  803. cdev->pdev->bus->number,
  804. PCI_SLOT(cdev->pdev->devfn), hwfn->abs_pf_id);
  805. hwfn->slowpath_wq = alloc_workqueue(name, 0, 0);
  806. if (!hwfn->slowpath_wq) {
  807. DP_NOTICE(hwfn, "Cannot create slowpath workqueue\n");
  808. return -ENOMEM;
  809. }
  810. INIT_DELAYED_WORK(&hwfn->slowpath_task, qed_slowpath_task);
  811. }
  812. return 0;
  813. }
  814. static int qed_slowpath_start(struct qed_dev *cdev,
  815. struct qed_slowpath_params *params)
  816. {
  817. struct qed_drv_load_params drv_load_params;
  818. struct qed_hw_init_params hw_init_params;
  819. struct qed_mcp_drv_version drv_version;
  820. struct qed_tunnel_info tunn_info;
  821. const u8 *data = NULL;
  822. struct qed_hwfn *hwfn;
  823. struct qed_ptt *p_ptt;
  824. int rc = -EINVAL;
  825. if (qed_iov_wq_start(cdev))
  826. goto err;
  827. if (qed_slowpath_wq_start(cdev))
  828. goto err;
  829. if (IS_PF(cdev)) {
  830. rc = request_firmware(&cdev->firmware, QED_FW_FILE_NAME,
  831. &cdev->pdev->dev);
  832. if (rc) {
  833. DP_NOTICE(cdev,
  834. "Failed to find fw file - /lib/firmware/%s\n",
  835. QED_FW_FILE_NAME);
  836. goto err;
  837. }
  838. if (cdev->num_hwfns == 1) {
  839. p_ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
  840. if (p_ptt) {
  841. QED_LEADING_HWFN(cdev)->p_arfs_ptt = p_ptt;
  842. } else {
  843. DP_NOTICE(cdev,
  844. "Failed to acquire PTT for aRFS\n");
  845. goto err;
  846. }
  847. }
  848. }
  849. cdev->rx_coalesce_usecs = QED_DEFAULT_RX_USECS;
  850. rc = qed_nic_setup(cdev);
  851. if (rc)
  852. goto err;
  853. if (IS_PF(cdev))
  854. rc = qed_slowpath_setup_int(cdev, params->int_mode);
  855. else
  856. rc = qed_slowpath_vf_setup_int(cdev);
  857. if (rc)
  858. goto err1;
  859. if (IS_PF(cdev)) {
  860. /* Allocate stream for unzipping */
  861. rc = qed_alloc_stream_mem(cdev);
  862. if (rc)
  863. goto err2;
  864. /* First Dword used to differentiate between various sources */
  865. data = cdev->firmware->data + sizeof(u32);
  866. qed_dbg_pf_init(cdev);
  867. }
  868. /* Start the slowpath */
  869. memset(&hw_init_params, 0, sizeof(hw_init_params));
  870. memset(&tunn_info, 0, sizeof(tunn_info));
  871. tunn_info.vxlan.b_mode_enabled = true;
  872. tunn_info.l2_gre.b_mode_enabled = true;
  873. tunn_info.ip_gre.b_mode_enabled = true;
  874. tunn_info.l2_geneve.b_mode_enabled = true;
  875. tunn_info.ip_geneve.b_mode_enabled = true;
  876. tunn_info.vxlan.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  877. tunn_info.l2_gre.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  878. tunn_info.ip_gre.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  879. tunn_info.l2_geneve.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  880. tunn_info.ip_geneve.tun_cls = QED_TUNN_CLSS_MAC_VLAN;
  881. hw_init_params.p_tunn = &tunn_info;
  882. hw_init_params.b_hw_start = true;
  883. hw_init_params.int_mode = cdev->int_params.out.int_mode;
  884. hw_init_params.allow_npar_tx_switch = true;
  885. hw_init_params.bin_fw_data = data;
  886. memset(&drv_load_params, 0, sizeof(drv_load_params));
  887. drv_load_params.is_crash_kernel = is_kdump_kernel();
  888. drv_load_params.mfw_timeout_val = QED_LOAD_REQ_LOCK_TO_DEFAULT;
  889. drv_load_params.avoid_eng_reset = false;
  890. drv_load_params.override_force_load = QED_OVERRIDE_FORCE_LOAD_NONE;
  891. hw_init_params.p_drv_load_params = &drv_load_params;
  892. rc = qed_hw_init(cdev, &hw_init_params);
  893. if (rc)
  894. goto err2;
  895. DP_INFO(cdev,
  896. "HW initialization and function start completed successfully\n");
  897. if (IS_PF(cdev)) {
  898. cdev->tunn_feature_mask = (BIT(QED_MODE_VXLAN_TUNN) |
  899. BIT(QED_MODE_L2GENEVE_TUNN) |
  900. BIT(QED_MODE_IPGENEVE_TUNN) |
  901. BIT(QED_MODE_L2GRE_TUNN) |
  902. BIT(QED_MODE_IPGRE_TUNN));
  903. }
  904. /* Allocate LL2 interface if needed */
  905. if (QED_LEADING_HWFN(cdev)->using_ll2) {
  906. rc = qed_ll2_alloc_if(cdev);
  907. if (rc)
  908. goto err3;
  909. }
  910. if (IS_PF(cdev)) {
  911. hwfn = QED_LEADING_HWFN(cdev);
  912. drv_version.version = (params->drv_major << 24) |
  913. (params->drv_minor << 16) |
  914. (params->drv_rev << 8) |
  915. (params->drv_eng);
  916. strlcpy(drv_version.name, params->name,
  917. MCP_DRV_VER_STR_SIZE - 4);
  918. rc = qed_mcp_send_drv_version(hwfn, hwfn->p_main_ptt,
  919. &drv_version);
  920. if (rc) {
  921. DP_NOTICE(cdev, "Failed sending drv version command\n");
  922. return rc;
  923. }
  924. }
  925. qed_reset_vport_stats(cdev);
  926. return 0;
  927. err3:
  928. qed_hw_stop(cdev);
  929. err2:
  930. qed_hw_timers_stop_all(cdev);
  931. if (IS_PF(cdev))
  932. qed_slowpath_irq_free(cdev);
  933. qed_free_stream_mem(cdev);
  934. qed_disable_msix(cdev);
  935. err1:
  936. qed_resc_free(cdev);
  937. err:
  938. if (IS_PF(cdev))
  939. release_firmware(cdev->firmware);
  940. if (IS_PF(cdev) && (cdev->num_hwfns == 1) &&
  941. QED_LEADING_HWFN(cdev)->p_arfs_ptt)
  942. qed_ptt_release(QED_LEADING_HWFN(cdev),
  943. QED_LEADING_HWFN(cdev)->p_arfs_ptt);
  944. qed_iov_wq_stop(cdev, false);
  945. qed_slowpath_wq_stop(cdev);
  946. return rc;
  947. }
  948. static int qed_slowpath_stop(struct qed_dev *cdev)
  949. {
  950. if (!cdev)
  951. return -ENODEV;
  952. qed_slowpath_wq_stop(cdev);
  953. qed_ll2_dealloc_if(cdev);
  954. if (IS_PF(cdev)) {
  955. if (cdev->num_hwfns == 1)
  956. qed_ptt_release(QED_LEADING_HWFN(cdev),
  957. QED_LEADING_HWFN(cdev)->p_arfs_ptt);
  958. qed_free_stream_mem(cdev);
  959. if (IS_QED_ETH_IF(cdev))
  960. qed_sriov_disable(cdev, true);
  961. }
  962. qed_nic_stop(cdev);
  963. if (IS_PF(cdev))
  964. qed_slowpath_irq_free(cdev);
  965. qed_disable_msix(cdev);
  966. qed_resc_free(cdev);
  967. qed_iov_wq_stop(cdev, true);
  968. if (IS_PF(cdev))
  969. release_firmware(cdev->firmware);
  970. return 0;
  971. }
  972. static void qed_set_name(struct qed_dev *cdev, char name[NAME_SIZE])
  973. {
  974. int i;
  975. memcpy(cdev->name, name, NAME_SIZE);
  976. for_each_hwfn(cdev, i)
  977. snprintf(cdev->hwfns[i].name, NAME_SIZE, "%s-%d", name, i);
  978. }
  979. static u32 qed_sb_init(struct qed_dev *cdev,
  980. struct qed_sb_info *sb_info,
  981. void *sb_virt_addr,
  982. dma_addr_t sb_phy_addr, u16 sb_id,
  983. enum qed_sb_type type)
  984. {
  985. struct qed_hwfn *p_hwfn;
  986. struct qed_ptt *p_ptt;
  987. int hwfn_index;
  988. u16 rel_sb_id;
  989. u8 n_hwfns;
  990. u32 rc;
  991. /* RoCE uses single engine and CMT uses two engines. When using both
  992. * we force only a single engine. Storage uses only engine 0 too.
  993. */
  994. if (type == QED_SB_TYPE_L2_QUEUE)
  995. n_hwfns = cdev->num_hwfns;
  996. else
  997. n_hwfns = 1;
  998. hwfn_index = sb_id % n_hwfns;
  999. p_hwfn = &cdev->hwfns[hwfn_index];
  1000. rel_sb_id = sb_id / n_hwfns;
  1001. DP_VERBOSE(cdev, NETIF_MSG_INTR,
  1002. "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
  1003. hwfn_index, rel_sb_id, sb_id);
  1004. if (IS_PF(p_hwfn->cdev)) {
  1005. p_ptt = qed_ptt_acquire(p_hwfn);
  1006. if (!p_ptt)
  1007. return -EBUSY;
  1008. rc = qed_int_sb_init(p_hwfn, p_ptt, sb_info, sb_virt_addr,
  1009. sb_phy_addr, rel_sb_id);
  1010. qed_ptt_release(p_hwfn, p_ptt);
  1011. } else {
  1012. rc = qed_int_sb_init(p_hwfn, NULL, sb_info, sb_virt_addr,
  1013. sb_phy_addr, rel_sb_id);
  1014. }
  1015. return rc;
  1016. }
  1017. static u32 qed_sb_release(struct qed_dev *cdev,
  1018. struct qed_sb_info *sb_info, u16 sb_id)
  1019. {
  1020. struct qed_hwfn *p_hwfn;
  1021. int hwfn_index;
  1022. u16 rel_sb_id;
  1023. u32 rc;
  1024. hwfn_index = sb_id % cdev->num_hwfns;
  1025. p_hwfn = &cdev->hwfns[hwfn_index];
  1026. rel_sb_id = sb_id / cdev->num_hwfns;
  1027. DP_VERBOSE(cdev, NETIF_MSG_INTR,
  1028. "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
  1029. hwfn_index, rel_sb_id, sb_id);
  1030. rc = qed_int_sb_release(p_hwfn, sb_info, rel_sb_id);
  1031. return rc;
  1032. }
  1033. static bool qed_can_link_change(struct qed_dev *cdev)
  1034. {
  1035. return true;
  1036. }
  1037. static int qed_set_link(struct qed_dev *cdev, struct qed_link_params *params)
  1038. {
  1039. struct qed_hwfn *hwfn;
  1040. struct qed_mcp_link_params *link_params;
  1041. struct qed_ptt *ptt;
  1042. int rc;
  1043. if (!cdev)
  1044. return -ENODEV;
  1045. /* The link should be set only once per PF */
  1046. hwfn = &cdev->hwfns[0];
  1047. /* When VF wants to set link, force it to read the bulletin instead.
  1048. * This mimics the PF behavior, where a noitification [both immediate
  1049. * and possible later] would be generated when changing properties.
  1050. */
  1051. if (IS_VF(cdev)) {
  1052. qed_schedule_iov(hwfn, QED_IOV_WQ_VF_FORCE_LINK_QUERY_FLAG);
  1053. return 0;
  1054. }
  1055. ptt = qed_ptt_acquire(hwfn);
  1056. if (!ptt)
  1057. return -EBUSY;
  1058. link_params = qed_mcp_get_link_params(hwfn);
  1059. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_AUTONEG)
  1060. link_params->speed.autoneg = params->autoneg;
  1061. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_ADV_SPEEDS) {
  1062. link_params->speed.advertised_speeds = 0;
  1063. if ((params->adv_speeds & QED_LM_1000baseT_Half_BIT) ||
  1064. (params->adv_speeds & QED_LM_1000baseT_Full_BIT))
  1065. link_params->speed.advertised_speeds |=
  1066. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G;
  1067. if (params->adv_speeds & QED_LM_10000baseKR_Full_BIT)
  1068. link_params->speed.advertised_speeds |=
  1069. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G;
  1070. if (params->adv_speeds & QED_LM_25000baseKR_Full_BIT)
  1071. link_params->speed.advertised_speeds |=
  1072. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_25G;
  1073. if (params->adv_speeds & QED_LM_40000baseLR4_Full_BIT)
  1074. link_params->speed.advertised_speeds |=
  1075. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G;
  1076. if (params->adv_speeds & QED_LM_50000baseKR2_Full_BIT)
  1077. link_params->speed.advertised_speeds |=
  1078. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G;
  1079. if (params->adv_speeds & QED_LM_100000baseKR4_Full_BIT)
  1080. link_params->speed.advertised_speeds |=
  1081. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_BB_100G;
  1082. }
  1083. if (params->override_flags & QED_LINK_OVERRIDE_SPEED_FORCED_SPEED)
  1084. link_params->speed.forced_speed = params->forced_speed;
  1085. if (params->override_flags & QED_LINK_OVERRIDE_PAUSE_CONFIG) {
  1086. if (params->pause_config & QED_LINK_PAUSE_AUTONEG_ENABLE)
  1087. link_params->pause.autoneg = true;
  1088. else
  1089. link_params->pause.autoneg = false;
  1090. if (params->pause_config & QED_LINK_PAUSE_RX_ENABLE)
  1091. link_params->pause.forced_rx = true;
  1092. else
  1093. link_params->pause.forced_rx = false;
  1094. if (params->pause_config & QED_LINK_PAUSE_TX_ENABLE)
  1095. link_params->pause.forced_tx = true;
  1096. else
  1097. link_params->pause.forced_tx = false;
  1098. }
  1099. if (params->override_flags & QED_LINK_OVERRIDE_LOOPBACK_MODE) {
  1100. switch (params->loopback_mode) {
  1101. case QED_LINK_LOOPBACK_INT_PHY:
  1102. link_params->loopback_mode = ETH_LOOPBACK_INT_PHY;
  1103. break;
  1104. case QED_LINK_LOOPBACK_EXT_PHY:
  1105. link_params->loopback_mode = ETH_LOOPBACK_EXT_PHY;
  1106. break;
  1107. case QED_LINK_LOOPBACK_EXT:
  1108. link_params->loopback_mode = ETH_LOOPBACK_EXT;
  1109. break;
  1110. case QED_LINK_LOOPBACK_MAC:
  1111. link_params->loopback_mode = ETH_LOOPBACK_MAC;
  1112. break;
  1113. default:
  1114. link_params->loopback_mode = ETH_LOOPBACK_NONE;
  1115. break;
  1116. }
  1117. }
  1118. if (params->override_flags & QED_LINK_OVERRIDE_EEE_CONFIG)
  1119. memcpy(&link_params->eee, &params->eee,
  1120. sizeof(link_params->eee));
  1121. rc = qed_mcp_set_link(hwfn, ptt, params->link_up);
  1122. qed_ptt_release(hwfn, ptt);
  1123. return rc;
  1124. }
  1125. static int qed_get_port_type(u32 media_type)
  1126. {
  1127. int port_type;
  1128. switch (media_type) {
  1129. case MEDIA_SFPP_10G_FIBER:
  1130. case MEDIA_SFP_1G_FIBER:
  1131. case MEDIA_XFP_FIBER:
  1132. case MEDIA_MODULE_FIBER:
  1133. case MEDIA_KR:
  1134. port_type = PORT_FIBRE;
  1135. break;
  1136. case MEDIA_DA_TWINAX:
  1137. port_type = PORT_DA;
  1138. break;
  1139. case MEDIA_BASE_T:
  1140. port_type = PORT_TP;
  1141. break;
  1142. case MEDIA_NOT_PRESENT:
  1143. port_type = PORT_NONE;
  1144. break;
  1145. case MEDIA_UNSPECIFIED:
  1146. default:
  1147. port_type = PORT_OTHER;
  1148. break;
  1149. }
  1150. return port_type;
  1151. }
  1152. static int qed_get_link_data(struct qed_hwfn *hwfn,
  1153. struct qed_mcp_link_params *params,
  1154. struct qed_mcp_link_state *link,
  1155. struct qed_mcp_link_capabilities *link_caps)
  1156. {
  1157. void *p;
  1158. if (!IS_PF(hwfn->cdev)) {
  1159. qed_vf_get_link_params(hwfn, params);
  1160. qed_vf_get_link_state(hwfn, link);
  1161. qed_vf_get_link_caps(hwfn, link_caps);
  1162. return 0;
  1163. }
  1164. p = qed_mcp_get_link_params(hwfn);
  1165. if (!p)
  1166. return -ENXIO;
  1167. memcpy(params, p, sizeof(*params));
  1168. p = qed_mcp_get_link_state(hwfn);
  1169. if (!p)
  1170. return -ENXIO;
  1171. memcpy(link, p, sizeof(*link));
  1172. p = qed_mcp_get_link_capabilities(hwfn);
  1173. if (!p)
  1174. return -ENXIO;
  1175. memcpy(link_caps, p, sizeof(*link_caps));
  1176. return 0;
  1177. }
  1178. static void qed_fill_link(struct qed_hwfn *hwfn,
  1179. struct qed_link_output *if_link)
  1180. {
  1181. struct qed_mcp_link_params params;
  1182. struct qed_mcp_link_state link;
  1183. struct qed_mcp_link_capabilities link_caps;
  1184. u32 media_type;
  1185. memset(if_link, 0, sizeof(*if_link));
  1186. /* Prepare source inputs */
  1187. if (qed_get_link_data(hwfn, &params, &link, &link_caps)) {
  1188. dev_warn(&hwfn->cdev->pdev->dev, "no link data available\n");
  1189. return;
  1190. }
  1191. /* Set the link parameters to pass to protocol driver */
  1192. if (link.link_up)
  1193. if_link->link_up = true;
  1194. /* TODO - at the moment assume supported and advertised speed equal */
  1195. if_link->supported_caps = QED_LM_FIBRE_BIT;
  1196. if (link_caps.default_speed_autoneg)
  1197. if_link->supported_caps |= QED_LM_Autoneg_BIT;
  1198. if (params.pause.autoneg ||
  1199. (params.pause.forced_rx && params.pause.forced_tx))
  1200. if_link->supported_caps |= QED_LM_Asym_Pause_BIT;
  1201. if (params.pause.autoneg || params.pause.forced_rx ||
  1202. params.pause.forced_tx)
  1203. if_link->supported_caps |= QED_LM_Pause_BIT;
  1204. if_link->advertised_caps = if_link->supported_caps;
  1205. if (params.speed.autoneg)
  1206. if_link->advertised_caps |= QED_LM_Autoneg_BIT;
  1207. else
  1208. if_link->advertised_caps &= ~QED_LM_Autoneg_BIT;
  1209. if (params.speed.advertised_speeds &
  1210. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G)
  1211. if_link->advertised_caps |= QED_LM_1000baseT_Half_BIT |
  1212. QED_LM_1000baseT_Full_BIT;
  1213. if (params.speed.advertised_speeds &
  1214. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G)
  1215. if_link->advertised_caps |= QED_LM_10000baseKR_Full_BIT;
  1216. if (params.speed.advertised_speeds &
  1217. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_25G)
  1218. if_link->advertised_caps |= QED_LM_25000baseKR_Full_BIT;
  1219. if (params.speed.advertised_speeds &
  1220. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G)
  1221. if_link->advertised_caps |= QED_LM_40000baseLR4_Full_BIT;
  1222. if (params.speed.advertised_speeds &
  1223. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G)
  1224. if_link->advertised_caps |= QED_LM_50000baseKR2_Full_BIT;
  1225. if (params.speed.advertised_speeds &
  1226. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_BB_100G)
  1227. if_link->advertised_caps |= QED_LM_100000baseKR4_Full_BIT;
  1228. if (link_caps.speed_capabilities &
  1229. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_1G)
  1230. if_link->supported_caps |= QED_LM_1000baseT_Half_BIT |
  1231. QED_LM_1000baseT_Full_BIT;
  1232. if (link_caps.speed_capabilities &
  1233. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_10G)
  1234. if_link->supported_caps |= QED_LM_10000baseKR_Full_BIT;
  1235. if (link_caps.speed_capabilities &
  1236. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_25G)
  1237. if_link->supported_caps |= QED_LM_25000baseKR_Full_BIT;
  1238. if (link_caps.speed_capabilities &
  1239. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_40G)
  1240. if_link->supported_caps |= QED_LM_40000baseLR4_Full_BIT;
  1241. if (link_caps.speed_capabilities &
  1242. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_50G)
  1243. if_link->supported_caps |= QED_LM_50000baseKR2_Full_BIT;
  1244. if (link_caps.speed_capabilities &
  1245. NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_BB_100G)
  1246. if_link->supported_caps |= QED_LM_100000baseKR4_Full_BIT;
  1247. if (link.link_up)
  1248. if_link->speed = link.speed;
  1249. /* TODO - fill duplex properly */
  1250. if_link->duplex = DUPLEX_FULL;
  1251. qed_mcp_get_media_type(hwfn->cdev, &media_type);
  1252. if_link->port = qed_get_port_type(media_type);
  1253. if_link->autoneg = params.speed.autoneg;
  1254. if (params.pause.autoneg)
  1255. if_link->pause_config |= QED_LINK_PAUSE_AUTONEG_ENABLE;
  1256. if (params.pause.forced_rx)
  1257. if_link->pause_config |= QED_LINK_PAUSE_RX_ENABLE;
  1258. if (params.pause.forced_tx)
  1259. if_link->pause_config |= QED_LINK_PAUSE_TX_ENABLE;
  1260. /* Link partner capabilities */
  1261. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_1G_HD)
  1262. if_link->lp_caps |= QED_LM_1000baseT_Half_BIT;
  1263. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_1G_FD)
  1264. if_link->lp_caps |= QED_LM_1000baseT_Full_BIT;
  1265. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_10G)
  1266. if_link->lp_caps |= QED_LM_10000baseKR_Full_BIT;
  1267. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_25G)
  1268. if_link->lp_caps |= QED_LM_25000baseKR_Full_BIT;
  1269. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_40G)
  1270. if_link->lp_caps |= QED_LM_40000baseLR4_Full_BIT;
  1271. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_50G)
  1272. if_link->lp_caps |= QED_LM_50000baseKR2_Full_BIT;
  1273. if (link.partner_adv_speed & QED_LINK_PARTNER_SPEED_100G)
  1274. if_link->lp_caps |= QED_LM_100000baseKR4_Full_BIT;
  1275. if (link.an_complete)
  1276. if_link->lp_caps |= QED_LM_Autoneg_BIT;
  1277. if (link.partner_adv_pause)
  1278. if_link->lp_caps |= QED_LM_Pause_BIT;
  1279. if (link.partner_adv_pause == QED_LINK_PARTNER_ASYMMETRIC_PAUSE ||
  1280. link.partner_adv_pause == QED_LINK_PARTNER_BOTH_PAUSE)
  1281. if_link->lp_caps |= QED_LM_Asym_Pause_BIT;
  1282. if (link_caps.default_eee == QED_MCP_EEE_UNSUPPORTED) {
  1283. if_link->eee_supported = false;
  1284. } else {
  1285. if_link->eee_supported = true;
  1286. if_link->eee_active = link.eee_active;
  1287. if_link->sup_caps = link_caps.eee_speed_caps;
  1288. /* MFW clears adv_caps on eee disable; use configured value */
  1289. if_link->eee.adv_caps = link.eee_adv_caps ? link.eee_adv_caps :
  1290. params.eee.adv_caps;
  1291. if_link->eee.lp_adv_caps = link.eee_lp_adv_caps;
  1292. if_link->eee.enable = params.eee.enable;
  1293. if_link->eee.tx_lpi_enable = params.eee.tx_lpi_enable;
  1294. if_link->eee.tx_lpi_timer = params.eee.tx_lpi_timer;
  1295. }
  1296. }
  1297. static void qed_get_current_link(struct qed_dev *cdev,
  1298. struct qed_link_output *if_link)
  1299. {
  1300. int i;
  1301. qed_fill_link(&cdev->hwfns[0], if_link);
  1302. for_each_hwfn(cdev, i)
  1303. qed_inform_vf_link_state(&cdev->hwfns[i]);
  1304. }
  1305. void qed_link_update(struct qed_hwfn *hwfn)
  1306. {
  1307. void *cookie = hwfn->cdev->ops_cookie;
  1308. struct qed_common_cb_ops *op = hwfn->cdev->protocol_ops.common;
  1309. struct qed_link_output if_link;
  1310. qed_fill_link(hwfn, &if_link);
  1311. qed_inform_vf_link_state(hwfn);
  1312. if (IS_LEAD_HWFN(hwfn) && cookie)
  1313. op->link_update(cookie, &if_link);
  1314. }
  1315. static int qed_drain(struct qed_dev *cdev)
  1316. {
  1317. struct qed_hwfn *hwfn;
  1318. struct qed_ptt *ptt;
  1319. int i, rc;
  1320. if (IS_VF(cdev))
  1321. return 0;
  1322. for_each_hwfn(cdev, i) {
  1323. hwfn = &cdev->hwfns[i];
  1324. ptt = qed_ptt_acquire(hwfn);
  1325. if (!ptt) {
  1326. DP_NOTICE(hwfn, "Failed to drain NIG; No PTT\n");
  1327. return -EBUSY;
  1328. }
  1329. rc = qed_mcp_drain(hwfn, ptt);
  1330. if (rc)
  1331. return rc;
  1332. qed_ptt_release(hwfn, ptt);
  1333. }
  1334. return 0;
  1335. }
  1336. static u32 qed_nvm_flash_image_access_crc(struct qed_dev *cdev,
  1337. struct qed_nvm_image_att *nvm_image,
  1338. u32 *crc)
  1339. {
  1340. u8 *buf = NULL;
  1341. int rc, j;
  1342. u32 val;
  1343. /* Allocate a buffer for holding the nvram image */
  1344. buf = kzalloc(nvm_image->length, GFP_KERNEL);
  1345. if (!buf)
  1346. return -ENOMEM;
  1347. /* Read image into buffer */
  1348. rc = qed_mcp_nvm_read(cdev, nvm_image->start_addr,
  1349. buf, nvm_image->length);
  1350. if (rc) {
  1351. DP_ERR(cdev, "Failed reading image from nvm\n");
  1352. goto out;
  1353. }
  1354. /* Convert the buffer into big-endian format (excluding the
  1355. * closing 4 bytes of CRC).
  1356. */
  1357. for (j = 0; j < nvm_image->length - 4; j += 4) {
  1358. val = cpu_to_be32(*(u32 *)&buf[j]);
  1359. *(u32 *)&buf[j] = val;
  1360. }
  1361. /* Calc CRC for the "actual" image buffer, i.e. not including
  1362. * the last 4 CRC bytes.
  1363. */
  1364. *crc = (~cpu_to_be32(crc32(0xffffffff, buf, nvm_image->length - 4)));
  1365. out:
  1366. kfree(buf);
  1367. return rc;
  1368. }
  1369. /* Binary file format -
  1370. * /----------------------------------------------------------------------\
  1371. * 0B | 0x4 [command index] |
  1372. * 4B | image_type | Options | Number of register settings |
  1373. * 8B | Value |
  1374. * 12B | Mask |
  1375. * 16B | Offset |
  1376. * \----------------------------------------------------------------------/
  1377. * There can be several Value-Mask-Offset sets as specified by 'Number of...'.
  1378. * Options - 0'b - Calculate & Update CRC for image
  1379. */
  1380. static int qed_nvm_flash_image_access(struct qed_dev *cdev, const u8 **data,
  1381. bool *check_resp)
  1382. {
  1383. struct qed_nvm_image_att nvm_image;
  1384. struct qed_hwfn *p_hwfn;
  1385. bool is_crc = false;
  1386. u32 image_type;
  1387. int rc = 0, i;
  1388. u16 len;
  1389. *data += 4;
  1390. image_type = **data;
  1391. p_hwfn = QED_LEADING_HWFN(cdev);
  1392. for (i = 0; i < p_hwfn->nvm_info.num_images; i++)
  1393. if (image_type == p_hwfn->nvm_info.image_att[i].image_type)
  1394. break;
  1395. if (i == p_hwfn->nvm_info.num_images) {
  1396. DP_ERR(cdev, "Failed to find nvram image of type %08x\n",
  1397. image_type);
  1398. return -ENOENT;
  1399. }
  1400. nvm_image.start_addr = p_hwfn->nvm_info.image_att[i].nvm_start_addr;
  1401. nvm_image.length = p_hwfn->nvm_info.image_att[i].len;
  1402. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1403. "Read image %02x; type = %08x; NVM [%08x,...,%08x]\n",
  1404. **data, image_type, nvm_image.start_addr,
  1405. nvm_image.start_addr + nvm_image.length - 1);
  1406. (*data)++;
  1407. is_crc = !!(**data & BIT(0));
  1408. (*data)++;
  1409. len = *((u16 *)*data);
  1410. *data += 2;
  1411. if (is_crc) {
  1412. u32 crc = 0;
  1413. rc = qed_nvm_flash_image_access_crc(cdev, &nvm_image, &crc);
  1414. if (rc) {
  1415. DP_ERR(cdev, "Failed calculating CRC, rc = %d\n", rc);
  1416. goto exit;
  1417. }
  1418. rc = qed_mcp_nvm_write(cdev, QED_NVM_WRITE_NVRAM,
  1419. (nvm_image.start_addr +
  1420. nvm_image.length - 4), (u8 *)&crc, 4);
  1421. if (rc)
  1422. DP_ERR(cdev, "Failed writing to %08x, rc = %d\n",
  1423. nvm_image.start_addr + nvm_image.length - 4, rc);
  1424. goto exit;
  1425. }
  1426. /* Iterate over the values for setting */
  1427. while (len) {
  1428. u32 offset, mask, value, cur_value;
  1429. u8 buf[4];
  1430. value = *((u32 *)*data);
  1431. *data += 4;
  1432. mask = *((u32 *)*data);
  1433. *data += 4;
  1434. offset = *((u32 *)*data);
  1435. *data += 4;
  1436. rc = qed_mcp_nvm_read(cdev, nvm_image.start_addr + offset, buf,
  1437. 4);
  1438. if (rc) {
  1439. DP_ERR(cdev, "Failed reading from %08x\n",
  1440. nvm_image.start_addr + offset);
  1441. goto exit;
  1442. }
  1443. cur_value = le32_to_cpu(*((__le32 *)buf));
  1444. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1445. "NVM %08x: %08x -> %08x [Value %08x Mask %08x]\n",
  1446. nvm_image.start_addr + offset, cur_value,
  1447. (cur_value & ~mask) | (value & mask), value, mask);
  1448. value = (value & mask) | (cur_value & ~mask);
  1449. rc = qed_mcp_nvm_write(cdev, QED_NVM_WRITE_NVRAM,
  1450. nvm_image.start_addr + offset,
  1451. (u8 *)&value, 4);
  1452. if (rc) {
  1453. DP_ERR(cdev, "Failed writing to %08x\n",
  1454. nvm_image.start_addr + offset);
  1455. goto exit;
  1456. }
  1457. len--;
  1458. }
  1459. exit:
  1460. return rc;
  1461. }
  1462. /* Binary file format -
  1463. * /----------------------------------------------------------------------\
  1464. * 0B | 0x3 [command index] |
  1465. * 4B | b'0: check_response? | b'1-31 reserved |
  1466. * 8B | File-type | reserved |
  1467. * \----------------------------------------------------------------------/
  1468. * Start a new file of the provided type
  1469. */
  1470. static int qed_nvm_flash_image_file_start(struct qed_dev *cdev,
  1471. const u8 **data, bool *check_resp)
  1472. {
  1473. int rc;
  1474. *data += 4;
  1475. *check_resp = !!(**data & BIT(0));
  1476. *data += 4;
  1477. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1478. "About to start a new file of type %02x\n", **data);
  1479. rc = qed_mcp_nvm_put_file_begin(cdev, **data);
  1480. *data += 4;
  1481. return rc;
  1482. }
  1483. /* Binary file format -
  1484. * /----------------------------------------------------------------------\
  1485. * 0B | 0x2 [command index] |
  1486. * 4B | Length in bytes |
  1487. * 8B | b'0: check_response? | b'1-31 reserved |
  1488. * 12B | Offset in bytes |
  1489. * 16B | Data ... |
  1490. * \----------------------------------------------------------------------/
  1491. * Write data as part of a file that was previously started. Data should be
  1492. * of length equal to that provided in the message
  1493. */
  1494. static int qed_nvm_flash_image_file_data(struct qed_dev *cdev,
  1495. const u8 **data, bool *check_resp)
  1496. {
  1497. u32 offset, len;
  1498. int rc;
  1499. *data += 4;
  1500. len = *((u32 *)(*data));
  1501. *data += 4;
  1502. *check_resp = !!(**data & BIT(0));
  1503. *data += 4;
  1504. offset = *((u32 *)(*data));
  1505. *data += 4;
  1506. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1507. "About to write File-data: %08x bytes to offset %08x\n",
  1508. len, offset);
  1509. rc = qed_mcp_nvm_write(cdev, QED_PUT_FILE_DATA, offset,
  1510. (char *)(*data), len);
  1511. *data += len;
  1512. return rc;
  1513. }
  1514. /* Binary file format [General header] -
  1515. * /----------------------------------------------------------------------\
  1516. * 0B | QED_NVM_SIGNATURE |
  1517. * 4B | Length in bytes |
  1518. * 8B | Highest command in this batchfile | Reserved |
  1519. * \----------------------------------------------------------------------/
  1520. */
  1521. static int qed_nvm_flash_image_validate(struct qed_dev *cdev,
  1522. const struct firmware *image,
  1523. const u8 **data)
  1524. {
  1525. u32 signature, len;
  1526. /* Check minimum size */
  1527. if (image->size < 12) {
  1528. DP_ERR(cdev, "Image is too short [%08x]\n", (u32)image->size);
  1529. return -EINVAL;
  1530. }
  1531. /* Check signature */
  1532. signature = *((u32 *)(*data));
  1533. if (signature != QED_NVM_SIGNATURE) {
  1534. DP_ERR(cdev, "Wrong signature '%08x'\n", signature);
  1535. return -EINVAL;
  1536. }
  1537. *data += 4;
  1538. /* Validate internal size equals the image-size */
  1539. len = *((u32 *)(*data));
  1540. if (len != image->size) {
  1541. DP_ERR(cdev, "Size mismatch: internal = %08x image = %08x\n",
  1542. len, (u32)image->size);
  1543. return -EINVAL;
  1544. }
  1545. *data += 4;
  1546. /* Make sure driver familiar with all commands necessary for this */
  1547. if (*((u16 *)(*data)) >= QED_NVM_FLASH_CMD_NVM_MAX) {
  1548. DP_ERR(cdev, "File contains unsupported commands [Need %04x]\n",
  1549. *((u16 *)(*data)));
  1550. return -EINVAL;
  1551. }
  1552. *data += 4;
  1553. return 0;
  1554. }
  1555. static int qed_nvm_flash(struct qed_dev *cdev, const char *name)
  1556. {
  1557. const struct firmware *image;
  1558. const u8 *data, *data_end;
  1559. u32 cmd_type;
  1560. int rc;
  1561. rc = request_firmware(&image, name, &cdev->pdev->dev);
  1562. if (rc) {
  1563. DP_ERR(cdev, "Failed to find '%s'\n", name);
  1564. return rc;
  1565. }
  1566. DP_VERBOSE(cdev, NETIF_MSG_DRV,
  1567. "Flashing '%s' - firmware's data at %p, size is %08x\n",
  1568. name, image->data, (u32)image->size);
  1569. data = image->data;
  1570. data_end = data + image->size;
  1571. rc = qed_nvm_flash_image_validate(cdev, image, &data);
  1572. if (rc)
  1573. goto exit;
  1574. while (data < data_end) {
  1575. bool check_resp = false;
  1576. /* Parse the actual command */
  1577. cmd_type = *((u32 *)data);
  1578. switch (cmd_type) {
  1579. case QED_NVM_FLASH_CMD_FILE_DATA:
  1580. rc = qed_nvm_flash_image_file_data(cdev, &data,
  1581. &check_resp);
  1582. break;
  1583. case QED_NVM_FLASH_CMD_FILE_START:
  1584. rc = qed_nvm_flash_image_file_start(cdev, &data,
  1585. &check_resp);
  1586. break;
  1587. case QED_NVM_FLASH_CMD_NVM_CHANGE:
  1588. rc = qed_nvm_flash_image_access(cdev, &data,
  1589. &check_resp);
  1590. break;
  1591. default:
  1592. DP_ERR(cdev, "Unknown command %08x\n", cmd_type);
  1593. rc = -EINVAL;
  1594. goto exit;
  1595. }
  1596. if (rc) {
  1597. DP_ERR(cdev, "Command %08x failed\n", cmd_type);
  1598. goto exit;
  1599. }
  1600. /* Check response if needed */
  1601. if (check_resp) {
  1602. u32 mcp_response = 0;
  1603. if (qed_mcp_nvm_resp(cdev, (u8 *)&mcp_response)) {
  1604. DP_ERR(cdev, "Failed getting MCP response\n");
  1605. rc = -EINVAL;
  1606. goto exit;
  1607. }
  1608. switch (mcp_response & FW_MSG_CODE_MASK) {
  1609. case FW_MSG_CODE_OK:
  1610. case FW_MSG_CODE_NVM_OK:
  1611. case FW_MSG_CODE_NVM_PUT_FILE_FINISH_OK:
  1612. case FW_MSG_CODE_PHY_OK:
  1613. break;
  1614. default:
  1615. DP_ERR(cdev, "MFW returns error: %08x\n",
  1616. mcp_response);
  1617. rc = -EINVAL;
  1618. goto exit;
  1619. }
  1620. }
  1621. }
  1622. exit:
  1623. release_firmware(image);
  1624. return rc;
  1625. }
  1626. static int qed_nvm_get_image(struct qed_dev *cdev, enum qed_nvm_images type,
  1627. u8 *buf, u16 len)
  1628. {
  1629. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1630. return qed_mcp_get_nvm_image(hwfn, type, buf, len);
  1631. }
  1632. static int qed_set_coalesce(struct qed_dev *cdev, u16 rx_coal, u16 tx_coal,
  1633. void *handle)
  1634. {
  1635. return qed_set_queue_coalesce(rx_coal, tx_coal, handle);
  1636. }
  1637. static int qed_set_led(struct qed_dev *cdev, enum qed_led_mode mode)
  1638. {
  1639. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1640. struct qed_ptt *ptt;
  1641. int status = 0;
  1642. ptt = qed_ptt_acquire(hwfn);
  1643. if (!ptt)
  1644. return -EAGAIN;
  1645. status = qed_mcp_set_led(hwfn, ptt, mode);
  1646. qed_ptt_release(hwfn, ptt);
  1647. return status;
  1648. }
  1649. static int qed_update_wol(struct qed_dev *cdev, bool enabled)
  1650. {
  1651. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1652. struct qed_ptt *ptt;
  1653. int rc = 0;
  1654. if (IS_VF(cdev))
  1655. return 0;
  1656. ptt = qed_ptt_acquire(hwfn);
  1657. if (!ptt)
  1658. return -EAGAIN;
  1659. rc = qed_mcp_ov_update_wol(hwfn, ptt, enabled ? QED_OV_WOL_ENABLED
  1660. : QED_OV_WOL_DISABLED);
  1661. if (rc)
  1662. goto out;
  1663. rc = qed_mcp_ov_update_current_config(hwfn, ptt, QED_OV_CLIENT_DRV);
  1664. out:
  1665. qed_ptt_release(hwfn, ptt);
  1666. return rc;
  1667. }
  1668. static int qed_update_drv_state(struct qed_dev *cdev, bool active)
  1669. {
  1670. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1671. struct qed_ptt *ptt;
  1672. int status = 0;
  1673. if (IS_VF(cdev))
  1674. return 0;
  1675. ptt = qed_ptt_acquire(hwfn);
  1676. if (!ptt)
  1677. return -EAGAIN;
  1678. status = qed_mcp_ov_update_driver_state(hwfn, ptt, active ?
  1679. QED_OV_DRIVER_STATE_ACTIVE :
  1680. QED_OV_DRIVER_STATE_DISABLED);
  1681. qed_ptt_release(hwfn, ptt);
  1682. return status;
  1683. }
  1684. static int qed_update_mac(struct qed_dev *cdev, u8 *mac)
  1685. {
  1686. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1687. struct qed_ptt *ptt;
  1688. int status = 0;
  1689. if (IS_VF(cdev))
  1690. return 0;
  1691. ptt = qed_ptt_acquire(hwfn);
  1692. if (!ptt)
  1693. return -EAGAIN;
  1694. status = qed_mcp_ov_update_mac(hwfn, ptt, mac);
  1695. if (status)
  1696. goto out;
  1697. status = qed_mcp_ov_update_current_config(hwfn, ptt, QED_OV_CLIENT_DRV);
  1698. out:
  1699. qed_ptt_release(hwfn, ptt);
  1700. return status;
  1701. }
  1702. static int qed_update_mtu(struct qed_dev *cdev, u16 mtu)
  1703. {
  1704. struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
  1705. struct qed_ptt *ptt;
  1706. int status = 0;
  1707. if (IS_VF(cdev))
  1708. return 0;
  1709. ptt = qed_ptt_acquire(hwfn);
  1710. if (!ptt)
  1711. return -EAGAIN;
  1712. status = qed_mcp_ov_update_mtu(hwfn, ptt, mtu);
  1713. if (status)
  1714. goto out;
  1715. status = qed_mcp_ov_update_current_config(hwfn, ptt, QED_OV_CLIENT_DRV);
  1716. out:
  1717. qed_ptt_release(hwfn, ptt);
  1718. return status;
  1719. }
  1720. static struct qed_selftest_ops qed_selftest_ops_pass = {
  1721. .selftest_memory = &qed_selftest_memory,
  1722. .selftest_interrupt = &qed_selftest_interrupt,
  1723. .selftest_register = &qed_selftest_register,
  1724. .selftest_clock = &qed_selftest_clock,
  1725. .selftest_nvram = &qed_selftest_nvram,
  1726. };
  1727. const struct qed_common_ops qed_common_ops_pass = {
  1728. .selftest = &qed_selftest_ops_pass,
  1729. .probe = &qed_probe,
  1730. .remove = &qed_remove,
  1731. .set_power_state = &qed_set_power_state,
  1732. .set_name = &qed_set_name,
  1733. .update_pf_params = &qed_update_pf_params,
  1734. .slowpath_start = &qed_slowpath_start,
  1735. .slowpath_stop = &qed_slowpath_stop,
  1736. .set_fp_int = &qed_set_int_fp,
  1737. .get_fp_int = &qed_get_int_fp,
  1738. .sb_init = &qed_sb_init,
  1739. .sb_release = &qed_sb_release,
  1740. .simd_handler_config = &qed_simd_handler_config,
  1741. .simd_handler_clean = &qed_simd_handler_clean,
  1742. .dbg_grc = &qed_dbg_grc,
  1743. .dbg_grc_size = &qed_dbg_grc_size,
  1744. .can_link_change = &qed_can_link_change,
  1745. .set_link = &qed_set_link,
  1746. .get_link = &qed_get_current_link,
  1747. .drain = &qed_drain,
  1748. .update_msglvl = &qed_init_dp,
  1749. .dbg_all_data = &qed_dbg_all_data,
  1750. .dbg_all_data_size = &qed_dbg_all_data_size,
  1751. .chain_alloc = &qed_chain_alloc,
  1752. .chain_free = &qed_chain_free,
  1753. .nvm_flash = &qed_nvm_flash,
  1754. .nvm_get_image = &qed_nvm_get_image,
  1755. .set_coalesce = &qed_set_coalesce,
  1756. .set_led = &qed_set_led,
  1757. .update_drv_state = &qed_update_drv_state,
  1758. .update_mac = &qed_update_mac,
  1759. .update_mtu = &qed_update_mtu,
  1760. .update_wol = &qed_update_wol,
  1761. };
  1762. void qed_get_protocol_stats(struct qed_dev *cdev,
  1763. enum qed_mcp_protocol_type type,
  1764. union qed_mcp_protocol_stats *stats)
  1765. {
  1766. struct qed_eth_stats eth_stats;
  1767. memset(stats, 0, sizeof(*stats));
  1768. switch (type) {
  1769. case QED_MCP_LAN_STATS:
  1770. qed_get_vport_stats(cdev, &eth_stats);
  1771. stats->lan_stats.ucast_rx_pkts =
  1772. eth_stats.common.rx_ucast_pkts;
  1773. stats->lan_stats.ucast_tx_pkts =
  1774. eth_stats.common.tx_ucast_pkts;
  1775. stats->lan_stats.fcs_err = -1;
  1776. break;
  1777. case QED_MCP_FCOE_STATS:
  1778. qed_get_protocol_stats_fcoe(cdev, &stats->fcoe_stats);
  1779. break;
  1780. case QED_MCP_ISCSI_STATS:
  1781. qed_get_protocol_stats_iscsi(cdev, &stats->iscsi_stats);
  1782. break;
  1783. default:
  1784. DP_VERBOSE(cdev, QED_MSG_SP,
  1785. "Invalid protocol type = %d\n", type);
  1786. return;
  1787. }
  1788. }
  1789. int qed_mfw_tlv_req(struct qed_hwfn *hwfn)
  1790. {
  1791. DP_VERBOSE(hwfn->cdev, NETIF_MSG_DRV,
  1792. "Scheduling slowpath task [Flag: %d]\n",
  1793. QED_SLOWPATH_MFW_TLV_REQ);
  1794. smp_mb__before_atomic();
  1795. set_bit(QED_SLOWPATH_MFW_TLV_REQ, &hwfn->slowpath_task_flags);
  1796. smp_mb__after_atomic();
  1797. queue_delayed_work(hwfn->slowpath_wq, &hwfn->slowpath_task, 0);
  1798. return 0;
  1799. }
  1800. static void
  1801. qed_fill_generic_tlv_data(struct qed_dev *cdev, struct qed_mfw_tlv_generic *tlv)
  1802. {
  1803. struct qed_common_cb_ops *op = cdev->protocol_ops.common;
  1804. struct qed_eth_stats_common *p_common;
  1805. struct qed_generic_tlvs gen_tlvs;
  1806. struct qed_eth_stats stats;
  1807. int i;
  1808. memset(&gen_tlvs, 0, sizeof(gen_tlvs));
  1809. op->get_generic_tlv_data(cdev->ops_cookie, &gen_tlvs);
  1810. if (gen_tlvs.feat_flags & QED_TLV_IP_CSUM)
  1811. tlv->flags.ipv4_csum_offload = true;
  1812. if (gen_tlvs.feat_flags & QED_TLV_LSO)
  1813. tlv->flags.lso_supported = true;
  1814. tlv->flags.b_set = true;
  1815. for (i = 0; i < QED_TLV_MAC_COUNT; i++) {
  1816. if (is_valid_ether_addr(gen_tlvs.mac[i])) {
  1817. ether_addr_copy(tlv->mac[i], gen_tlvs.mac[i]);
  1818. tlv->mac_set[i] = true;
  1819. }
  1820. }
  1821. qed_get_vport_stats(cdev, &stats);
  1822. p_common = &stats.common;
  1823. tlv->rx_frames = p_common->rx_ucast_pkts + p_common->rx_mcast_pkts +
  1824. p_common->rx_bcast_pkts;
  1825. tlv->rx_frames_set = true;
  1826. tlv->rx_bytes = p_common->rx_ucast_bytes + p_common->rx_mcast_bytes +
  1827. p_common->rx_bcast_bytes;
  1828. tlv->rx_bytes_set = true;
  1829. tlv->tx_frames = p_common->tx_ucast_pkts + p_common->tx_mcast_pkts +
  1830. p_common->tx_bcast_pkts;
  1831. tlv->tx_frames_set = true;
  1832. tlv->tx_bytes = p_common->tx_ucast_bytes + p_common->tx_mcast_bytes +
  1833. p_common->tx_bcast_bytes;
  1834. tlv->rx_bytes_set = true;
  1835. }
  1836. int qed_mfw_fill_tlv_data(struct qed_hwfn *hwfn, enum qed_mfw_tlv_type type,
  1837. union qed_mfw_tlv_data *tlv_buf)
  1838. {
  1839. struct qed_dev *cdev = hwfn->cdev;
  1840. struct qed_common_cb_ops *ops;
  1841. ops = cdev->protocol_ops.common;
  1842. if (!ops || !ops->get_protocol_tlv_data || !ops->get_generic_tlv_data) {
  1843. DP_NOTICE(hwfn, "Can't collect TLV management info\n");
  1844. return -EINVAL;
  1845. }
  1846. switch (type) {
  1847. case QED_MFW_TLV_GENERIC:
  1848. qed_fill_generic_tlv_data(hwfn->cdev, &tlv_buf->generic);
  1849. break;
  1850. case QED_MFW_TLV_ETH:
  1851. ops->get_protocol_tlv_data(cdev->ops_cookie, &tlv_buf->eth);
  1852. break;
  1853. case QED_MFW_TLV_FCOE:
  1854. ops->get_protocol_tlv_data(cdev->ops_cookie, &tlv_buf->fcoe);
  1855. break;
  1856. case QED_MFW_TLV_ISCSI:
  1857. ops->get_protocol_tlv_data(cdev->ops_cookie, &tlv_buf->iscsi);
  1858. break;
  1859. default:
  1860. break;
  1861. }
  1862. return 0;
  1863. }