ice_main.c 101 KB

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