ice_main.c 101 KB

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