ice_main.c 96 KB

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