ice_main.c 101 KB

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