ice_main.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. /* Intel(R) Ethernet Connection E800 Series Linux Driver */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include "ice.h"
  6. #include "ice_lib.h"
  7. #define DRV_VERSION "0.7.1-k"
  8. #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
  9. const char ice_drv_ver[] = DRV_VERSION;
  10. static const char ice_driver_string[] = DRV_SUMMARY;
  11. static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
  12. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  13. MODULE_DESCRIPTION(DRV_SUMMARY);
  14. MODULE_LICENSE("GPL v2");
  15. MODULE_VERSION(DRV_VERSION);
  16. static int debug = -1;
  17. module_param(debug, int, 0644);
  18. #ifndef CONFIG_DYNAMIC_DEBUG
  19. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
  20. #else
  21. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
  22. #endif /* !CONFIG_DYNAMIC_DEBUG */
  23. static struct workqueue_struct *ice_wq;
  24. static const struct net_device_ops ice_netdev_ops;
  25. static void ice_pf_dis_all_vsi(struct ice_pf *pf);
  26. static void ice_rebuild(struct ice_pf *pf);
  27. static void ice_vsi_release_all(struct ice_pf *pf);
  28. static void ice_update_vsi_stats(struct ice_vsi *vsi);
  29. static void ice_update_pf_stats(struct ice_pf *pf);
  30. /**
  31. * ice_get_tx_pending - returns number of Tx descriptors not processed
  32. * @ring: the ring of descriptors
  33. */
  34. static u32 ice_get_tx_pending(struct ice_ring *ring)
  35. {
  36. u32 head, tail;
  37. head = ring->next_to_clean;
  38. tail = readl(ring->tail);
  39. if (head != tail)
  40. return (head < tail) ?
  41. tail - head : (tail + ring->count - head);
  42. return 0;
  43. }
  44. /**
  45. * ice_check_for_hang_subtask - check for and recover hung queues
  46. * @pf: pointer to PF struct
  47. */
  48. static void ice_check_for_hang_subtask(struct ice_pf *pf)
  49. {
  50. struct ice_vsi *vsi = NULL;
  51. unsigned int i;
  52. u32 v, v_idx;
  53. int packets;
  54. ice_for_each_vsi(pf, v)
  55. if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
  56. vsi = pf->vsi[v];
  57. break;
  58. }
  59. if (!vsi || test_bit(__ICE_DOWN, vsi->state))
  60. return;
  61. if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
  62. return;
  63. for (i = 0; i < vsi->num_txq; i++) {
  64. struct ice_ring *tx_ring = vsi->tx_rings[i];
  65. if (tx_ring && tx_ring->desc) {
  66. int itr = ICE_ITR_NONE;
  67. /* If packet counter has not changed the queue is
  68. * likely stalled, so force an interrupt for this
  69. * queue.
  70. *
  71. * prev_pkt would be negative if there was no
  72. * pending work.
  73. */
  74. packets = tx_ring->stats.pkts & INT_MAX;
  75. if (tx_ring->tx_stats.prev_pkt == packets) {
  76. /* Trigger sw interrupt to revive the queue */
  77. v_idx = tx_ring->q_vector->v_idx;
  78. wr32(&vsi->back->hw,
  79. GLINT_DYN_CTL(vsi->hw_base_vector + v_idx),
  80. (itr << GLINT_DYN_CTL_ITR_INDX_S) |
  81. GLINT_DYN_CTL_SWINT_TRIG_M |
  82. GLINT_DYN_CTL_INTENA_MSK_M);
  83. continue;
  84. }
  85. /* Memory barrier between read of packet count and call
  86. * to ice_get_tx_pending()
  87. */
  88. smp_rmb();
  89. tx_ring->tx_stats.prev_pkt =
  90. ice_get_tx_pending(tx_ring) ? packets : -1;
  91. }
  92. }
  93. }
  94. /**
  95. * ice_add_mac_to_sync_list - creates list of mac addresses to be synced
  96. * @netdev: the net device on which the sync is happening
  97. * @addr: mac address to sync
  98. *
  99. * This is a callback function which is called by the in kernel device sync
  100. * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
  101. * populates the tmp_sync_list, which is later used by ice_add_mac to add the
  102. * mac filters from the hardware.
  103. */
  104. static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
  105. {
  106. struct ice_netdev_priv *np = netdev_priv(netdev);
  107. struct ice_vsi *vsi = np->vsi;
  108. if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
  109. return -EINVAL;
  110. return 0;
  111. }
  112. /**
  113. * ice_add_mac_to_unsync_list - creates list of mac addresses to be unsynced
  114. * @netdev: the net device on which the unsync is happening
  115. * @addr: mac address to unsync
  116. *
  117. * This is a callback function which is called by the in kernel device unsync
  118. * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
  119. * populates the tmp_unsync_list, which is later used by ice_remove_mac to
  120. * delete the mac filters from the hardware.
  121. */
  122. static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
  123. {
  124. struct ice_netdev_priv *np = netdev_priv(netdev);
  125. struct ice_vsi *vsi = np->vsi;
  126. if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
  127. return -EINVAL;
  128. return 0;
  129. }
  130. /**
  131. * ice_vsi_fltr_changed - check if filter state changed
  132. * @vsi: VSI to be checked
  133. *
  134. * returns true if filter state has changed, false otherwise.
  135. */
  136. static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
  137. {
  138. return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
  139. test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
  140. test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
  141. }
  142. /**
  143. * ice_vsi_sync_fltr - Update the VSI filter list to the HW
  144. * @vsi: ptr to the VSI
  145. *
  146. * Push any outstanding VSI filter changes through the AdminQ.
  147. */
  148. static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
  149. {
  150. struct device *dev = &vsi->back->pdev->dev;
  151. struct net_device *netdev = vsi->netdev;
  152. bool promisc_forced_on = false;
  153. struct ice_pf *pf = vsi->back;
  154. struct ice_hw *hw = &pf->hw;
  155. enum ice_status status = 0;
  156. u32 changed_flags = 0;
  157. int err = 0;
  158. if (!vsi->netdev)
  159. return -EINVAL;
  160. while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
  161. usleep_range(1000, 2000);
  162. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  163. vsi->current_netdev_flags = vsi->netdev->flags;
  164. INIT_LIST_HEAD(&vsi->tmp_sync_list);
  165. INIT_LIST_HEAD(&vsi->tmp_unsync_list);
  166. if (ice_vsi_fltr_changed(vsi)) {
  167. clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  168. clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  169. clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
  170. /* grab the netdev's addr_list_lock */
  171. netif_addr_lock_bh(netdev);
  172. __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
  173. ice_add_mac_to_unsync_list);
  174. __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
  175. ice_add_mac_to_unsync_list);
  176. /* our temp lists are populated. release lock */
  177. netif_addr_unlock_bh(netdev);
  178. }
  179. /* Remove mac addresses in the unsync list */
  180. status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
  181. ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
  182. if (status) {
  183. netdev_err(netdev, "Failed to delete MAC filters\n");
  184. /* if we failed because of alloc failures, just bail */
  185. if (status == ICE_ERR_NO_MEMORY) {
  186. err = -ENOMEM;
  187. goto out;
  188. }
  189. }
  190. /* Add mac addresses in the sync list */
  191. status = ice_add_mac(hw, &vsi->tmp_sync_list);
  192. ice_free_fltr_list(dev, &vsi->tmp_sync_list);
  193. if (status) {
  194. netdev_err(netdev, "Failed to add MAC filters\n");
  195. /* If there is no more space for new umac filters, vsi
  196. * should go into promiscuous mode. There should be some
  197. * space reserved for promiscuous filters.
  198. */
  199. if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
  200. !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
  201. vsi->state)) {
  202. promisc_forced_on = true;
  203. netdev_warn(netdev,
  204. "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
  205. vsi->vsi_num);
  206. } else {
  207. err = -EIO;
  208. goto out;
  209. }
  210. }
  211. /* check for changes in promiscuous modes */
  212. if (changed_flags & IFF_ALLMULTI)
  213. netdev_warn(netdev, "Unsupported configuration\n");
  214. if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
  215. test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
  216. clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
  217. if (vsi->current_netdev_flags & IFF_PROMISC) {
  218. /* Apply TX filter rule to get traffic from VMs */
  219. status = ice_cfg_dflt_vsi(hw, vsi->idx, true,
  220. ICE_FLTR_TX);
  221. if (status) {
  222. netdev_err(netdev, "Error setting default VSI %i tx rule\n",
  223. vsi->vsi_num);
  224. vsi->current_netdev_flags &= ~IFF_PROMISC;
  225. err = -EIO;
  226. goto out_promisc;
  227. }
  228. /* Apply RX filter rule to get traffic from wire */
  229. status = ice_cfg_dflt_vsi(hw, vsi->idx, true,
  230. ICE_FLTR_RX);
  231. if (status) {
  232. netdev_err(netdev, "Error setting default VSI %i rx rule\n",
  233. vsi->vsi_num);
  234. vsi->current_netdev_flags &= ~IFF_PROMISC;
  235. err = -EIO;
  236. goto out_promisc;
  237. }
  238. } else {
  239. /* Clear TX filter rule to stop traffic from VMs */
  240. status = ice_cfg_dflt_vsi(hw, vsi->idx, false,
  241. ICE_FLTR_TX);
  242. if (status) {
  243. netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
  244. vsi->vsi_num);
  245. vsi->current_netdev_flags |= IFF_PROMISC;
  246. err = -EIO;
  247. goto out_promisc;
  248. }
  249. /* Clear RX filter to remove traffic from wire */
  250. status = ice_cfg_dflt_vsi(hw, vsi->idx, false,
  251. ICE_FLTR_RX);
  252. if (status) {
  253. netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
  254. vsi->vsi_num);
  255. vsi->current_netdev_flags |= IFF_PROMISC;
  256. err = -EIO;
  257. goto out_promisc;
  258. }
  259. }
  260. }
  261. goto exit;
  262. out_promisc:
  263. set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
  264. goto exit;
  265. out:
  266. /* if something went wrong then set the changed flag so we try again */
  267. set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  268. set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  269. exit:
  270. clear_bit(__ICE_CFG_BUSY, vsi->state);
  271. return err;
  272. }
  273. /**
  274. * ice_sync_fltr_subtask - Sync the VSI filter list with HW
  275. * @pf: board private structure
  276. */
  277. static void ice_sync_fltr_subtask(struct ice_pf *pf)
  278. {
  279. int v;
  280. if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
  281. return;
  282. clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  283. for (v = 0; v < pf->num_alloc_vsi; v++)
  284. if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
  285. ice_vsi_sync_fltr(pf->vsi[v])) {
  286. /* come back and try again later */
  287. set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  288. break;
  289. }
  290. }
  291. /**
  292. * ice_prepare_for_reset - prep for the core to reset
  293. * @pf: board private structure
  294. *
  295. * Inform or close all dependent features in prep for reset.
  296. */
  297. static void
  298. ice_prepare_for_reset(struct ice_pf *pf)
  299. {
  300. struct ice_hw *hw = &pf->hw;
  301. /* disable the VSIs and their queues that are not already DOWN */
  302. ice_pf_dis_all_vsi(pf);
  303. ice_shutdown_all_ctrlq(hw);
  304. set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
  305. }
  306. /**
  307. * ice_do_reset - Initiate one of many types of resets
  308. * @pf: board private structure
  309. * @reset_type: reset type requested
  310. * before this function was called.
  311. */
  312. static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
  313. {
  314. struct device *dev = &pf->pdev->dev;
  315. struct ice_hw *hw = &pf->hw;
  316. dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
  317. WARN_ON(in_interrupt());
  318. ice_prepare_for_reset(pf);
  319. /* trigger the reset */
  320. if (ice_reset(hw, reset_type)) {
  321. dev_err(dev, "reset %d failed\n", reset_type);
  322. set_bit(__ICE_RESET_FAILED, pf->state);
  323. clear_bit(__ICE_RESET_OICR_RECV, pf->state);
  324. clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
  325. clear_bit(__ICE_PFR_REQ, pf->state);
  326. clear_bit(__ICE_CORER_REQ, pf->state);
  327. clear_bit(__ICE_GLOBR_REQ, pf->state);
  328. return;
  329. }
  330. /* PFR is a bit of a special case because it doesn't result in an OICR
  331. * interrupt. So for PFR, rebuild after the reset and clear the reset-
  332. * associated state bits.
  333. */
  334. if (reset_type == ICE_RESET_PFR) {
  335. pf->pfr_count++;
  336. ice_rebuild(pf);
  337. clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
  338. clear_bit(__ICE_PFR_REQ, pf->state);
  339. }
  340. }
  341. /**
  342. * ice_reset_subtask - Set up for resetting the device and driver
  343. * @pf: board private structure
  344. */
  345. static void ice_reset_subtask(struct ice_pf *pf)
  346. {
  347. enum ice_reset_req reset_type = ICE_RESET_INVAL;
  348. /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
  349. * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
  350. * of reset is pending and sets bits in pf->state indicating the reset
  351. * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
  352. * prepare for pending reset if not already (for PF software-initiated
  353. * global resets the software should already be prepared for it as
  354. * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
  355. * by firmware or software on other PFs, that bit is not set so prepare
  356. * for the reset now), poll for reset done, rebuild and return.
  357. */
  358. if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
  359. clear_bit(__ICE_GLOBR_RECV, pf->state);
  360. clear_bit(__ICE_CORER_RECV, pf->state);
  361. if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
  362. ice_prepare_for_reset(pf);
  363. /* make sure we are ready to rebuild */
  364. if (ice_check_reset(&pf->hw)) {
  365. set_bit(__ICE_RESET_FAILED, pf->state);
  366. } else {
  367. /* done with reset. start rebuild */
  368. pf->hw.reset_ongoing = false;
  369. ice_rebuild(pf);
  370. /* clear bit to resume normal operations, but
  371. * ICE_NEEDS_RESTART bit is set incase rebuild failed
  372. */
  373. clear_bit(__ICE_RESET_OICR_RECV, pf->state);
  374. clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
  375. clear_bit(__ICE_PFR_REQ, pf->state);
  376. clear_bit(__ICE_CORER_REQ, pf->state);
  377. clear_bit(__ICE_GLOBR_REQ, pf->state);
  378. }
  379. return;
  380. }
  381. /* No pending resets to finish processing. Check for new resets */
  382. if (test_bit(__ICE_PFR_REQ, pf->state))
  383. reset_type = ICE_RESET_PFR;
  384. if (test_bit(__ICE_CORER_REQ, pf->state))
  385. reset_type = ICE_RESET_CORER;
  386. if (test_bit(__ICE_GLOBR_REQ, pf->state))
  387. reset_type = ICE_RESET_GLOBR;
  388. /* If no valid reset type requested just return */
  389. if (reset_type == ICE_RESET_INVAL)
  390. return;
  391. /* reset if not already down or busy */
  392. if (!test_bit(__ICE_DOWN, pf->state) &&
  393. !test_bit(__ICE_CFG_BUSY, pf->state)) {
  394. ice_do_reset(pf, reset_type);
  395. }
  396. }
  397. /**
  398. * ice_watchdog_subtask - periodic tasks not using event driven scheduling
  399. * @pf: board private structure
  400. */
  401. static void ice_watchdog_subtask(struct ice_pf *pf)
  402. {
  403. int i;
  404. /* if interface is down do nothing */
  405. if (test_bit(__ICE_DOWN, pf->state) ||
  406. test_bit(__ICE_CFG_BUSY, pf->state))
  407. return;
  408. /* make sure we don't do these things too often */
  409. if (time_before(jiffies,
  410. pf->serv_tmr_prev + pf->serv_tmr_period))
  411. return;
  412. pf->serv_tmr_prev = jiffies;
  413. /* Update the stats for active netdevs so the network stack
  414. * can look at updated numbers whenever it cares to
  415. */
  416. ice_update_pf_stats(pf);
  417. for (i = 0; i < pf->num_alloc_vsi; i++)
  418. if (pf->vsi[i] && pf->vsi[i]->netdev)
  419. ice_update_vsi_stats(pf->vsi[i]);
  420. }
  421. /**
  422. * ice_print_link_msg - print link up or down message
  423. * @vsi: the VSI whose link status is being queried
  424. * @isup: boolean for if the link is now up or down
  425. */
  426. void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
  427. {
  428. const char *speed;
  429. const char *fc;
  430. if (vsi->current_isup == isup)
  431. return;
  432. vsi->current_isup = isup;
  433. if (!isup) {
  434. netdev_info(vsi->netdev, "NIC Link is Down\n");
  435. return;
  436. }
  437. switch (vsi->port_info->phy.link_info.link_speed) {
  438. case ICE_AQ_LINK_SPEED_40GB:
  439. speed = "40 G";
  440. break;
  441. case ICE_AQ_LINK_SPEED_25GB:
  442. speed = "25 G";
  443. break;
  444. case ICE_AQ_LINK_SPEED_20GB:
  445. speed = "20 G";
  446. break;
  447. case ICE_AQ_LINK_SPEED_10GB:
  448. speed = "10 G";
  449. break;
  450. case ICE_AQ_LINK_SPEED_5GB:
  451. speed = "5 G";
  452. break;
  453. case ICE_AQ_LINK_SPEED_2500MB:
  454. speed = "2.5 G";
  455. break;
  456. case ICE_AQ_LINK_SPEED_1000MB:
  457. speed = "1 G";
  458. break;
  459. case ICE_AQ_LINK_SPEED_100MB:
  460. speed = "100 M";
  461. break;
  462. default:
  463. speed = "Unknown";
  464. break;
  465. }
  466. switch (vsi->port_info->fc.current_mode) {
  467. case ICE_FC_FULL:
  468. fc = "RX/TX";
  469. break;
  470. case ICE_FC_TX_PAUSE:
  471. fc = "TX";
  472. break;
  473. case ICE_FC_RX_PAUSE:
  474. fc = "RX";
  475. break;
  476. default:
  477. fc = "Unknown";
  478. break;
  479. }
  480. netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
  481. speed, fc);
  482. }
  483. /**
  484. * ice_init_link_events - enable/initialize link events
  485. * @pi: pointer to the port_info instance
  486. *
  487. * Returns -EIO on failure, 0 on success
  488. */
  489. static int ice_init_link_events(struct ice_port_info *pi)
  490. {
  491. u16 mask;
  492. mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
  493. ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
  494. if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
  495. dev_dbg(ice_hw_to_dev(pi->hw),
  496. "Failed to set link event mask for port %d\n",
  497. pi->lport);
  498. return -EIO;
  499. }
  500. if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
  501. dev_dbg(ice_hw_to_dev(pi->hw),
  502. "Failed to enable link events for port %d\n",
  503. pi->lport);
  504. return -EIO;
  505. }
  506. return 0;
  507. }
  508. /**
  509. * ice_vsi_link_event - update the vsi's netdev
  510. * @vsi: the vsi on which the link event occurred
  511. * @link_up: whether or not the vsi needs to be set up or down
  512. */
  513. static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
  514. {
  515. if (!vsi || test_bit(__ICE_DOWN, vsi->state))
  516. return;
  517. if (vsi->type == ICE_VSI_PF) {
  518. if (!vsi->netdev) {
  519. dev_dbg(&vsi->back->pdev->dev,
  520. "vsi->netdev is not initialized!\n");
  521. return;
  522. }
  523. if (link_up) {
  524. netif_carrier_on(vsi->netdev);
  525. netif_tx_wake_all_queues(vsi->netdev);
  526. } else {
  527. netif_carrier_off(vsi->netdev);
  528. netif_tx_stop_all_queues(vsi->netdev);
  529. }
  530. }
  531. }
  532. /**
  533. * ice_link_event - process the link event
  534. * @pf: pf that the link event is associated with
  535. * @pi: port_info for the port that the link event is associated with
  536. *
  537. * Returns -EIO if ice_get_link_status() fails
  538. * Returns 0 on success
  539. */
  540. static int
  541. ice_link_event(struct ice_pf *pf, struct ice_port_info *pi)
  542. {
  543. u8 new_link_speed, old_link_speed;
  544. struct ice_phy_info *phy_info;
  545. bool new_link_same_as_old;
  546. bool new_link, old_link;
  547. u8 lport;
  548. u16 v;
  549. phy_info = &pi->phy;
  550. phy_info->link_info_old = phy_info->link_info;
  551. /* Force ice_get_link_status() to update link info */
  552. phy_info->get_link_info = true;
  553. old_link = (phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
  554. old_link_speed = phy_info->link_info_old.link_speed;
  555. lport = pi->lport;
  556. if (ice_get_link_status(pi, &new_link)) {
  557. dev_dbg(&pf->pdev->dev,
  558. "Could not get link status for port %d\n", lport);
  559. return -EIO;
  560. }
  561. new_link_speed = phy_info->link_info.link_speed;
  562. new_link_same_as_old = (new_link == old_link &&
  563. new_link_speed == old_link_speed);
  564. ice_for_each_vsi(pf, v) {
  565. struct ice_vsi *vsi = pf->vsi[v];
  566. if (!vsi || !vsi->port_info)
  567. continue;
  568. if (new_link_same_as_old &&
  569. (test_bit(__ICE_DOWN, vsi->state) ||
  570. new_link == netif_carrier_ok(vsi->netdev)))
  571. continue;
  572. if (vsi->port_info->lport == lport) {
  573. ice_print_link_msg(vsi, new_link);
  574. ice_vsi_link_event(vsi, new_link);
  575. }
  576. }
  577. return 0;
  578. }
  579. /**
  580. * ice_handle_link_event - handle link event via ARQ
  581. * @pf: pf that the link event is associated with
  582. *
  583. * Return -EINVAL if port_info is null
  584. * Return status on succes
  585. */
  586. static int ice_handle_link_event(struct ice_pf *pf)
  587. {
  588. struct ice_port_info *port_info;
  589. int status;
  590. port_info = pf->hw.port_info;
  591. if (!port_info)
  592. return -EINVAL;
  593. status = ice_link_event(pf, port_info);
  594. if (status)
  595. dev_dbg(&pf->pdev->dev,
  596. "Could not process link event, error %d\n", status);
  597. return status;
  598. }
  599. /**
  600. * __ice_clean_ctrlq - helper function to clean controlq rings
  601. * @pf: ptr to struct ice_pf
  602. * @q_type: specific Control queue type
  603. */
  604. static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
  605. {
  606. struct ice_rq_event_info event;
  607. struct ice_hw *hw = &pf->hw;
  608. struct ice_ctl_q_info *cq;
  609. u16 pending, i = 0;
  610. const char *qtype;
  611. u32 oldval, val;
  612. /* Do not clean control queue if/when PF reset fails */
  613. if (test_bit(__ICE_RESET_FAILED, pf->state))
  614. return 0;
  615. switch (q_type) {
  616. case ICE_CTL_Q_ADMIN:
  617. cq = &hw->adminq;
  618. qtype = "Admin";
  619. break;
  620. default:
  621. dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
  622. q_type);
  623. return 0;
  624. }
  625. /* check for error indications - PF_xx_AxQLEN register layout for
  626. * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
  627. */
  628. val = rd32(hw, cq->rq.len);
  629. if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  630. PF_FW_ARQLEN_ARQCRIT_M)) {
  631. oldval = val;
  632. if (val & PF_FW_ARQLEN_ARQVFE_M)
  633. dev_dbg(&pf->pdev->dev,
  634. "%s Receive Queue VF Error detected\n", qtype);
  635. if (val & PF_FW_ARQLEN_ARQOVFL_M) {
  636. dev_dbg(&pf->pdev->dev,
  637. "%s Receive Queue Overflow Error detected\n",
  638. qtype);
  639. }
  640. if (val & PF_FW_ARQLEN_ARQCRIT_M)
  641. dev_dbg(&pf->pdev->dev,
  642. "%s Receive Queue Critical Error detected\n",
  643. qtype);
  644. val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  645. PF_FW_ARQLEN_ARQCRIT_M);
  646. if (oldval != val)
  647. wr32(hw, cq->rq.len, val);
  648. }
  649. val = rd32(hw, cq->sq.len);
  650. if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  651. PF_FW_ATQLEN_ATQCRIT_M)) {
  652. oldval = val;
  653. if (val & PF_FW_ATQLEN_ATQVFE_M)
  654. dev_dbg(&pf->pdev->dev,
  655. "%s Send Queue VF Error detected\n", qtype);
  656. if (val & PF_FW_ATQLEN_ATQOVFL_M) {
  657. dev_dbg(&pf->pdev->dev,
  658. "%s Send Queue Overflow Error detected\n",
  659. qtype);
  660. }
  661. if (val & PF_FW_ATQLEN_ATQCRIT_M)
  662. dev_dbg(&pf->pdev->dev,
  663. "%s Send Queue Critical Error detected\n",
  664. qtype);
  665. val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  666. PF_FW_ATQLEN_ATQCRIT_M);
  667. if (oldval != val)
  668. wr32(hw, cq->sq.len, val);
  669. }
  670. event.buf_len = cq->rq_buf_size;
  671. event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
  672. GFP_KERNEL);
  673. if (!event.msg_buf)
  674. return 0;
  675. do {
  676. enum ice_status ret;
  677. u16 opcode;
  678. ret = ice_clean_rq_elem(hw, cq, &event, &pending);
  679. if (ret == ICE_ERR_AQ_NO_WORK)
  680. break;
  681. if (ret) {
  682. dev_err(&pf->pdev->dev,
  683. "%s Receive Queue event error %d\n", qtype,
  684. ret);
  685. break;
  686. }
  687. opcode = le16_to_cpu(event.desc.opcode);
  688. switch (opcode) {
  689. case ice_aqc_opc_get_link_status:
  690. if (ice_handle_link_event(pf))
  691. dev_err(&pf->pdev->dev,
  692. "Could not handle link event\n");
  693. break;
  694. case ice_aqc_opc_fw_logging:
  695. ice_output_fw_log(hw, &event.desc, event.msg_buf);
  696. break;
  697. default:
  698. dev_dbg(&pf->pdev->dev,
  699. "%s Receive Queue unknown event 0x%04x ignored\n",
  700. qtype, opcode);
  701. break;
  702. }
  703. } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
  704. devm_kfree(&pf->pdev->dev, event.msg_buf);
  705. return pending && (i == ICE_DFLT_IRQ_WORK);
  706. }
  707. /**
  708. * ice_ctrlq_pending - check if there is a difference between ntc and ntu
  709. * @hw: pointer to hardware info
  710. * @cq: control queue information
  711. *
  712. * returns true if there are pending messages in a queue, false if there aren't
  713. */
  714. static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  715. {
  716. u16 ntu;
  717. ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
  718. return cq->rq.next_to_clean != ntu;
  719. }
  720. /**
  721. * ice_clean_adminq_subtask - clean the AdminQ rings
  722. * @pf: board private structure
  723. */
  724. static void ice_clean_adminq_subtask(struct ice_pf *pf)
  725. {
  726. struct ice_hw *hw = &pf->hw;
  727. if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
  728. return;
  729. if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
  730. return;
  731. clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
  732. /* There might be a situation where new messages arrive to a control
  733. * queue between processing the last message and clearing the
  734. * EVENT_PENDING bit. So before exiting, check queue head again (using
  735. * ice_ctrlq_pending) and process new messages if any.
  736. */
  737. if (ice_ctrlq_pending(hw, &hw->adminq))
  738. __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
  739. ice_flush(hw);
  740. }
  741. /**
  742. * ice_service_task_schedule - schedule the service task to wake up
  743. * @pf: board private structure
  744. *
  745. * If not already scheduled, this puts the task into the work queue.
  746. */
  747. static void ice_service_task_schedule(struct ice_pf *pf)
  748. {
  749. if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
  750. !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
  751. !test_bit(__ICE_NEEDS_RESTART, pf->state))
  752. queue_work(ice_wq, &pf->serv_task);
  753. }
  754. /**
  755. * ice_service_task_complete - finish up the service task
  756. * @pf: board private structure
  757. */
  758. static void ice_service_task_complete(struct ice_pf *pf)
  759. {
  760. WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
  761. /* force memory (pf->state) to sync before next service task */
  762. smp_mb__before_atomic();
  763. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  764. }
  765. /**
  766. * ice_service_task_stop - stop service task and cancel works
  767. * @pf: board private structure
  768. */
  769. static void ice_service_task_stop(struct ice_pf *pf)
  770. {
  771. set_bit(__ICE_SERVICE_DIS, pf->state);
  772. if (pf->serv_tmr.function)
  773. del_timer_sync(&pf->serv_tmr);
  774. if (pf->serv_task.func)
  775. cancel_work_sync(&pf->serv_task);
  776. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  777. }
  778. /**
  779. * ice_service_timer - timer callback to schedule service task
  780. * @t: pointer to timer_list
  781. */
  782. static void ice_service_timer(struct timer_list *t)
  783. {
  784. struct ice_pf *pf = from_timer(pf, t, serv_tmr);
  785. mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
  786. ice_service_task_schedule(pf);
  787. }
  788. /**
  789. * ice_handle_mdd_event - handle malicious driver detect event
  790. * @pf: pointer to the PF structure
  791. *
  792. * Called from service task. OICR interrupt handler indicates MDD event
  793. */
  794. static void ice_handle_mdd_event(struct ice_pf *pf)
  795. {
  796. struct ice_hw *hw = &pf->hw;
  797. bool mdd_detected = false;
  798. u32 reg;
  799. if (!test_bit(__ICE_MDD_EVENT_PENDING, pf->state))
  800. return;
  801. /* find what triggered the MDD event */
  802. reg = rd32(hw, GL_MDET_TX_PQM);
  803. if (reg & GL_MDET_TX_PQM_VALID_M) {
  804. u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
  805. GL_MDET_TX_PQM_PF_NUM_S;
  806. u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
  807. GL_MDET_TX_PQM_VF_NUM_S;
  808. u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
  809. GL_MDET_TX_PQM_MAL_TYPE_S;
  810. u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
  811. GL_MDET_TX_PQM_QNUM_S);
  812. if (netif_msg_tx_err(pf))
  813. dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
  814. event, queue, pf_num, vf_num);
  815. wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
  816. mdd_detected = true;
  817. }
  818. reg = rd32(hw, GL_MDET_TX_TCLAN);
  819. if (reg & GL_MDET_TX_TCLAN_VALID_M) {
  820. u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
  821. GL_MDET_TX_TCLAN_PF_NUM_S;
  822. u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
  823. GL_MDET_TX_TCLAN_VF_NUM_S;
  824. u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
  825. GL_MDET_TX_TCLAN_MAL_TYPE_S;
  826. u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
  827. GL_MDET_TX_TCLAN_QNUM_S);
  828. if (netif_msg_rx_err(pf))
  829. dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
  830. event, queue, pf_num, vf_num);
  831. wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
  832. mdd_detected = true;
  833. }
  834. reg = rd32(hw, GL_MDET_RX);
  835. if (reg & GL_MDET_RX_VALID_M) {
  836. u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
  837. GL_MDET_RX_PF_NUM_S;
  838. u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
  839. GL_MDET_RX_VF_NUM_S;
  840. u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
  841. GL_MDET_RX_MAL_TYPE_S;
  842. u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
  843. GL_MDET_RX_QNUM_S);
  844. if (netif_msg_rx_err(pf))
  845. dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
  846. event, queue, pf_num, vf_num);
  847. wr32(hw, GL_MDET_RX, 0xffffffff);
  848. mdd_detected = true;
  849. }
  850. if (mdd_detected) {
  851. bool pf_mdd_detected = false;
  852. reg = rd32(hw, PF_MDET_TX_PQM);
  853. if (reg & PF_MDET_TX_PQM_VALID_M) {
  854. wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
  855. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  856. pf_mdd_detected = true;
  857. }
  858. reg = rd32(hw, PF_MDET_TX_TCLAN);
  859. if (reg & PF_MDET_TX_TCLAN_VALID_M) {
  860. wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
  861. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  862. pf_mdd_detected = true;
  863. }
  864. reg = rd32(hw, PF_MDET_RX);
  865. if (reg & PF_MDET_RX_VALID_M) {
  866. wr32(hw, PF_MDET_RX, 0xFFFF);
  867. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  868. pf_mdd_detected = true;
  869. }
  870. /* Queue belongs to the PF initiate a reset */
  871. if (pf_mdd_detected) {
  872. set_bit(__ICE_NEEDS_RESTART, pf->state);
  873. ice_service_task_schedule(pf);
  874. }
  875. }
  876. /* re-enable MDD interrupt cause */
  877. clear_bit(__ICE_MDD_EVENT_PENDING, pf->state);
  878. reg = rd32(hw, PFINT_OICR_ENA);
  879. reg |= PFINT_OICR_MAL_DETECT_M;
  880. wr32(hw, PFINT_OICR_ENA, reg);
  881. ice_flush(hw);
  882. }
  883. /**
  884. * ice_service_task - manage and run subtasks
  885. * @work: pointer to work_struct contained by the PF struct
  886. */
  887. static void ice_service_task(struct work_struct *work)
  888. {
  889. struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
  890. unsigned long start_time = jiffies;
  891. /* subtasks */
  892. /* process reset requests first */
  893. ice_reset_subtask(pf);
  894. /* bail if a reset/recovery cycle is pending or rebuild failed */
  895. if (ice_is_reset_in_progress(pf->state) ||
  896. test_bit(__ICE_SUSPENDED, pf->state) ||
  897. test_bit(__ICE_NEEDS_RESTART, pf->state)) {
  898. ice_service_task_complete(pf);
  899. return;
  900. }
  901. ice_check_for_hang_subtask(pf);
  902. ice_sync_fltr_subtask(pf);
  903. ice_handle_mdd_event(pf);
  904. ice_watchdog_subtask(pf);
  905. ice_clean_adminq_subtask(pf);
  906. /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
  907. ice_service_task_complete(pf);
  908. /* If the tasks have taken longer than one service timer period
  909. * or there is more work to be done, reset the service timer to
  910. * schedule the service task now.
  911. */
  912. if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
  913. test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
  914. test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
  915. mod_timer(&pf->serv_tmr, jiffies);
  916. }
  917. /**
  918. * ice_set_ctrlq_len - helper function to set controlq length
  919. * @hw: pointer to the hw instance
  920. */
  921. static void ice_set_ctrlq_len(struct ice_hw *hw)
  922. {
  923. hw->adminq.num_rq_entries = ICE_AQ_LEN;
  924. hw->adminq.num_sq_entries = ICE_AQ_LEN;
  925. hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
  926. hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
  927. }
  928. /**
  929. * ice_irq_affinity_notify - Callback for affinity changes
  930. * @notify: context as to what irq was changed
  931. * @mask: the new affinity mask
  932. *
  933. * This is a callback function used by the irq_set_affinity_notifier function
  934. * so that we may register to receive changes to the irq affinity masks.
  935. */
  936. static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
  937. const cpumask_t *mask)
  938. {
  939. struct ice_q_vector *q_vector =
  940. container_of(notify, struct ice_q_vector, affinity_notify);
  941. cpumask_copy(&q_vector->affinity_mask, mask);
  942. }
  943. /**
  944. * ice_irq_affinity_release - Callback for affinity notifier release
  945. * @ref: internal core kernel usage
  946. *
  947. * This is a callback function used by the irq_set_affinity_notifier function
  948. * to inform the current notification subscriber that they will no longer
  949. * receive notifications.
  950. */
  951. static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
  952. /**
  953. * ice_vsi_ena_irq - Enable IRQ for the given VSI
  954. * @vsi: the VSI being configured
  955. */
  956. static int ice_vsi_ena_irq(struct ice_vsi *vsi)
  957. {
  958. struct ice_pf *pf = vsi->back;
  959. struct ice_hw *hw = &pf->hw;
  960. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  961. int i;
  962. for (i = 0; i < vsi->num_q_vectors; i++)
  963. ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
  964. }
  965. ice_flush(hw);
  966. return 0;
  967. }
  968. /**
  969. * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
  970. * @vsi: the VSI being configured
  971. * @basename: name for the vector
  972. */
  973. static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
  974. {
  975. int q_vectors = vsi->num_q_vectors;
  976. struct ice_pf *pf = vsi->back;
  977. int base = vsi->sw_base_vector;
  978. int rx_int_idx = 0;
  979. int tx_int_idx = 0;
  980. int vector, err;
  981. int irq_num;
  982. for (vector = 0; vector < q_vectors; vector++) {
  983. struct ice_q_vector *q_vector = vsi->q_vectors[vector];
  984. irq_num = pf->msix_entries[base + vector].vector;
  985. if (q_vector->tx.ring && q_vector->rx.ring) {
  986. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  987. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  988. tx_int_idx++;
  989. } else if (q_vector->rx.ring) {
  990. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  991. "%s-%s-%d", basename, "rx", rx_int_idx++);
  992. } else if (q_vector->tx.ring) {
  993. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  994. "%s-%s-%d", basename, "tx", tx_int_idx++);
  995. } else {
  996. /* skip this unused q_vector */
  997. continue;
  998. }
  999. err = devm_request_irq(&pf->pdev->dev,
  1000. pf->msix_entries[base + vector].vector,
  1001. vsi->irq_handler, 0, q_vector->name,
  1002. q_vector);
  1003. if (err) {
  1004. netdev_err(vsi->netdev,
  1005. "MSIX request_irq failed, error: %d\n", err);
  1006. goto free_q_irqs;
  1007. }
  1008. /* register for affinity change notifications */
  1009. q_vector->affinity_notify.notify = ice_irq_affinity_notify;
  1010. q_vector->affinity_notify.release = ice_irq_affinity_release;
  1011. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  1012. /* assign the mask for this irq */
  1013. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  1014. }
  1015. vsi->irqs_ready = true;
  1016. return 0;
  1017. free_q_irqs:
  1018. while (vector) {
  1019. vector--;
  1020. irq_num = pf->msix_entries[base + vector].vector,
  1021. irq_set_affinity_notifier(irq_num, NULL);
  1022. irq_set_affinity_hint(irq_num, NULL);
  1023. devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
  1024. }
  1025. return err;
  1026. }
  1027. /**
  1028. * ice_ena_misc_vector - enable the non-queue interrupts
  1029. * @pf: board private structure
  1030. */
  1031. static void ice_ena_misc_vector(struct ice_pf *pf)
  1032. {
  1033. struct ice_hw *hw = &pf->hw;
  1034. u32 val;
  1035. /* clear things first */
  1036. wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
  1037. rd32(hw, PFINT_OICR); /* read to clear */
  1038. val = (PFINT_OICR_ECC_ERR_M |
  1039. PFINT_OICR_MAL_DETECT_M |
  1040. PFINT_OICR_GRST_M |
  1041. PFINT_OICR_PCI_EXCEPTION_M |
  1042. PFINT_OICR_HMC_ERR_M |
  1043. PFINT_OICR_PE_CRITERR_M);
  1044. wr32(hw, PFINT_OICR_ENA, val);
  1045. /* SW_ITR_IDX = 0, but don't change INTENA */
  1046. wr32(hw, GLINT_DYN_CTL(pf->hw_oicr_idx),
  1047. GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
  1048. }
  1049. /**
  1050. * ice_misc_intr - misc interrupt handler
  1051. * @irq: interrupt number
  1052. * @data: pointer to a q_vector
  1053. */
  1054. static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
  1055. {
  1056. struct ice_pf *pf = (struct ice_pf *)data;
  1057. struct ice_hw *hw = &pf->hw;
  1058. irqreturn_t ret = IRQ_NONE;
  1059. u32 oicr, ena_mask;
  1060. set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
  1061. oicr = rd32(hw, PFINT_OICR);
  1062. ena_mask = rd32(hw, PFINT_OICR_ENA);
  1063. if (oicr & PFINT_OICR_MAL_DETECT_M) {
  1064. ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
  1065. set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
  1066. }
  1067. if (oicr & PFINT_OICR_GRST_M) {
  1068. u32 reset;
  1069. /* we have a reset warning */
  1070. ena_mask &= ~PFINT_OICR_GRST_M;
  1071. reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
  1072. GLGEN_RSTAT_RESET_TYPE_S;
  1073. if (reset == ICE_RESET_CORER)
  1074. pf->corer_count++;
  1075. else if (reset == ICE_RESET_GLOBR)
  1076. pf->globr_count++;
  1077. else
  1078. pf->empr_count++;
  1079. /* If a reset cycle isn't already in progress, we set a bit in
  1080. * pf->state so that the service task can start a reset/rebuild.
  1081. * We also make note of which reset happened so that peer
  1082. * devices/drivers can be informed.
  1083. */
  1084. if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
  1085. if (reset == ICE_RESET_CORER)
  1086. set_bit(__ICE_CORER_RECV, pf->state);
  1087. else if (reset == ICE_RESET_GLOBR)
  1088. set_bit(__ICE_GLOBR_RECV, pf->state);
  1089. else
  1090. set_bit(__ICE_EMPR_RECV, pf->state);
  1091. /* There are couple of different bits at play here.
  1092. * hw->reset_ongoing indicates whether the hardware is
  1093. * in reset. This is set to true when a reset interrupt
  1094. * is received and set back to false after the driver
  1095. * has determined that the hardware is out of reset.
  1096. *
  1097. * __ICE_RESET_OICR_RECV in pf->state indicates
  1098. * that a post reset rebuild is required before the
  1099. * driver is operational again. This is set above.
  1100. *
  1101. * As this is the start of the reset/rebuild cycle, set
  1102. * both to indicate that.
  1103. */
  1104. hw->reset_ongoing = true;
  1105. }
  1106. }
  1107. if (oicr & PFINT_OICR_HMC_ERR_M) {
  1108. ena_mask &= ~PFINT_OICR_HMC_ERR_M;
  1109. dev_dbg(&pf->pdev->dev,
  1110. "HMC Error interrupt - info 0x%x, data 0x%x\n",
  1111. rd32(hw, PFHMC_ERRORINFO),
  1112. rd32(hw, PFHMC_ERRORDATA));
  1113. }
  1114. /* Report and mask off any remaining unexpected interrupts */
  1115. oicr &= ena_mask;
  1116. if (oicr) {
  1117. dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
  1118. oicr);
  1119. /* If a critical error is pending there is no choice but to
  1120. * reset the device.
  1121. */
  1122. if (oicr & (PFINT_OICR_PE_CRITERR_M |
  1123. PFINT_OICR_PCI_EXCEPTION_M |
  1124. PFINT_OICR_ECC_ERR_M)) {
  1125. set_bit(__ICE_PFR_REQ, pf->state);
  1126. ice_service_task_schedule(pf);
  1127. }
  1128. ena_mask &= ~oicr;
  1129. }
  1130. ret = IRQ_HANDLED;
  1131. /* re-enable interrupt causes that are not handled during this pass */
  1132. wr32(hw, PFINT_OICR_ENA, ena_mask);
  1133. if (!test_bit(__ICE_DOWN, pf->state)) {
  1134. ice_service_task_schedule(pf);
  1135. ice_irq_dynamic_ena(hw, NULL, NULL);
  1136. }
  1137. return ret;
  1138. }
  1139. /**
  1140. * ice_free_irq_msix_misc - Unroll misc vector setup
  1141. * @pf: board private structure
  1142. */
  1143. static void ice_free_irq_msix_misc(struct ice_pf *pf)
  1144. {
  1145. /* disable OICR interrupt */
  1146. wr32(&pf->hw, PFINT_OICR_ENA, 0);
  1147. ice_flush(&pf->hw);
  1148. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
  1149. synchronize_irq(pf->msix_entries[pf->sw_oicr_idx].vector);
  1150. devm_free_irq(&pf->pdev->dev,
  1151. pf->msix_entries[pf->sw_oicr_idx].vector, pf);
  1152. }
  1153. pf->num_avail_sw_msix += 1;
  1154. ice_free_res(pf->sw_irq_tracker, pf->sw_oicr_idx, ICE_RES_MISC_VEC_ID);
  1155. pf->num_avail_hw_msix += 1;
  1156. ice_free_res(pf->hw_irq_tracker, pf->hw_oicr_idx, ICE_RES_MISC_VEC_ID);
  1157. }
  1158. /**
  1159. * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
  1160. * @pf: board private structure
  1161. *
  1162. * This sets up the handler for MSIX 0, which is used to manage the
  1163. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  1164. * when in MSI or Legacy interrupt mode.
  1165. */
  1166. static int ice_req_irq_msix_misc(struct ice_pf *pf)
  1167. {
  1168. struct ice_hw *hw = &pf->hw;
  1169. int oicr_idx, err = 0;
  1170. u8 itr_gran;
  1171. u32 val;
  1172. if (!pf->int_name[0])
  1173. snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
  1174. dev_driver_string(&pf->pdev->dev),
  1175. dev_name(&pf->pdev->dev));
  1176. /* Do not request IRQ but do enable OICR interrupt since settings are
  1177. * lost during reset. Note that this function is called only during
  1178. * rebuild path and not while reset is in progress.
  1179. */
  1180. if (ice_is_reset_in_progress(pf->state))
  1181. goto skip_req_irq;
  1182. /* reserve one vector in sw_irq_tracker for misc interrupts */
  1183. oicr_idx = ice_get_res(pf, pf->sw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  1184. if (oicr_idx < 0)
  1185. return oicr_idx;
  1186. pf->num_avail_sw_msix -= 1;
  1187. pf->sw_oicr_idx = oicr_idx;
  1188. /* reserve one vector in hw_irq_tracker for misc interrupts */
  1189. oicr_idx = ice_get_res(pf, pf->hw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  1190. if (oicr_idx < 0) {
  1191. ice_free_res(pf->sw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  1192. pf->num_avail_sw_msix += 1;
  1193. return oicr_idx;
  1194. }
  1195. pf->num_avail_hw_msix -= 1;
  1196. pf->hw_oicr_idx = oicr_idx;
  1197. err = devm_request_irq(&pf->pdev->dev,
  1198. pf->msix_entries[pf->sw_oicr_idx].vector,
  1199. ice_misc_intr, 0, pf->int_name, pf);
  1200. if (err) {
  1201. dev_err(&pf->pdev->dev,
  1202. "devm_request_irq for %s failed: %d\n",
  1203. pf->int_name, err);
  1204. ice_free_res(pf->sw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  1205. pf->num_avail_sw_msix += 1;
  1206. ice_free_res(pf->hw_irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  1207. pf->num_avail_hw_msix += 1;
  1208. return err;
  1209. }
  1210. skip_req_irq:
  1211. ice_ena_misc_vector(pf);
  1212. val = ((pf->hw_oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
  1213. PFINT_OICR_CTL_CAUSE_ENA_M);
  1214. wr32(hw, PFINT_OICR_CTL, val);
  1215. /* This enables Admin queue Interrupt causes */
  1216. val = ((pf->hw_oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
  1217. PFINT_FW_CTL_CAUSE_ENA_M);
  1218. wr32(hw, PFINT_FW_CTL, val);
  1219. itr_gran = hw->itr_gran_200;
  1220. wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->hw_oicr_idx),
  1221. ITR_TO_REG(ICE_ITR_8K, itr_gran));
  1222. ice_flush(hw);
  1223. ice_irq_dynamic_ena(hw, NULL, NULL);
  1224. return 0;
  1225. }
  1226. /**
  1227. * ice_napi_del - Remove NAPI handler for the VSI
  1228. * @vsi: VSI for which NAPI handler is to be removed
  1229. */
  1230. static void ice_napi_del(struct ice_vsi *vsi)
  1231. {
  1232. int v_idx;
  1233. if (!vsi->netdev)
  1234. return;
  1235. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  1236. netif_napi_del(&vsi->q_vectors[v_idx]->napi);
  1237. }
  1238. /**
  1239. * ice_napi_add - register NAPI handler for the VSI
  1240. * @vsi: VSI for which NAPI handler is to be registered
  1241. *
  1242. * This function is only called in the driver's load path. Registering the NAPI
  1243. * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
  1244. * reset/rebuild, etc.)
  1245. */
  1246. static void ice_napi_add(struct ice_vsi *vsi)
  1247. {
  1248. int v_idx;
  1249. if (!vsi->netdev)
  1250. return;
  1251. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  1252. netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
  1253. ice_napi_poll, NAPI_POLL_WEIGHT);
  1254. }
  1255. /**
  1256. * ice_cfg_netdev - Allocate, configure and register a netdev
  1257. * @vsi: the VSI associated with the new netdev
  1258. *
  1259. * Returns 0 on success, negative value on failure
  1260. */
  1261. static int ice_cfg_netdev(struct ice_vsi *vsi)
  1262. {
  1263. netdev_features_t csumo_features;
  1264. netdev_features_t vlano_features;
  1265. netdev_features_t dflt_features;
  1266. netdev_features_t tso_features;
  1267. struct ice_netdev_priv *np;
  1268. struct net_device *netdev;
  1269. u8 mac_addr[ETH_ALEN];
  1270. int err;
  1271. netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
  1272. vsi->alloc_txq, vsi->alloc_rxq);
  1273. if (!netdev)
  1274. return -ENOMEM;
  1275. vsi->netdev = netdev;
  1276. np = netdev_priv(netdev);
  1277. np->vsi = vsi;
  1278. dflt_features = NETIF_F_SG |
  1279. NETIF_F_HIGHDMA |
  1280. NETIF_F_RXHASH;
  1281. csumo_features = NETIF_F_RXCSUM |
  1282. NETIF_F_IP_CSUM |
  1283. NETIF_F_IPV6_CSUM;
  1284. vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
  1285. NETIF_F_HW_VLAN_CTAG_TX |
  1286. NETIF_F_HW_VLAN_CTAG_RX;
  1287. tso_features = NETIF_F_TSO;
  1288. /* set features that user can change */
  1289. netdev->hw_features = dflt_features | csumo_features |
  1290. vlano_features | tso_features;
  1291. /* enable features */
  1292. netdev->features |= netdev->hw_features;
  1293. /* encap and VLAN devices inherit default, csumo and tso features */
  1294. netdev->hw_enc_features |= dflt_features | csumo_features |
  1295. tso_features;
  1296. netdev->vlan_features |= dflt_features | csumo_features |
  1297. tso_features;
  1298. if (vsi->type == ICE_VSI_PF) {
  1299. SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
  1300. ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
  1301. ether_addr_copy(netdev->dev_addr, mac_addr);
  1302. ether_addr_copy(netdev->perm_addr, mac_addr);
  1303. }
  1304. netdev->priv_flags |= IFF_UNICAST_FLT;
  1305. /* assign netdev_ops */
  1306. netdev->netdev_ops = &ice_netdev_ops;
  1307. /* setup watchdog timeout value to be 5 second */
  1308. netdev->watchdog_timeo = 5 * HZ;
  1309. ice_set_ethtool_ops(netdev);
  1310. netdev->min_mtu = ETH_MIN_MTU;
  1311. netdev->max_mtu = ICE_MAX_MTU;
  1312. err = register_netdev(vsi->netdev);
  1313. if (err)
  1314. return err;
  1315. netif_carrier_off(vsi->netdev);
  1316. /* make sure transmit queues start off as stopped */
  1317. netif_tx_stop_all_queues(vsi->netdev);
  1318. return 0;
  1319. }
  1320. /**
  1321. * ice_fill_rss_lut - Fill the RSS lookup table with default values
  1322. * @lut: Lookup table
  1323. * @rss_table_size: Lookup table size
  1324. * @rss_size: Range of queue number for hashing
  1325. */
  1326. void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
  1327. {
  1328. u16 i;
  1329. for (i = 0; i < rss_table_size; i++)
  1330. lut[i] = i % rss_size;
  1331. }
  1332. /**
  1333. * ice_pf_vsi_setup - Set up a PF VSI
  1334. * @pf: board private structure
  1335. * @pi: pointer to the port_info instance
  1336. *
  1337. * Returns pointer to the successfully allocated VSI sw struct on success,
  1338. * otherwise returns NULL on failure.
  1339. */
  1340. static struct ice_vsi *
  1341. ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
  1342. {
  1343. return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
  1344. }
  1345. /**
  1346. * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
  1347. * @netdev: network interface to be adjusted
  1348. * @proto: unused protocol
  1349. * @vid: vlan id to be added
  1350. *
  1351. * net_device_ops implementation for adding vlan ids
  1352. */
  1353. static int ice_vlan_rx_add_vid(struct net_device *netdev,
  1354. __always_unused __be16 proto, u16 vid)
  1355. {
  1356. struct ice_netdev_priv *np = netdev_priv(netdev);
  1357. struct ice_vsi *vsi = np->vsi;
  1358. int ret;
  1359. if (vid >= VLAN_N_VID) {
  1360. netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
  1361. vid, VLAN_N_VID);
  1362. return -EINVAL;
  1363. }
  1364. if (vsi->info.pvid)
  1365. return -EINVAL;
  1366. /* Enable VLAN pruning when VLAN 0 is added */
  1367. if (unlikely(!vid)) {
  1368. ret = ice_cfg_vlan_pruning(vsi, true);
  1369. if (ret)
  1370. return ret;
  1371. }
  1372. /* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
  1373. * needed to continue allowing all untagged packets since VLAN prune
  1374. * list is applied to all packets by the switch
  1375. */
  1376. ret = ice_vsi_add_vlan(vsi, vid);
  1377. if (!ret)
  1378. set_bit(vid, vsi->active_vlans);
  1379. return ret;
  1380. }
  1381. /**
  1382. * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  1383. * @netdev: network interface to be adjusted
  1384. * @proto: unused protocol
  1385. * @vid: vlan id to be removed
  1386. *
  1387. * net_device_ops implementation for removing vlan ids
  1388. */
  1389. static int ice_vlan_rx_kill_vid(struct net_device *netdev,
  1390. __always_unused __be16 proto, u16 vid)
  1391. {
  1392. struct ice_netdev_priv *np = netdev_priv(netdev);
  1393. struct ice_vsi *vsi = np->vsi;
  1394. int status;
  1395. if (vsi->info.pvid)
  1396. return -EINVAL;
  1397. /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
  1398. * information
  1399. */
  1400. status = ice_vsi_kill_vlan(vsi, vid);
  1401. if (status)
  1402. return status;
  1403. clear_bit(vid, vsi->active_vlans);
  1404. /* Disable VLAN pruning when VLAN 0 is removed */
  1405. if (unlikely(!vid))
  1406. status = ice_cfg_vlan_pruning(vsi, false);
  1407. return status;
  1408. }
  1409. /**
  1410. * ice_setup_pf_sw - Setup the HW switch on startup or after reset
  1411. * @pf: board private structure
  1412. *
  1413. * Returns 0 on success, negative value on failure
  1414. */
  1415. static int ice_setup_pf_sw(struct ice_pf *pf)
  1416. {
  1417. LIST_HEAD(tmp_add_list);
  1418. u8 broadcast[ETH_ALEN];
  1419. struct ice_vsi *vsi;
  1420. int status = 0;
  1421. if (ice_is_reset_in_progress(pf->state))
  1422. return -EBUSY;
  1423. vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
  1424. if (!vsi) {
  1425. status = -ENOMEM;
  1426. goto unroll_vsi_setup;
  1427. }
  1428. status = ice_cfg_netdev(vsi);
  1429. if (status) {
  1430. status = -ENODEV;
  1431. goto unroll_vsi_setup;
  1432. }
  1433. /* registering the NAPI handler requires both the queues and
  1434. * netdev to be created, which are done in ice_pf_vsi_setup()
  1435. * and ice_cfg_netdev() respectively
  1436. */
  1437. ice_napi_add(vsi);
  1438. /* To add a MAC filter, first add the MAC to a list and then
  1439. * pass the list to ice_add_mac.
  1440. */
  1441. /* Add a unicast MAC filter so the VSI can get its packets */
  1442. status = ice_add_mac_to_list(vsi, &tmp_add_list,
  1443. vsi->port_info->mac.perm_addr);
  1444. if (status)
  1445. goto unroll_napi_add;
  1446. /* VSI needs to receive broadcast traffic, so add the broadcast
  1447. * MAC address to the list as well.
  1448. */
  1449. eth_broadcast_addr(broadcast);
  1450. status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
  1451. if (status)
  1452. goto free_mac_list;
  1453. /* program MAC filters for entries in tmp_add_list */
  1454. status = ice_add_mac(&pf->hw, &tmp_add_list);
  1455. if (status) {
  1456. dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
  1457. status = -ENOMEM;
  1458. goto free_mac_list;
  1459. }
  1460. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  1461. return status;
  1462. free_mac_list:
  1463. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  1464. unroll_napi_add:
  1465. if (vsi) {
  1466. ice_napi_del(vsi);
  1467. if (vsi->netdev) {
  1468. if (vsi->netdev->reg_state == NETREG_REGISTERED)
  1469. unregister_netdev(vsi->netdev);
  1470. free_netdev(vsi->netdev);
  1471. vsi->netdev = NULL;
  1472. }
  1473. }
  1474. unroll_vsi_setup:
  1475. if (vsi) {
  1476. ice_vsi_free_q_vectors(vsi);
  1477. ice_vsi_delete(vsi);
  1478. ice_vsi_put_qs(vsi);
  1479. pf->q_left_tx += vsi->alloc_txq;
  1480. pf->q_left_rx += vsi->alloc_rxq;
  1481. ice_vsi_clear(vsi);
  1482. }
  1483. return status;
  1484. }
  1485. /**
  1486. * ice_determine_q_usage - Calculate queue distribution
  1487. * @pf: board private structure
  1488. *
  1489. * Return -ENOMEM if we don't get enough queues for all ports
  1490. */
  1491. static void ice_determine_q_usage(struct ice_pf *pf)
  1492. {
  1493. u16 q_left_tx, q_left_rx;
  1494. q_left_tx = pf->hw.func_caps.common_cap.num_txq;
  1495. q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
  1496. pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
  1497. /* only 1 rx queue unless RSS is enabled */
  1498. if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1499. pf->num_lan_rx = 1;
  1500. else
  1501. pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
  1502. pf->q_left_tx = q_left_tx - pf->num_lan_tx;
  1503. pf->q_left_rx = q_left_rx - pf->num_lan_rx;
  1504. }
  1505. /**
  1506. * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
  1507. * @pf: board private structure to initialize
  1508. */
  1509. static void ice_deinit_pf(struct ice_pf *pf)
  1510. {
  1511. ice_service_task_stop(pf);
  1512. mutex_destroy(&pf->sw_mutex);
  1513. mutex_destroy(&pf->avail_q_mutex);
  1514. }
  1515. /**
  1516. * ice_init_pf - Initialize general software structures (struct ice_pf)
  1517. * @pf: board private structure to initialize
  1518. */
  1519. static void ice_init_pf(struct ice_pf *pf)
  1520. {
  1521. bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
  1522. set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  1523. mutex_init(&pf->sw_mutex);
  1524. mutex_init(&pf->avail_q_mutex);
  1525. /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
  1526. mutex_lock(&pf->avail_q_mutex);
  1527. bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
  1528. bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
  1529. mutex_unlock(&pf->avail_q_mutex);
  1530. if (pf->hw.func_caps.common_cap.rss_table_size)
  1531. set_bit(ICE_FLAG_RSS_ENA, pf->flags);
  1532. /* setup service timer and periodic service task */
  1533. timer_setup(&pf->serv_tmr, ice_service_timer, 0);
  1534. pf->serv_tmr_period = HZ;
  1535. INIT_WORK(&pf->serv_task, ice_service_task);
  1536. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  1537. }
  1538. /**
  1539. * ice_ena_msix_range - Request a range of MSIX vectors from the OS
  1540. * @pf: board private structure
  1541. *
  1542. * compute the number of MSIX vectors required (v_budget) and request from
  1543. * the OS. Return the number of vectors reserved or negative on failure
  1544. */
  1545. static int ice_ena_msix_range(struct ice_pf *pf)
  1546. {
  1547. int v_left, v_actual, v_budget = 0;
  1548. int needed, err, i;
  1549. v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
  1550. /* reserve one vector for miscellaneous handler */
  1551. needed = 1;
  1552. v_budget += needed;
  1553. v_left -= needed;
  1554. /* reserve vectors for LAN traffic */
  1555. pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
  1556. v_budget += pf->num_lan_msix;
  1557. v_left -= pf->num_lan_msix;
  1558. pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
  1559. sizeof(struct msix_entry), GFP_KERNEL);
  1560. if (!pf->msix_entries) {
  1561. err = -ENOMEM;
  1562. goto exit_err;
  1563. }
  1564. for (i = 0; i < v_budget; i++)
  1565. pf->msix_entries[i].entry = i;
  1566. /* actually reserve the vectors */
  1567. v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  1568. ICE_MIN_MSIX, v_budget);
  1569. if (v_actual < 0) {
  1570. dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
  1571. err = v_actual;
  1572. goto msix_err;
  1573. }
  1574. if (v_actual < v_budget) {
  1575. dev_warn(&pf->pdev->dev,
  1576. "not enough vectors. requested = %d, obtained = %d\n",
  1577. v_budget, v_actual);
  1578. if (v_actual >= (pf->num_lan_msix + 1)) {
  1579. pf->num_avail_sw_msix = v_actual -
  1580. (pf->num_lan_msix + 1);
  1581. } else if (v_actual >= 2) {
  1582. pf->num_lan_msix = 1;
  1583. pf->num_avail_sw_msix = v_actual - 2;
  1584. } else {
  1585. pci_disable_msix(pf->pdev);
  1586. err = -ERANGE;
  1587. goto msix_err;
  1588. }
  1589. }
  1590. return v_actual;
  1591. msix_err:
  1592. devm_kfree(&pf->pdev->dev, pf->msix_entries);
  1593. goto exit_err;
  1594. exit_err:
  1595. pf->num_lan_msix = 0;
  1596. clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  1597. return err;
  1598. }
  1599. /**
  1600. * ice_dis_msix - Disable MSI-X interrupt setup in OS
  1601. * @pf: board private structure
  1602. */
  1603. static void ice_dis_msix(struct ice_pf *pf)
  1604. {
  1605. pci_disable_msix(pf->pdev);
  1606. devm_kfree(&pf->pdev->dev, pf->msix_entries);
  1607. pf->msix_entries = NULL;
  1608. clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  1609. }
  1610. /**
  1611. * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
  1612. * @pf: board private structure
  1613. */
  1614. static void ice_clear_interrupt_scheme(struct ice_pf *pf)
  1615. {
  1616. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  1617. ice_dis_msix(pf);
  1618. if (pf->sw_irq_tracker) {
  1619. devm_kfree(&pf->pdev->dev, pf->sw_irq_tracker);
  1620. pf->sw_irq_tracker = NULL;
  1621. }
  1622. if (pf->hw_irq_tracker) {
  1623. devm_kfree(&pf->pdev->dev, pf->hw_irq_tracker);
  1624. pf->hw_irq_tracker = NULL;
  1625. }
  1626. }
  1627. /**
  1628. * ice_init_interrupt_scheme - Determine proper interrupt scheme
  1629. * @pf: board private structure to initialize
  1630. */
  1631. static int ice_init_interrupt_scheme(struct ice_pf *pf)
  1632. {
  1633. int vectors = 0, hw_vectors = 0;
  1634. ssize_t size;
  1635. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  1636. vectors = ice_ena_msix_range(pf);
  1637. else
  1638. return -ENODEV;
  1639. if (vectors < 0)
  1640. return vectors;
  1641. /* set up vector assignment tracking */
  1642. size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
  1643. pf->sw_irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
  1644. if (!pf->sw_irq_tracker) {
  1645. ice_dis_msix(pf);
  1646. return -ENOMEM;
  1647. }
  1648. /* populate SW interrupts pool with number of OS granted IRQs. */
  1649. pf->num_avail_sw_msix = vectors;
  1650. pf->sw_irq_tracker->num_entries = vectors;
  1651. /* set up HW vector assignment tracking */
  1652. hw_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
  1653. size = sizeof(struct ice_res_tracker) + (sizeof(u16) * hw_vectors);
  1654. pf->hw_irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
  1655. if (!pf->hw_irq_tracker) {
  1656. ice_clear_interrupt_scheme(pf);
  1657. return -ENOMEM;
  1658. }
  1659. /* populate HW interrupts pool with number of HW supported irqs. */
  1660. pf->num_avail_hw_msix = hw_vectors;
  1661. pf->hw_irq_tracker->num_entries = hw_vectors;
  1662. return 0;
  1663. }
  1664. /**
  1665. * ice_probe - Device initialization routine
  1666. * @pdev: PCI device information struct
  1667. * @ent: entry in ice_pci_tbl
  1668. *
  1669. * Returns 0 on success, negative on failure
  1670. */
  1671. static int ice_probe(struct pci_dev *pdev,
  1672. const struct pci_device_id __always_unused *ent)
  1673. {
  1674. struct ice_pf *pf;
  1675. struct ice_hw *hw;
  1676. int err;
  1677. /* this driver uses devres, see Documentation/driver-model/devres.txt */
  1678. err = pcim_enable_device(pdev);
  1679. if (err)
  1680. return err;
  1681. err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
  1682. if (err) {
  1683. dev_err(&pdev->dev, "BAR0 I/O map error %d\n", err);
  1684. return err;
  1685. }
  1686. pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
  1687. if (!pf)
  1688. return -ENOMEM;
  1689. /* set up for high or low dma */
  1690. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  1691. if (err)
  1692. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1693. if (err) {
  1694. dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
  1695. return err;
  1696. }
  1697. pci_enable_pcie_error_reporting(pdev);
  1698. pci_set_master(pdev);
  1699. pf->pdev = pdev;
  1700. pci_set_drvdata(pdev, pf);
  1701. set_bit(__ICE_DOWN, pf->state);
  1702. /* Disable service task until DOWN bit is cleared */
  1703. set_bit(__ICE_SERVICE_DIS, pf->state);
  1704. hw = &pf->hw;
  1705. hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
  1706. hw->back = pf;
  1707. hw->vendor_id = pdev->vendor;
  1708. hw->device_id = pdev->device;
  1709. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  1710. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  1711. hw->subsystem_device_id = pdev->subsystem_device;
  1712. hw->bus.device = PCI_SLOT(pdev->devfn);
  1713. hw->bus.func = PCI_FUNC(pdev->devfn);
  1714. ice_set_ctrlq_len(hw);
  1715. pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
  1716. #ifndef CONFIG_DYNAMIC_DEBUG
  1717. if (debug < -1)
  1718. hw->debug_mask = debug;
  1719. #endif
  1720. err = ice_init_hw(hw);
  1721. if (err) {
  1722. dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
  1723. err = -EIO;
  1724. goto err_exit_unroll;
  1725. }
  1726. dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
  1727. hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
  1728. hw->api_maj_ver, hw->api_min_ver);
  1729. ice_init_pf(pf);
  1730. ice_determine_q_usage(pf);
  1731. pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
  1732. hw->func_caps.guaranteed_num_vsi);
  1733. if (!pf->num_alloc_vsi) {
  1734. err = -EIO;
  1735. goto err_init_pf_unroll;
  1736. }
  1737. pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
  1738. sizeof(struct ice_vsi *), GFP_KERNEL);
  1739. if (!pf->vsi) {
  1740. err = -ENOMEM;
  1741. goto err_init_pf_unroll;
  1742. }
  1743. err = ice_init_interrupt_scheme(pf);
  1744. if (err) {
  1745. dev_err(&pdev->dev,
  1746. "ice_init_interrupt_scheme failed: %d\n", err);
  1747. err = -EIO;
  1748. goto err_init_interrupt_unroll;
  1749. }
  1750. /* Driver is mostly up */
  1751. clear_bit(__ICE_DOWN, pf->state);
  1752. /* In case of MSIX we are going to setup the misc vector right here
  1753. * to handle admin queue events etc. In case of legacy and MSI
  1754. * the misc functionality and queue processing is combined in
  1755. * the same vector and that gets setup at open.
  1756. */
  1757. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1758. err = ice_req_irq_msix_misc(pf);
  1759. if (err) {
  1760. dev_err(&pdev->dev,
  1761. "setup of misc vector failed: %d\n", err);
  1762. goto err_init_interrupt_unroll;
  1763. }
  1764. }
  1765. /* create switch struct for the switch element created by FW on boot */
  1766. pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
  1767. GFP_KERNEL);
  1768. if (!pf->first_sw) {
  1769. err = -ENOMEM;
  1770. goto err_msix_misc_unroll;
  1771. }
  1772. if (hw->evb_veb)
  1773. pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
  1774. else
  1775. pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
  1776. pf->first_sw->pf = pf;
  1777. /* record the sw_id available for later use */
  1778. pf->first_sw->sw_id = hw->port_info->sw_id;
  1779. err = ice_setup_pf_sw(pf);
  1780. if (err) {
  1781. dev_err(&pdev->dev,
  1782. "probe failed due to setup pf switch:%d\n", err);
  1783. goto err_alloc_sw_unroll;
  1784. }
  1785. clear_bit(__ICE_SERVICE_DIS, pf->state);
  1786. /* since everything is good, start the service timer */
  1787. mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
  1788. err = ice_init_link_events(pf->hw.port_info);
  1789. if (err) {
  1790. dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
  1791. goto err_alloc_sw_unroll;
  1792. }
  1793. return 0;
  1794. err_alloc_sw_unroll:
  1795. set_bit(__ICE_SERVICE_DIS, pf->state);
  1796. set_bit(__ICE_DOWN, pf->state);
  1797. devm_kfree(&pf->pdev->dev, pf->first_sw);
  1798. err_msix_misc_unroll:
  1799. ice_free_irq_msix_misc(pf);
  1800. err_init_interrupt_unroll:
  1801. ice_clear_interrupt_scheme(pf);
  1802. devm_kfree(&pdev->dev, pf->vsi);
  1803. err_init_pf_unroll:
  1804. ice_deinit_pf(pf);
  1805. ice_deinit_hw(hw);
  1806. err_exit_unroll:
  1807. pci_disable_pcie_error_reporting(pdev);
  1808. return err;
  1809. }
  1810. /**
  1811. * ice_remove - Device removal routine
  1812. * @pdev: PCI device information struct
  1813. */
  1814. static void ice_remove(struct pci_dev *pdev)
  1815. {
  1816. struct ice_pf *pf = pci_get_drvdata(pdev);
  1817. if (!pf)
  1818. return;
  1819. set_bit(__ICE_DOWN, pf->state);
  1820. ice_service_task_stop(pf);
  1821. ice_vsi_release_all(pf);
  1822. ice_free_irq_msix_misc(pf);
  1823. ice_clear_interrupt_scheme(pf);
  1824. ice_deinit_pf(pf);
  1825. ice_deinit_hw(&pf->hw);
  1826. pci_disable_pcie_error_reporting(pdev);
  1827. }
  1828. /* ice_pci_tbl - PCI Device ID Table
  1829. *
  1830. * Wildcard entries (PCI_ANY_ID) should come last
  1831. * Last entry must be all 0s
  1832. *
  1833. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  1834. * Class, Class Mask, private data (not used) }
  1835. */
  1836. static const struct pci_device_id ice_pci_tbl[] = {
  1837. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
  1838. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
  1839. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
  1840. /* required last entry */
  1841. { 0, }
  1842. };
  1843. MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
  1844. static struct pci_driver ice_driver = {
  1845. .name = KBUILD_MODNAME,
  1846. .id_table = ice_pci_tbl,
  1847. .probe = ice_probe,
  1848. .remove = ice_remove,
  1849. };
  1850. /**
  1851. * ice_module_init - Driver registration routine
  1852. *
  1853. * ice_module_init is the first routine called when the driver is
  1854. * loaded. All it does is register with the PCI subsystem.
  1855. */
  1856. static int __init ice_module_init(void)
  1857. {
  1858. int status;
  1859. pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
  1860. pr_info("%s\n", ice_copyright);
  1861. ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
  1862. if (!ice_wq) {
  1863. pr_err("Failed to create workqueue\n");
  1864. return -ENOMEM;
  1865. }
  1866. status = pci_register_driver(&ice_driver);
  1867. if (status) {
  1868. pr_err("failed to register pci driver, err %d\n", status);
  1869. destroy_workqueue(ice_wq);
  1870. }
  1871. return status;
  1872. }
  1873. module_init(ice_module_init);
  1874. /**
  1875. * ice_module_exit - Driver exit cleanup routine
  1876. *
  1877. * ice_module_exit is called just before the driver is removed
  1878. * from memory.
  1879. */
  1880. static void __exit ice_module_exit(void)
  1881. {
  1882. pci_unregister_driver(&ice_driver);
  1883. destroy_workqueue(ice_wq);
  1884. pr_info("module unloaded\n");
  1885. }
  1886. module_exit(ice_module_exit);
  1887. /**
  1888. * ice_set_mac_address - NDO callback to set mac address
  1889. * @netdev: network interface device structure
  1890. * @pi: pointer to an address structure
  1891. *
  1892. * Returns 0 on success, negative on failure
  1893. */
  1894. static int ice_set_mac_address(struct net_device *netdev, void *pi)
  1895. {
  1896. struct ice_netdev_priv *np = netdev_priv(netdev);
  1897. struct ice_vsi *vsi = np->vsi;
  1898. struct ice_pf *pf = vsi->back;
  1899. struct ice_hw *hw = &pf->hw;
  1900. struct sockaddr *addr = pi;
  1901. enum ice_status status;
  1902. LIST_HEAD(a_mac_list);
  1903. LIST_HEAD(r_mac_list);
  1904. u8 flags = 0;
  1905. int err;
  1906. u8 *mac;
  1907. mac = (u8 *)addr->sa_data;
  1908. if (!is_valid_ether_addr(mac))
  1909. return -EADDRNOTAVAIL;
  1910. if (ether_addr_equal(netdev->dev_addr, mac)) {
  1911. netdev_warn(netdev, "already using mac %pM\n", mac);
  1912. return 0;
  1913. }
  1914. if (test_bit(__ICE_DOWN, pf->state) ||
  1915. ice_is_reset_in_progress(pf->state)) {
  1916. netdev_err(netdev, "can't set mac %pM. device not ready\n",
  1917. mac);
  1918. return -EBUSY;
  1919. }
  1920. /* When we change the mac address we also have to change the mac address
  1921. * based filter rules that were created previously for the old mac
  1922. * address. So first, we remove the old filter rule using ice_remove_mac
  1923. * and then create a new filter rule using ice_add_mac. Note that for
  1924. * both these operations, we first need to form a "list" of mac
  1925. * addresses (even though in this case, we have only 1 mac address to be
  1926. * added/removed) and this done using ice_add_mac_to_list. Depending on
  1927. * the ensuing operation this "list" of mac addresses is either to be
  1928. * added or removed from the filter.
  1929. */
  1930. err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
  1931. if (err) {
  1932. err = -EADDRNOTAVAIL;
  1933. goto free_lists;
  1934. }
  1935. status = ice_remove_mac(hw, &r_mac_list);
  1936. if (status) {
  1937. err = -EADDRNOTAVAIL;
  1938. goto free_lists;
  1939. }
  1940. err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
  1941. if (err) {
  1942. err = -EADDRNOTAVAIL;
  1943. goto free_lists;
  1944. }
  1945. status = ice_add_mac(hw, &a_mac_list);
  1946. if (status) {
  1947. err = -EADDRNOTAVAIL;
  1948. goto free_lists;
  1949. }
  1950. free_lists:
  1951. /* free list entries */
  1952. ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
  1953. ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
  1954. if (err) {
  1955. netdev_err(netdev, "can't set mac %pM. filter update failed\n",
  1956. mac);
  1957. return err;
  1958. }
  1959. /* change the netdev's mac address */
  1960. memcpy(netdev->dev_addr, mac, netdev->addr_len);
  1961. netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
  1962. netdev->dev_addr);
  1963. /* write new mac address to the firmware */
  1964. flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
  1965. status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
  1966. if (status) {
  1967. netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
  1968. mac);
  1969. }
  1970. return 0;
  1971. }
  1972. /**
  1973. * ice_set_rx_mode - NDO callback to set the netdev filters
  1974. * @netdev: network interface device structure
  1975. */
  1976. static void ice_set_rx_mode(struct net_device *netdev)
  1977. {
  1978. struct ice_netdev_priv *np = netdev_priv(netdev);
  1979. struct ice_vsi *vsi = np->vsi;
  1980. if (!vsi)
  1981. return;
  1982. /* Set the flags to synchronize filters
  1983. * ndo_set_rx_mode may be triggered even without a change in netdev
  1984. * flags
  1985. */
  1986. set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  1987. set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  1988. set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
  1989. /* schedule our worker thread which will take care of
  1990. * applying the new filter changes
  1991. */
  1992. ice_service_task_schedule(vsi->back);
  1993. }
  1994. /**
  1995. * ice_fdb_add - add an entry to the hardware database
  1996. * @ndm: the input from the stack
  1997. * @tb: pointer to array of nladdr (unused)
  1998. * @dev: the net device pointer
  1999. * @addr: the MAC address entry being added
  2000. * @vid: VLAN id
  2001. * @flags: instructions from stack about fdb operation
  2002. */
  2003. static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
  2004. struct net_device *dev, const unsigned char *addr,
  2005. u16 vid, u16 flags)
  2006. {
  2007. int err;
  2008. if (vid) {
  2009. netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
  2010. return -EINVAL;
  2011. }
  2012. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  2013. netdev_err(dev, "FDB only supports static addresses\n");
  2014. return -EINVAL;
  2015. }
  2016. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  2017. err = dev_uc_add_excl(dev, addr);
  2018. else if (is_multicast_ether_addr(addr))
  2019. err = dev_mc_add_excl(dev, addr);
  2020. else
  2021. err = -EINVAL;
  2022. /* Only return duplicate errors if NLM_F_EXCL is set */
  2023. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  2024. err = 0;
  2025. return err;
  2026. }
  2027. /**
  2028. * ice_fdb_del - delete an entry from the hardware database
  2029. * @ndm: the input from the stack
  2030. * @tb: pointer to array of nladdr (unused)
  2031. * @dev: the net device pointer
  2032. * @addr: the MAC address entry being added
  2033. * @vid: VLAN id
  2034. */
  2035. static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
  2036. struct net_device *dev, const unsigned char *addr,
  2037. __always_unused u16 vid)
  2038. {
  2039. int err;
  2040. if (ndm->ndm_state & NUD_PERMANENT) {
  2041. netdev_err(dev, "FDB only supports static addresses\n");
  2042. return -EINVAL;
  2043. }
  2044. if (is_unicast_ether_addr(addr))
  2045. err = dev_uc_del(dev, addr);
  2046. else if (is_multicast_ether_addr(addr))
  2047. err = dev_mc_del(dev, addr);
  2048. else
  2049. err = -EINVAL;
  2050. return err;
  2051. }
  2052. /**
  2053. * ice_set_features - set the netdev feature flags
  2054. * @netdev: ptr to the netdev being adjusted
  2055. * @features: the feature set that the stack is suggesting
  2056. */
  2057. static int ice_set_features(struct net_device *netdev,
  2058. netdev_features_t features)
  2059. {
  2060. struct ice_netdev_priv *np = netdev_priv(netdev);
  2061. struct ice_vsi *vsi = np->vsi;
  2062. int ret = 0;
  2063. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  2064. ret = ice_vsi_manage_rss_lut(vsi, true);
  2065. else if (!(features & NETIF_F_RXHASH) &&
  2066. netdev->features & NETIF_F_RXHASH)
  2067. ret = ice_vsi_manage_rss_lut(vsi, false);
  2068. if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
  2069. !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  2070. ret = ice_vsi_manage_vlan_stripping(vsi, true);
  2071. else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
  2072. (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  2073. ret = ice_vsi_manage_vlan_stripping(vsi, false);
  2074. else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
  2075. !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
  2076. ret = ice_vsi_manage_vlan_insertion(vsi);
  2077. else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
  2078. (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
  2079. ret = ice_vsi_manage_vlan_insertion(vsi);
  2080. return ret;
  2081. }
  2082. /**
  2083. * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
  2084. * @vsi: VSI to setup vlan properties for
  2085. */
  2086. static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
  2087. {
  2088. int ret = 0;
  2089. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2090. ret = ice_vsi_manage_vlan_stripping(vsi, true);
  2091. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
  2092. ret = ice_vsi_manage_vlan_insertion(vsi);
  2093. return ret;
  2094. }
  2095. /**
  2096. * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
  2097. * @vsi: the VSI being brought back up
  2098. */
  2099. static int ice_restore_vlan(struct ice_vsi *vsi)
  2100. {
  2101. int err;
  2102. u16 vid;
  2103. if (!vsi->netdev)
  2104. return -EINVAL;
  2105. err = ice_vsi_vlan_setup(vsi);
  2106. if (err)
  2107. return err;
  2108. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
  2109. err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
  2110. if (err)
  2111. break;
  2112. }
  2113. return err;
  2114. }
  2115. /**
  2116. * ice_vsi_cfg - Setup the VSI
  2117. * @vsi: the VSI being configured
  2118. *
  2119. * Return 0 on success and negative value on error
  2120. */
  2121. static int ice_vsi_cfg(struct ice_vsi *vsi)
  2122. {
  2123. int err;
  2124. if (vsi->netdev) {
  2125. ice_set_rx_mode(vsi->netdev);
  2126. err = ice_restore_vlan(vsi);
  2127. if (err)
  2128. return err;
  2129. }
  2130. err = ice_vsi_cfg_txqs(vsi);
  2131. if (!err)
  2132. err = ice_vsi_cfg_rxqs(vsi);
  2133. return err;
  2134. }
  2135. /**
  2136. * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  2137. * @vsi: the VSI being configured
  2138. */
  2139. static void ice_napi_enable_all(struct ice_vsi *vsi)
  2140. {
  2141. int q_idx;
  2142. if (!vsi->netdev)
  2143. return;
  2144. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  2145. napi_enable(&vsi->q_vectors[q_idx]->napi);
  2146. }
  2147. /**
  2148. * ice_up_complete - Finish the last steps of bringing up a connection
  2149. * @vsi: The VSI being configured
  2150. *
  2151. * Return 0 on success and negative value on error
  2152. */
  2153. static int ice_up_complete(struct ice_vsi *vsi)
  2154. {
  2155. struct ice_pf *pf = vsi->back;
  2156. int err;
  2157. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  2158. ice_vsi_cfg_msix(vsi);
  2159. else
  2160. return -ENOTSUPP;
  2161. /* Enable only Rx rings, Tx rings were enabled by the FW when the
  2162. * Tx queue group list was configured and the context bits were
  2163. * programmed using ice_vsi_cfg_txqs
  2164. */
  2165. err = ice_vsi_start_rx_rings(vsi);
  2166. if (err)
  2167. return err;
  2168. clear_bit(__ICE_DOWN, vsi->state);
  2169. ice_napi_enable_all(vsi);
  2170. ice_vsi_ena_irq(vsi);
  2171. if (vsi->port_info &&
  2172. (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
  2173. vsi->netdev) {
  2174. ice_print_link_msg(vsi, true);
  2175. netif_tx_start_all_queues(vsi->netdev);
  2176. netif_carrier_on(vsi->netdev);
  2177. }
  2178. ice_service_task_schedule(pf);
  2179. return err;
  2180. }
  2181. /**
  2182. * ice_up - Bring the connection back up after being down
  2183. * @vsi: VSI being configured
  2184. */
  2185. int ice_up(struct ice_vsi *vsi)
  2186. {
  2187. int err;
  2188. err = ice_vsi_cfg(vsi);
  2189. if (!err)
  2190. err = ice_up_complete(vsi);
  2191. return err;
  2192. }
  2193. /**
  2194. * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
  2195. * @ring: Tx or Rx ring to read stats from
  2196. * @pkts: packets stats counter
  2197. * @bytes: bytes stats counter
  2198. *
  2199. * This function fetches stats from the ring considering the atomic operations
  2200. * that needs to be performed to read u64 values in 32 bit machine.
  2201. */
  2202. static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
  2203. u64 *bytes)
  2204. {
  2205. unsigned int start;
  2206. *pkts = 0;
  2207. *bytes = 0;
  2208. if (!ring)
  2209. return;
  2210. do {
  2211. start = u64_stats_fetch_begin_irq(&ring->syncp);
  2212. *pkts = ring->stats.pkts;
  2213. *bytes = ring->stats.bytes;
  2214. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  2215. }
  2216. /**
  2217. * ice_update_vsi_ring_stats - Update VSI stats counters
  2218. * @vsi: the VSI to be updated
  2219. */
  2220. static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
  2221. {
  2222. struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
  2223. struct ice_ring *ring;
  2224. u64 pkts, bytes;
  2225. int i;
  2226. /* reset netdev stats */
  2227. vsi_stats->tx_packets = 0;
  2228. vsi_stats->tx_bytes = 0;
  2229. vsi_stats->rx_packets = 0;
  2230. vsi_stats->rx_bytes = 0;
  2231. /* reset non-netdev (extended) stats */
  2232. vsi->tx_restart = 0;
  2233. vsi->tx_busy = 0;
  2234. vsi->tx_linearize = 0;
  2235. vsi->rx_buf_failed = 0;
  2236. vsi->rx_page_failed = 0;
  2237. rcu_read_lock();
  2238. /* update Tx rings counters */
  2239. ice_for_each_txq(vsi, i) {
  2240. ring = READ_ONCE(vsi->tx_rings[i]);
  2241. ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
  2242. vsi_stats->tx_packets += pkts;
  2243. vsi_stats->tx_bytes += bytes;
  2244. vsi->tx_restart += ring->tx_stats.restart_q;
  2245. vsi->tx_busy += ring->tx_stats.tx_busy;
  2246. vsi->tx_linearize += ring->tx_stats.tx_linearize;
  2247. }
  2248. /* update Rx rings counters */
  2249. ice_for_each_rxq(vsi, i) {
  2250. ring = READ_ONCE(vsi->rx_rings[i]);
  2251. ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
  2252. vsi_stats->rx_packets += pkts;
  2253. vsi_stats->rx_bytes += bytes;
  2254. vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
  2255. vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
  2256. }
  2257. rcu_read_unlock();
  2258. }
  2259. /**
  2260. * ice_update_vsi_stats - Update VSI stats counters
  2261. * @vsi: the VSI to be updated
  2262. */
  2263. static void ice_update_vsi_stats(struct ice_vsi *vsi)
  2264. {
  2265. struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
  2266. struct ice_eth_stats *cur_es = &vsi->eth_stats;
  2267. struct ice_pf *pf = vsi->back;
  2268. if (test_bit(__ICE_DOWN, vsi->state) ||
  2269. test_bit(__ICE_CFG_BUSY, pf->state))
  2270. return;
  2271. /* get stats as recorded by Tx/Rx rings */
  2272. ice_update_vsi_ring_stats(vsi);
  2273. /* get VSI stats as recorded by the hardware */
  2274. ice_update_eth_stats(vsi);
  2275. cur_ns->tx_errors = cur_es->tx_errors;
  2276. cur_ns->rx_dropped = cur_es->rx_discards;
  2277. cur_ns->tx_dropped = cur_es->tx_discards;
  2278. cur_ns->multicast = cur_es->rx_multicast;
  2279. /* update some more netdev stats if this is main VSI */
  2280. if (vsi->type == ICE_VSI_PF) {
  2281. cur_ns->rx_crc_errors = pf->stats.crc_errors;
  2282. cur_ns->rx_errors = pf->stats.crc_errors +
  2283. pf->stats.illegal_bytes;
  2284. cur_ns->rx_length_errors = pf->stats.rx_len_errors;
  2285. }
  2286. }
  2287. /**
  2288. * ice_update_pf_stats - Update PF port stats counters
  2289. * @pf: PF whose stats needs to be updated
  2290. */
  2291. static void ice_update_pf_stats(struct ice_pf *pf)
  2292. {
  2293. struct ice_hw_port_stats *prev_ps, *cur_ps;
  2294. struct ice_hw *hw = &pf->hw;
  2295. u8 pf_id;
  2296. prev_ps = &pf->stats_prev;
  2297. cur_ps = &pf->stats;
  2298. pf_id = hw->pf_id;
  2299. ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
  2300. pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
  2301. &cur_ps->eth.rx_bytes);
  2302. ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
  2303. pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
  2304. &cur_ps->eth.rx_unicast);
  2305. ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
  2306. pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
  2307. &cur_ps->eth.rx_multicast);
  2308. ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
  2309. pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
  2310. &cur_ps->eth.rx_broadcast);
  2311. ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
  2312. pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
  2313. &cur_ps->eth.tx_bytes);
  2314. ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
  2315. pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
  2316. &cur_ps->eth.tx_unicast);
  2317. ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
  2318. pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
  2319. &cur_ps->eth.tx_multicast);
  2320. ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
  2321. pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
  2322. &cur_ps->eth.tx_broadcast);
  2323. ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
  2324. &prev_ps->tx_dropped_link_down,
  2325. &cur_ps->tx_dropped_link_down);
  2326. ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
  2327. pf->stat_prev_loaded, &prev_ps->rx_size_64,
  2328. &cur_ps->rx_size_64);
  2329. ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
  2330. pf->stat_prev_loaded, &prev_ps->rx_size_127,
  2331. &cur_ps->rx_size_127);
  2332. ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
  2333. pf->stat_prev_loaded, &prev_ps->rx_size_255,
  2334. &cur_ps->rx_size_255);
  2335. ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
  2336. pf->stat_prev_loaded, &prev_ps->rx_size_511,
  2337. &cur_ps->rx_size_511);
  2338. ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
  2339. GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
  2340. &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
  2341. ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
  2342. GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
  2343. &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
  2344. ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
  2345. GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
  2346. &prev_ps->rx_size_big, &cur_ps->rx_size_big);
  2347. ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
  2348. pf->stat_prev_loaded, &prev_ps->tx_size_64,
  2349. &cur_ps->tx_size_64);
  2350. ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
  2351. pf->stat_prev_loaded, &prev_ps->tx_size_127,
  2352. &cur_ps->tx_size_127);
  2353. ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
  2354. pf->stat_prev_loaded, &prev_ps->tx_size_255,
  2355. &cur_ps->tx_size_255);
  2356. ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
  2357. pf->stat_prev_loaded, &prev_ps->tx_size_511,
  2358. &cur_ps->tx_size_511);
  2359. ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
  2360. GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
  2361. &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
  2362. ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
  2363. GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
  2364. &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
  2365. ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
  2366. GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
  2367. &prev_ps->tx_size_big, &cur_ps->tx_size_big);
  2368. ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
  2369. &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
  2370. ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
  2371. &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
  2372. ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
  2373. &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
  2374. ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
  2375. &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
  2376. ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
  2377. &prev_ps->crc_errors, &cur_ps->crc_errors);
  2378. ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
  2379. &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
  2380. ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
  2381. &prev_ps->mac_local_faults,
  2382. &cur_ps->mac_local_faults);
  2383. ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
  2384. &prev_ps->mac_remote_faults,
  2385. &cur_ps->mac_remote_faults);
  2386. ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
  2387. &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
  2388. ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
  2389. &prev_ps->rx_undersize, &cur_ps->rx_undersize);
  2390. ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
  2391. &prev_ps->rx_fragments, &cur_ps->rx_fragments);
  2392. ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
  2393. &prev_ps->rx_oversize, &cur_ps->rx_oversize);
  2394. ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
  2395. &prev_ps->rx_jabber, &cur_ps->rx_jabber);
  2396. pf->stat_prev_loaded = true;
  2397. }
  2398. /**
  2399. * ice_get_stats64 - get statistics for network device structure
  2400. * @netdev: network interface device structure
  2401. * @stats: main device statistics structure
  2402. */
  2403. static
  2404. void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
  2405. {
  2406. struct ice_netdev_priv *np = netdev_priv(netdev);
  2407. struct rtnl_link_stats64 *vsi_stats;
  2408. struct ice_vsi *vsi = np->vsi;
  2409. vsi_stats = &vsi->net_stats;
  2410. if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
  2411. return;
  2412. /* netdev packet/byte stats come from ring counter. These are obtained
  2413. * by summing up ring counters (done by ice_update_vsi_ring_stats).
  2414. */
  2415. ice_update_vsi_ring_stats(vsi);
  2416. stats->tx_packets = vsi_stats->tx_packets;
  2417. stats->tx_bytes = vsi_stats->tx_bytes;
  2418. stats->rx_packets = vsi_stats->rx_packets;
  2419. stats->rx_bytes = vsi_stats->rx_bytes;
  2420. /* The rest of the stats can be read from the hardware but instead we
  2421. * just return values that the watchdog task has already obtained from
  2422. * the hardware.
  2423. */
  2424. stats->multicast = vsi_stats->multicast;
  2425. stats->tx_errors = vsi_stats->tx_errors;
  2426. stats->tx_dropped = vsi_stats->tx_dropped;
  2427. stats->rx_errors = vsi_stats->rx_errors;
  2428. stats->rx_dropped = vsi_stats->rx_dropped;
  2429. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  2430. stats->rx_length_errors = vsi_stats->rx_length_errors;
  2431. }
  2432. /**
  2433. * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  2434. * @vsi: VSI having NAPI disabled
  2435. */
  2436. static void ice_napi_disable_all(struct ice_vsi *vsi)
  2437. {
  2438. int q_idx;
  2439. if (!vsi->netdev)
  2440. return;
  2441. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  2442. napi_disable(&vsi->q_vectors[q_idx]->napi);
  2443. }
  2444. /**
  2445. * ice_down - Shutdown the connection
  2446. * @vsi: The VSI being stopped
  2447. */
  2448. int ice_down(struct ice_vsi *vsi)
  2449. {
  2450. int i, tx_err, rx_err;
  2451. /* Caller of this function is expected to set the
  2452. * vsi->state __ICE_DOWN bit
  2453. */
  2454. if (vsi->netdev) {
  2455. netif_carrier_off(vsi->netdev);
  2456. netif_tx_disable(vsi->netdev);
  2457. }
  2458. ice_vsi_dis_irq(vsi);
  2459. tx_err = ice_vsi_stop_tx_rings(vsi);
  2460. if (tx_err)
  2461. netdev_err(vsi->netdev,
  2462. "Failed stop Tx rings, VSI %d error %d\n",
  2463. vsi->vsi_num, tx_err);
  2464. rx_err = ice_vsi_stop_rx_rings(vsi);
  2465. if (rx_err)
  2466. netdev_err(vsi->netdev,
  2467. "Failed stop Rx rings, VSI %d error %d\n",
  2468. vsi->vsi_num, rx_err);
  2469. ice_napi_disable_all(vsi);
  2470. ice_for_each_txq(vsi, i)
  2471. ice_clean_tx_ring(vsi->tx_rings[i]);
  2472. ice_for_each_rxq(vsi, i)
  2473. ice_clean_rx_ring(vsi->rx_rings[i]);
  2474. if (tx_err || rx_err) {
  2475. netdev_err(vsi->netdev,
  2476. "Failed to close VSI 0x%04X on switch 0x%04X\n",
  2477. vsi->vsi_num, vsi->vsw->sw_id);
  2478. return -EIO;
  2479. }
  2480. return 0;
  2481. }
  2482. /**
  2483. * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
  2484. * @vsi: VSI having resources allocated
  2485. *
  2486. * Return 0 on success, negative on failure
  2487. */
  2488. static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
  2489. {
  2490. int i, err = 0;
  2491. if (!vsi->num_txq) {
  2492. dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
  2493. vsi->vsi_num);
  2494. return -EINVAL;
  2495. }
  2496. ice_for_each_txq(vsi, i) {
  2497. vsi->tx_rings[i]->netdev = vsi->netdev;
  2498. err = ice_setup_tx_ring(vsi->tx_rings[i]);
  2499. if (err)
  2500. break;
  2501. }
  2502. return err;
  2503. }
  2504. /**
  2505. * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
  2506. * @vsi: VSI having resources allocated
  2507. *
  2508. * Return 0 on success, negative on failure
  2509. */
  2510. static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
  2511. {
  2512. int i, err = 0;
  2513. if (!vsi->num_rxq) {
  2514. dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
  2515. vsi->vsi_num);
  2516. return -EINVAL;
  2517. }
  2518. ice_for_each_rxq(vsi, i) {
  2519. vsi->rx_rings[i]->netdev = vsi->netdev;
  2520. err = ice_setup_rx_ring(vsi->rx_rings[i]);
  2521. if (err)
  2522. break;
  2523. }
  2524. return err;
  2525. }
  2526. /**
  2527. * ice_vsi_req_irq - Request IRQ from the OS
  2528. * @vsi: The VSI IRQ is being requested for
  2529. * @basename: name for the vector
  2530. *
  2531. * Return 0 on success and a negative value on error
  2532. */
  2533. static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
  2534. {
  2535. struct ice_pf *pf = vsi->back;
  2536. int err = -EINVAL;
  2537. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  2538. err = ice_vsi_req_irq_msix(vsi, basename);
  2539. return err;
  2540. }
  2541. /**
  2542. * ice_vsi_open - Called when a network interface is made active
  2543. * @vsi: the VSI to open
  2544. *
  2545. * Initialization of the VSI
  2546. *
  2547. * Returns 0 on success, negative value on error
  2548. */
  2549. static int ice_vsi_open(struct ice_vsi *vsi)
  2550. {
  2551. char int_name[ICE_INT_NAME_STR_LEN];
  2552. struct ice_pf *pf = vsi->back;
  2553. int err;
  2554. /* allocate descriptors */
  2555. err = ice_vsi_setup_tx_rings(vsi);
  2556. if (err)
  2557. goto err_setup_tx;
  2558. err = ice_vsi_setup_rx_rings(vsi);
  2559. if (err)
  2560. goto err_setup_rx;
  2561. err = ice_vsi_cfg(vsi);
  2562. if (err)
  2563. goto err_setup_rx;
  2564. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  2565. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  2566. err = ice_vsi_req_irq(vsi, int_name);
  2567. if (err)
  2568. goto err_setup_rx;
  2569. /* Notify the stack of the actual queue counts. */
  2570. err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
  2571. if (err)
  2572. goto err_set_qs;
  2573. err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
  2574. if (err)
  2575. goto err_set_qs;
  2576. err = ice_up_complete(vsi);
  2577. if (err)
  2578. goto err_up_complete;
  2579. return 0;
  2580. err_up_complete:
  2581. ice_down(vsi);
  2582. err_set_qs:
  2583. ice_vsi_free_irq(vsi);
  2584. err_setup_rx:
  2585. ice_vsi_free_rx_rings(vsi);
  2586. err_setup_tx:
  2587. ice_vsi_free_tx_rings(vsi);
  2588. return err;
  2589. }
  2590. /**
  2591. * ice_vsi_release_all - Delete all VSIs
  2592. * @pf: PF from which all VSIs are being removed
  2593. */
  2594. static void ice_vsi_release_all(struct ice_pf *pf)
  2595. {
  2596. int err, i;
  2597. if (!pf->vsi)
  2598. return;
  2599. for (i = 0; i < pf->num_alloc_vsi; i++) {
  2600. if (!pf->vsi[i])
  2601. continue;
  2602. err = ice_vsi_release(pf->vsi[i]);
  2603. if (err)
  2604. dev_dbg(&pf->pdev->dev,
  2605. "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
  2606. i, err, pf->vsi[i]->vsi_num);
  2607. }
  2608. }
  2609. /**
  2610. * ice_dis_vsi - pause a VSI
  2611. * @vsi: the VSI being paused
  2612. */
  2613. static void ice_dis_vsi(struct ice_vsi *vsi)
  2614. {
  2615. if (test_bit(__ICE_DOWN, vsi->state))
  2616. return;
  2617. set_bit(__ICE_NEEDS_RESTART, vsi->state);
  2618. if (vsi->netdev && netif_running(vsi->netdev) &&
  2619. vsi->type == ICE_VSI_PF) {
  2620. rtnl_lock();
  2621. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  2622. rtnl_unlock();
  2623. } else {
  2624. ice_vsi_close(vsi);
  2625. }
  2626. }
  2627. /**
  2628. * ice_ena_vsi - resume a VSI
  2629. * @vsi: the VSI being resume
  2630. */
  2631. static int ice_ena_vsi(struct ice_vsi *vsi)
  2632. {
  2633. int err = 0;
  2634. if (test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
  2635. if (vsi->netdev && netif_running(vsi->netdev)) {
  2636. rtnl_lock();
  2637. err = vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  2638. rtnl_unlock();
  2639. }
  2640. return err;
  2641. }
  2642. /**
  2643. * ice_pf_dis_all_vsi - Pause all VSIs on a PF
  2644. * @pf: the PF
  2645. */
  2646. static void ice_pf_dis_all_vsi(struct ice_pf *pf)
  2647. {
  2648. int v;
  2649. ice_for_each_vsi(pf, v)
  2650. if (pf->vsi[v])
  2651. ice_dis_vsi(pf->vsi[v]);
  2652. }
  2653. /**
  2654. * ice_pf_ena_all_vsi - Resume all VSIs on a PF
  2655. * @pf: the PF
  2656. */
  2657. static int ice_pf_ena_all_vsi(struct ice_pf *pf)
  2658. {
  2659. int v;
  2660. ice_for_each_vsi(pf, v)
  2661. if (pf->vsi[v])
  2662. if (ice_ena_vsi(pf->vsi[v]))
  2663. return -EIO;
  2664. return 0;
  2665. }
  2666. /**
  2667. * ice_vsi_rebuild_all - rebuild all VSIs in pf
  2668. * @pf: the PF
  2669. */
  2670. static int ice_vsi_rebuild_all(struct ice_pf *pf)
  2671. {
  2672. int i;
  2673. /* loop through pf->vsi array and reinit the VSI if found */
  2674. for (i = 0; i < pf->num_alloc_vsi; i++) {
  2675. int err;
  2676. if (!pf->vsi[i])
  2677. continue;
  2678. err = ice_vsi_rebuild(pf->vsi[i]);
  2679. if (err) {
  2680. dev_err(&pf->pdev->dev,
  2681. "VSI at index %d rebuild failed\n",
  2682. pf->vsi[i]->idx);
  2683. return err;
  2684. }
  2685. dev_info(&pf->pdev->dev,
  2686. "VSI at index %d rebuilt. vsi_num = 0x%x\n",
  2687. pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
  2688. }
  2689. return 0;
  2690. }
  2691. /**
  2692. * ice_vsi_replay_all - replay all VSIs configuration in the PF
  2693. * @pf: the PF
  2694. */
  2695. static int ice_vsi_replay_all(struct ice_pf *pf)
  2696. {
  2697. struct ice_hw *hw = &pf->hw;
  2698. enum ice_status ret;
  2699. int i;
  2700. /* loop through pf->vsi array and replay the VSI if found */
  2701. for (i = 0; i < pf->num_alloc_vsi; i++) {
  2702. if (!pf->vsi[i])
  2703. continue;
  2704. ret = ice_replay_vsi(hw, pf->vsi[i]->idx);
  2705. if (ret) {
  2706. dev_err(&pf->pdev->dev,
  2707. "VSI at index %d replay failed %d\n",
  2708. pf->vsi[i]->idx, ret);
  2709. return -EIO;
  2710. }
  2711. /* Re-map HW VSI number, using VSI handle that has been
  2712. * previously validated in ice_replay_vsi() call above
  2713. */
  2714. pf->vsi[i]->vsi_num = ice_get_hw_vsi_num(hw, pf->vsi[i]->idx);
  2715. dev_info(&pf->pdev->dev,
  2716. "VSI at index %d filter replayed successfully - vsi_num %i\n",
  2717. pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
  2718. }
  2719. /* Clean up replay filter after successful re-configuration */
  2720. ice_replay_post(hw);
  2721. return 0;
  2722. }
  2723. /**
  2724. * ice_rebuild - rebuild after reset
  2725. * @pf: pf to rebuild
  2726. */
  2727. static void ice_rebuild(struct ice_pf *pf)
  2728. {
  2729. struct device *dev = &pf->pdev->dev;
  2730. struct ice_hw *hw = &pf->hw;
  2731. enum ice_status ret;
  2732. int err;
  2733. if (test_bit(__ICE_DOWN, pf->state))
  2734. goto clear_recovery;
  2735. dev_dbg(dev, "rebuilding pf\n");
  2736. ret = ice_init_all_ctrlq(hw);
  2737. if (ret) {
  2738. dev_err(dev, "control queues init failed %d\n", ret);
  2739. goto err_init_ctrlq;
  2740. }
  2741. ret = ice_clear_pf_cfg(hw);
  2742. if (ret) {
  2743. dev_err(dev, "clear PF configuration failed %d\n", ret);
  2744. goto err_init_ctrlq;
  2745. }
  2746. ice_clear_pxe_mode(hw);
  2747. ret = ice_get_caps(hw);
  2748. if (ret) {
  2749. dev_err(dev, "ice_get_caps failed %d\n", ret);
  2750. goto err_init_ctrlq;
  2751. }
  2752. err = ice_sched_init_port(hw->port_info);
  2753. if (err)
  2754. goto err_sched_init_port;
  2755. /* reset search_hint of irq_trackers to 0 since interrupts are
  2756. * reclaimed and could be allocated from beginning during VSI rebuild
  2757. */
  2758. pf->sw_irq_tracker->search_hint = 0;
  2759. pf->hw_irq_tracker->search_hint = 0;
  2760. err = ice_vsi_rebuild_all(pf);
  2761. if (err) {
  2762. dev_err(dev, "ice_vsi_rebuild_all failed\n");
  2763. goto err_vsi_rebuild;
  2764. }
  2765. err = ice_update_link_info(hw->port_info);
  2766. if (err)
  2767. dev_err(&pf->pdev->dev, "Get link status error %d\n", err);
  2768. /* Replay all VSIs Configuration, including filters after reset */
  2769. if (ice_vsi_replay_all(pf)) {
  2770. dev_err(&pf->pdev->dev,
  2771. "error replaying VSI configurations with switch filter rules\n");
  2772. goto err_vsi_rebuild;
  2773. }
  2774. /* start misc vector */
  2775. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  2776. err = ice_req_irq_msix_misc(pf);
  2777. if (err) {
  2778. dev_err(dev, "misc vector setup failed: %d\n", err);
  2779. goto err_vsi_rebuild;
  2780. }
  2781. }
  2782. /* restart the VSIs that were rebuilt and running before the reset */
  2783. err = ice_pf_ena_all_vsi(pf);
  2784. if (err) {
  2785. dev_err(&pf->pdev->dev, "error enabling VSIs\n");
  2786. /* no need to disable VSIs in tear down path in ice_rebuild()
  2787. * since its already taken care in ice_vsi_open()
  2788. */
  2789. goto err_vsi_rebuild;
  2790. }
  2791. /* if we get here, reset flow is successful */
  2792. clear_bit(__ICE_RESET_FAILED, pf->state);
  2793. return;
  2794. err_vsi_rebuild:
  2795. ice_vsi_release_all(pf);
  2796. err_sched_init_port:
  2797. ice_sched_cleanup_all(hw);
  2798. err_init_ctrlq:
  2799. ice_shutdown_all_ctrlq(hw);
  2800. set_bit(__ICE_RESET_FAILED, pf->state);
  2801. clear_recovery:
  2802. /* set this bit in PF state to control service task scheduling */
  2803. set_bit(__ICE_NEEDS_RESTART, pf->state);
  2804. dev_err(dev, "Rebuild failed, unload and reload driver\n");
  2805. }
  2806. /**
  2807. * ice_change_mtu - NDO callback to change the MTU
  2808. * @netdev: network interface device structure
  2809. * @new_mtu: new value for maximum frame size
  2810. *
  2811. * Returns 0 on success, negative on failure
  2812. */
  2813. static int ice_change_mtu(struct net_device *netdev, int new_mtu)
  2814. {
  2815. struct ice_netdev_priv *np = netdev_priv(netdev);
  2816. struct ice_vsi *vsi = np->vsi;
  2817. struct ice_pf *pf = vsi->back;
  2818. u8 count = 0;
  2819. if (new_mtu == netdev->mtu) {
  2820. netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
  2821. return 0;
  2822. }
  2823. if (new_mtu < netdev->min_mtu) {
  2824. netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
  2825. netdev->min_mtu);
  2826. return -EINVAL;
  2827. } else if (new_mtu > netdev->max_mtu) {
  2828. netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
  2829. netdev->min_mtu);
  2830. return -EINVAL;
  2831. }
  2832. /* if a reset is in progress, wait for some time for it to complete */
  2833. do {
  2834. if (ice_is_reset_in_progress(pf->state)) {
  2835. count++;
  2836. usleep_range(1000, 2000);
  2837. } else {
  2838. break;
  2839. }
  2840. } while (count < 100);
  2841. if (count == 100) {
  2842. netdev_err(netdev, "can't change mtu. Device is busy\n");
  2843. return -EBUSY;
  2844. }
  2845. netdev->mtu = new_mtu;
  2846. /* if VSI is up, bring it down and then back up */
  2847. if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
  2848. int err;
  2849. err = ice_down(vsi);
  2850. if (err) {
  2851. netdev_err(netdev, "change mtu if_up err %d\n", err);
  2852. return err;
  2853. }
  2854. err = ice_up(vsi);
  2855. if (err) {
  2856. netdev_err(netdev, "change mtu if_up err %d\n", err);
  2857. return err;
  2858. }
  2859. }
  2860. netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
  2861. return 0;
  2862. }
  2863. /**
  2864. * ice_set_rss - Set RSS keys and lut
  2865. * @vsi: Pointer to VSI structure
  2866. * @seed: RSS hash seed
  2867. * @lut: Lookup table
  2868. * @lut_size: Lookup table size
  2869. *
  2870. * Returns 0 on success, negative on failure
  2871. */
  2872. int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  2873. {
  2874. struct ice_pf *pf = vsi->back;
  2875. struct ice_hw *hw = &pf->hw;
  2876. enum ice_status status;
  2877. if (seed) {
  2878. struct ice_aqc_get_set_rss_keys *buf =
  2879. (struct ice_aqc_get_set_rss_keys *)seed;
  2880. status = ice_aq_set_rss_key(hw, vsi->idx, buf);
  2881. if (status) {
  2882. dev_err(&pf->pdev->dev,
  2883. "Cannot set RSS key, err %d aq_err %d\n",
  2884. status, hw->adminq.rq_last_status);
  2885. return -EIO;
  2886. }
  2887. }
  2888. if (lut) {
  2889. status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
  2890. lut, lut_size);
  2891. if (status) {
  2892. dev_err(&pf->pdev->dev,
  2893. "Cannot set RSS lut, err %d aq_err %d\n",
  2894. status, hw->adminq.rq_last_status);
  2895. return -EIO;
  2896. }
  2897. }
  2898. return 0;
  2899. }
  2900. /**
  2901. * ice_get_rss - Get RSS keys and lut
  2902. * @vsi: Pointer to VSI structure
  2903. * @seed: Buffer to store the keys
  2904. * @lut: Buffer to store the lookup table entries
  2905. * @lut_size: Size of buffer to store the lookup table entries
  2906. *
  2907. * Returns 0 on success, negative on failure
  2908. */
  2909. int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  2910. {
  2911. struct ice_pf *pf = vsi->back;
  2912. struct ice_hw *hw = &pf->hw;
  2913. enum ice_status status;
  2914. if (seed) {
  2915. struct ice_aqc_get_set_rss_keys *buf =
  2916. (struct ice_aqc_get_set_rss_keys *)seed;
  2917. status = ice_aq_get_rss_key(hw, vsi->idx, buf);
  2918. if (status) {
  2919. dev_err(&pf->pdev->dev,
  2920. "Cannot get RSS key, err %d aq_err %d\n",
  2921. status, hw->adminq.rq_last_status);
  2922. return -EIO;
  2923. }
  2924. }
  2925. if (lut) {
  2926. status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
  2927. lut, lut_size);
  2928. if (status) {
  2929. dev_err(&pf->pdev->dev,
  2930. "Cannot get RSS lut, err %d aq_err %d\n",
  2931. status, hw->adminq.rq_last_status);
  2932. return -EIO;
  2933. }
  2934. }
  2935. return 0;
  2936. }
  2937. /**
  2938. * ice_bridge_getlink - Get the hardware bridge mode
  2939. * @skb: skb buff
  2940. * @pid: process id
  2941. * @seq: RTNL message seq
  2942. * @dev: the netdev being configured
  2943. * @filter_mask: filter mask passed in
  2944. * @nlflags: netlink flags passed in
  2945. *
  2946. * Return the bridge mode (VEB/VEPA)
  2947. */
  2948. static int
  2949. ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  2950. struct net_device *dev, u32 filter_mask, int nlflags)
  2951. {
  2952. struct ice_netdev_priv *np = netdev_priv(dev);
  2953. struct ice_vsi *vsi = np->vsi;
  2954. struct ice_pf *pf = vsi->back;
  2955. u16 bmode;
  2956. bmode = pf->first_sw->bridge_mode;
  2957. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
  2958. filter_mask, NULL);
  2959. }
  2960. /**
  2961. * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
  2962. * @vsi: Pointer to VSI structure
  2963. * @bmode: Hardware bridge mode (VEB/VEPA)
  2964. *
  2965. * Returns 0 on success, negative on failure
  2966. */
  2967. static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
  2968. {
  2969. struct device *dev = &vsi->back->pdev->dev;
  2970. struct ice_aqc_vsi_props *vsi_props;
  2971. struct ice_hw *hw = &vsi->back->hw;
  2972. struct ice_vsi_ctx ctxt = { 0 };
  2973. enum ice_status status;
  2974. vsi_props = &vsi->info;
  2975. ctxt.info = vsi->info;
  2976. if (bmode == BRIDGE_MODE_VEB)
  2977. /* change from VEPA to VEB mode */
  2978. ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  2979. else
  2980. /* change from VEB to VEPA mode */
  2981. ctxt.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  2982. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
  2983. status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
  2984. if (status) {
  2985. dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
  2986. bmode, status, hw->adminq.sq_last_status);
  2987. return -EIO;
  2988. }
  2989. /* Update sw flags for book keeping */
  2990. vsi_props->sw_flags = ctxt.info.sw_flags;
  2991. return 0;
  2992. }
  2993. /**
  2994. * ice_bridge_setlink - Set the hardware bridge mode
  2995. * @dev: the netdev being configured
  2996. * @nlh: RTNL message
  2997. * @flags: bridge setlink flags
  2998. *
  2999. * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
  3000. * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
  3001. * not already set for all VSIs connected to this switch. And also update the
  3002. * unicast switch filter rules for the corresponding switch of the netdev.
  3003. */
  3004. static int
  3005. ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
  3006. u16 __always_unused flags)
  3007. {
  3008. struct ice_netdev_priv *np = netdev_priv(dev);
  3009. struct ice_pf *pf = np->vsi->back;
  3010. struct nlattr *attr, *br_spec;
  3011. struct ice_hw *hw = &pf->hw;
  3012. enum ice_status status;
  3013. struct ice_sw *pf_sw;
  3014. int rem, v, err = 0;
  3015. pf_sw = pf->first_sw;
  3016. /* find the attribute in the netlink message */
  3017. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  3018. nla_for_each_nested(attr, br_spec, rem) {
  3019. __u16 mode;
  3020. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  3021. continue;
  3022. mode = nla_get_u16(attr);
  3023. if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
  3024. return -EINVAL;
  3025. /* Continue if bridge mode is not being flipped */
  3026. if (mode == pf_sw->bridge_mode)
  3027. continue;
  3028. /* Iterates through the PF VSI list and update the loopback
  3029. * mode of the VSI
  3030. */
  3031. ice_for_each_vsi(pf, v) {
  3032. if (!pf->vsi[v])
  3033. continue;
  3034. err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
  3035. if (err)
  3036. return err;
  3037. }
  3038. hw->evb_veb = (mode == BRIDGE_MODE_VEB);
  3039. /* Update the unicast switch filter rules for the corresponding
  3040. * switch of the netdev
  3041. */
  3042. status = ice_update_sw_rule_bridge_mode(hw);
  3043. if (status) {
  3044. netdev_err(dev, "update SW_RULE for bridge mode failed, = %d err %d aq_err %d\n",
  3045. mode, status, hw->adminq.sq_last_status);
  3046. /* revert hw->evb_veb */
  3047. hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
  3048. return -EIO;
  3049. }
  3050. pf_sw->bridge_mode = mode;
  3051. }
  3052. return 0;
  3053. }
  3054. /**
  3055. * ice_tx_timeout - Respond to a Tx Hang
  3056. * @netdev: network interface device structure
  3057. */
  3058. static void ice_tx_timeout(struct net_device *netdev)
  3059. {
  3060. struct ice_netdev_priv *np = netdev_priv(netdev);
  3061. struct ice_ring *tx_ring = NULL;
  3062. struct ice_vsi *vsi = np->vsi;
  3063. struct ice_pf *pf = vsi->back;
  3064. u32 head, val = 0, i;
  3065. int hung_queue = -1;
  3066. pf->tx_timeout_count++;
  3067. /* find the stopped queue the same way the stack does */
  3068. for (i = 0; i < netdev->num_tx_queues; i++) {
  3069. struct netdev_queue *q;
  3070. unsigned long trans_start;
  3071. q = netdev_get_tx_queue(netdev, i);
  3072. trans_start = q->trans_start;
  3073. if (netif_xmit_stopped(q) &&
  3074. time_after(jiffies,
  3075. (trans_start + netdev->watchdog_timeo))) {
  3076. hung_queue = i;
  3077. break;
  3078. }
  3079. }
  3080. if (i == netdev->num_tx_queues) {
  3081. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  3082. } else {
  3083. /* now that we have an index, find the tx_ring struct */
  3084. for (i = 0; i < vsi->num_txq; i++) {
  3085. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  3086. if (hung_queue ==
  3087. vsi->tx_rings[i]->q_index) {
  3088. tx_ring = vsi->tx_rings[i];
  3089. break;
  3090. }
  3091. }
  3092. }
  3093. }
  3094. /* Reset recovery level if enough time has elapsed after last timeout.
  3095. * Also ensure no new reset action happens before next timeout period.
  3096. */
  3097. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
  3098. pf->tx_timeout_recovery_level = 1;
  3099. else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
  3100. netdev->watchdog_timeo)))
  3101. return;
  3102. if (tx_ring) {
  3103. head = tx_ring->next_to_clean;
  3104. /* Read interrupt register */
  3105. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  3106. val = rd32(&pf->hw,
  3107. GLINT_DYN_CTL(tx_ring->q_vector->v_idx +
  3108. tx_ring->vsi->hw_base_vector));
  3109. netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
  3110. vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
  3111. head, tx_ring->next_to_use,
  3112. readl(tx_ring->tail), val);
  3113. }
  3114. pf->tx_timeout_last_recovery = jiffies;
  3115. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  3116. pf->tx_timeout_recovery_level, hung_queue);
  3117. switch (pf->tx_timeout_recovery_level) {
  3118. case 1:
  3119. set_bit(__ICE_PFR_REQ, pf->state);
  3120. break;
  3121. case 2:
  3122. set_bit(__ICE_CORER_REQ, pf->state);
  3123. break;
  3124. case 3:
  3125. set_bit(__ICE_GLOBR_REQ, pf->state);
  3126. break;
  3127. default:
  3128. netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
  3129. set_bit(__ICE_DOWN, pf->state);
  3130. set_bit(__ICE_NEEDS_RESTART, vsi->state);
  3131. set_bit(__ICE_SERVICE_DIS, pf->state);
  3132. break;
  3133. }
  3134. ice_service_task_schedule(pf);
  3135. pf->tx_timeout_recovery_level++;
  3136. }
  3137. /**
  3138. * ice_open - Called when a network interface becomes active
  3139. * @netdev: network interface device structure
  3140. *
  3141. * The open entry point is called when a network interface is made
  3142. * active by the system (IFF_UP). At this point all resources needed
  3143. * for transmit and receive operations are allocated, the interrupt
  3144. * handler is registered with the OS, the netdev watchdog is enabled,
  3145. * and the stack is notified that the interface is ready.
  3146. *
  3147. * Returns 0 on success, negative value on failure
  3148. */
  3149. static int ice_open(struct net_device *netdev)
  3150. {
  3151. struct ice_netdev_priv *np = netdev_priv(netdev);
  3152. struct ice_vsi *vsi = np->vsi;
  3153. int err;
  3154. if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
  3155. netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
  3156. return -EIO;
  3157. }
  3158. netif_carrier_off(netdev);
  3159. err = ice_vsi_open(vsi);
  3160. if (err)
  3161. netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
  3162. vsi->vsi_num, vsi->vsw->sw_id);
  3163. return err;
  3164. }
  3165. /**
  3166. * ice_stop - Disables a network interface
  3167. * @netdev: network interface device structure
  3168. *
  3169. * The stop entry point is called when an interface is de-activated by the OS,
  3170. * and the netdevice enters the DOWN state. The hardware is still under the
  3171. * driver's control, but the netdev interface is disabled.
  3172. *
  3173. * Returns success only - not allowed to fail
  3174. */
  3175. static int ice_stop(struct net_device *netdev)
  3176. {
  3177. struct ice_netdev_priv *np = netdev_priv(netdev);
  3178. struct ice_vsi *vsi = np->vsi;
  3179. ice_vsi_close(vsi);
  3180. return 0;
  3181. }
  3182. /**
  3183. * ice_features_check - Validate encapsulated packet conforms to limits
  3184. * @skb: skb buffer
  3185. * @netdev: This port's netdev
  3186. * @features: Offload features that the stack believes apply
  3187. */
  3188. static netdev_features_t
  3189. ice_features_check(struct sk_buff *skb,
  3190. struct net_device __always_unused *netdev,
  3191. netdev_features_t features)
  3192. {
  3193. size_t len;
  3194. /* No point in doing any of this if neither checksum nor GSO are
  3195. * being requested for this frame. We can rule out both by just
  3196. * checking for CHECKSUM_PARTIAL
  3197. */
  3198. if (skb->ip_summed != CHECKSUM_PARTIAL)
  3199. return features;
  3200. /* We cannot support GSO if the MSS is going to be less than
  3201. * 64 bytes. If it is then we need to drop support for GSO.
  3202. */
  3203. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  3204. features &= ~NETIF_F_GSO_MASK;
  3205. len = skb_network_header(skb) - skb->data;
  3206. if (len & ~(ICE_TXD_MACLEN_MAX))
  3207. goto out_rm_features;
  3208. len = skb_transport_header(skb) - skb_network_header(skb);
  3209. if (len & ~(ICE_TXD_IPLEN_MAX))
  3210. goto out_rm_features;
  3211. if (skb->encapsulation) {
  3212. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  3213. if (len & ~(ICE_TXD_L4LEN_MAX))
  3214. goto out_rm_features;
  3215. len = skb_inner_transport_header(skb) -
  3216. skb_inner_network_header(skb);
  3217. if (len & ~(ICE_TXD_IPLEN_MAX))
  3218. goto out_rm_features;
  3219. }
  3220. return features;
  3221. out_rm_features:
  3222. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  3223. }
  3224. static const struct net_device_ops ice_netdev_ops = {
  3225. .ndo_open = ice_open,
  3226. .ndo_stop = ice_stop,
  3227. .ndo_start_xmit = ice_start_xmit,
  3228. .ndo_features_check = ice_features_check,
  3229. .ndo_set_rx_mode = ice_set_rx_mode,
  3230. .ndo_set_mac_address = ice_set_mac_address,
  3231. .ndo_validate_addr = eth_validate_addr,
  3232. .ndo_change_mtu = ice_change_mtu,
  3233. .ndo_get_stats64 = ice_get_stats64,
  3234. .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
  3235. .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
  3236. .ndo_set_features = ice_set_features,
  3237. .ndo_bridge_getlink = ice_bridge_getlink,
  3238. .ndo_bridge_setlink = ice_bridge_setlink,
  3239. .ndo_fdb_add = ice_fdb_add,
  3240. .ndo_fdb_del = ice_fdb_del,
  3241. .ndo_tx_timeout = ice_tx_timeout,
  3242. };