ice_main.c 101 KB

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