ice_main.c 100 KB

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