ice_main.c 97 KB

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