ice_main.c 100 KB

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