ice_main.c 99 KB

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