ionic_lif.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
  3. #include <linux/ethtool.h>
  4. #include <linux/printk.h>
  5. #include <linux/dynamic_debug.h>
  6. #include <linux/netdevice.h>
  7. #include <linux/etherdevice.h>
  8. #include <linux/if_vlan.h>
  9. #include <linux/rtnetlink.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/pci.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/crash_dump.h>
  14. #include <linux/vmalloc.h>
  15. #include <net/page_pool/helpers.h>
  16. #include "ionic.h"
  17. #include "ionic_bus.h"
  18. #include "ionic_dev.h"
  19. #include "ionic_lif.h"
  20. #include "ionic_txrx.h"
  21. #include "ionic_ethtool.h"
  22. #include "ionic_debugfs.h"
  23. /* queuetype support level */
  24. static const u8 ionic_qtype_versions[IONIC_QTYPE_MAX] = {
  25. [IONIC_QTYPE_ADMINQ] = 0, /* 0 = Base version with CQ support */
  26. [IONIC_QTYPE_NOTIFYQ] = 0, /* 0 = Base version */
  27. [IONIC_QTYPE_RXQ] = 2, /* 0 = Base version with CQ+SG support
  28. * 2 = ... with CMB rings
  29. */
  30. [IONIC_QTYPE_TXQ] = 3, /* 0 = Base version with CQ+SG support
  31. * 1 = ... with Tx SG version 1
  32. * 3 = ... with CMB rings
  33. */
  34. };
  35. static void ionic_link_status_check(struct ionic_lif *lif);
  36. static void ionic_lif_handle_fw_down(struct ionic_lif *lif);
  37. static void ionic_lif_handle_fw_up(struct ionic_lif *lif);
  38. static void ionic_lif_set_netdev_info(struct ionic_lif *lif);
  39. static void ionic_txrx_deinit(struct ionic_lif *lif);
  40. static int ionic_txrx_init(struct ionic_lif *lif);
  41. static int ionic_start_queues(struct ionic_lif *lif);
  42. static void ionic_stop_queues(struct ionic_lif *lif);
  43. static void ionic_lif_queue_identify(struct ionic_lif *lif);
  44. static void ionic_xdp_rxqs_prog_update(struct ionic_lif *lif);
  45. static void ionic_unregister_rxq_info(struct ionic_queue *q);
  46. static int ionic_register_rxq_info(struct ionic_queue *q, unsigned int napi_id);
  47. static void ionic_dim_work(struct work_struct *work)
  48. {
  49. struct dim *dim = container_of(work, struct dim, work);
  50. struct dim_cq_moder cur_moder;
  51. struct ionic_intr_info *intr;
  52. struct ionic_qcq *qcq;
  53. struct ionic_lif *lif;
  54. struct ionic_queue *q;
  55. u32 new_coal;
  56. qcq = container_of(dim, struct ionic_qcq, dim);
  57. q = &qcq->q;
  58. if (q->type == IONIC_QTYPE_RXQ)
  59. cur_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
  60. else
  61. cur_moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
  62. lif = q->lif;
  63. new_coal = ionic_coal_usec_to_hw(lif->ionic, cur_moder.usec);
  64. new_coal = new_coal ? new_coal : 1;
  65. intr = &qcq->intr;
  66. if (intr->dim_coal_hw != new_coal) {
  67. intr->dim_coal_hw = new_coal;
  68. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  69. intr->index, intr->dim_coal_hw);
  70. }
  71. dim->state = DIM_START_MEASURE;
  72. }
  73. static void ionic_lif_deferred_work(struct work_struct *work)
  74. {
  75. struct ionic_lif *lif = container_of(work, struct ionic_lif, deferred.work);
  76. struct ionic_deferred *def = &lif->deferred;
  77. struct ionic_deferred_work *w = NULL;
  78. do {
  79. spin_lock_bh(&def->lock);
  80. if (!list_empty(&def->list)) {
  81. w = list_first_entry(&def->list,
  82. struct ionic_deferred_work, list);
  83. list_del(&w->list);
  84. }
  85. spin_unlock_bh(&def->lock);
  86. if (!w)
  87. break;
  88. switch (w->type) {
  89. case IONIC_DW_TYPE_RX_MODE:
  90. ionic_lif_rx_mode(lif);
  91. break;
  92. case IONIC_DW_TYPE_LINK_STATUS:
  93. ionic_link_status_check(lif);
  94. break;
  95. case IONIC_DW_TYPE_LIF_RESET:
  96. if (w->fw_status) {
  97. ionic_lif_handle_fw_up(lif);
  98. } else {
  99. ionic_lif_handle_fw_down(lif);
  100. /* Fire off another watchdog to see
  101. * if the FW is already back rather than
  102. * waiting another whole cycle
  103. */
  104. mod_timer(&lif->ionic->watchdog_timer, jiffies + 1);
  105. }
  106. break;
  107. default:
  108. break;
  109. }
  110. kfree(w);
  111. w = NULL;
  112. } while (true);
  113. }
  114. void ionic_lif_deferred_enqueue(struct ionic_lif *lif,
  115. struct ionic_deferred_work *work)
  116. {
  117. spin_lock_bh(&lif->deferred.lock);
  118. list_add_tail(&work->list, &lif->deferred.list);
  119. spin_unlock_bh(&lif->deferred.lock);
  120. queue_work(lif->ionic->wq, &lif->deferred.work);
  121. }
  122. static void ionic_link_status_check(struct ionic_lif *lif)
  123. {
  124. struct net_device *netdev = lif->netdev;
  125. u16 link_status;
  126. bool link_up;
  127. if (!test_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
  128. return;
  129. /* Don't put carrier back up if we're in a broken state */
  130. if (test_bit(IONIC_LIF_F_BROKEN, lif->state)) {
  131. clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
  132. return;
  133. }
  134. link_status = le16_to_cpu(lif->info->status.link_status);
  135. link_up = link_status == IONIC_PORT_OPER_STATUS_UP;
  136. if (link_up) {
  137. int err = 0;
  138. if (netdev->flags & IFF_UP && netif_running(netdev)) {
  139. mutex_lock(&lif->queue_lock);
  140. err = ionic_start_queues(lif);
  141. if (err && err != -EBUSY) {
  142. netdev_err(netdev,
  143. "Failed to start queues: %d\n", err);
  144. set_bit(IONIC_LIF_F_BROKEN, lif->state);
  145. netif_carrier_off(lif->netdev);
  146. }
  147. mutex_unlock(&lif->queue_lock);
  148. }
  149. if (!err && !netif_carrier_ok(netdev)) {
  150. ionic_port_identify(lif->ionic);
  151. netdev_info(netdev, "Link up - %d Gbps\n",
  152. le32_to_cpu(lif->info->status.link_speed) / 1000);
  153. netif_carrier_on(netdev);
  154. }
  155. } else {
  156. if (netif_carrier_ok(netdev)) {
  157. lif->link_down_count++;
  158. netdev_info(netdev, "Link down\n");
  159. netif_carrier_off(netdev);
  160. }
  161. if (netdev->flags & IFF_UP && netif_running(netdev)) {
  162. mutex_lock(&lif->queue_lock);
  163. ionic_stop_queues(lif);
  164. mutex_unlock(&lif->queue_lock);
  165. }
  166. }
  167. clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
  168. }
  169. void ionic_link_status_check_request(struct ionic_lif *lif, bool can_sleep)
  170. {
  171. struct ionic_deferred_work *work;
  172. /* we only need one request outstanding at a time */
  173. if (test_and_set_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
  174. return;
  175. if (!can_sleep) {
  176. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  177. if (!work) {
  178. clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
  179. return;
  180. }
  181. work->type = IONIC_DW_TYPE_LINK_STATUS;
  182. ionic_lif_deferred_enqueue(lif, work);
  183. } else {
  184. ionic_link_status_check(lif);
  185. }
  186. }
  187. static irqreturn_t ionic_isr(int irq, void *data)
  188. {
  189. struct napi_struct *napi = data;
  190. napi_schedule_irqoff(napi);
  191. return IRQ_HANDLED;
  192. }
  193. static int ionic_request_irq(struct ionic_lif *lif, struct ionic_qcq *qcq)
  194. {
  195. struct ionic_intr_info *intr = &qcq->intr;
  196. struct device *dev = lif->ionic->dev;
  197. struct ionic_queue *q = &qcq->q;
  198. const char *name;
  199. if (lif->registered)
  200. name = netdev_name(lif->netdev);
  201. else
  202. name = dev_name(dev);
  203. snprintf(intr->name, sizeof(intr->name),
  204. "%.5s-%.16s-%.8s", IONIC_DRV_NAME, name, q->name);
  205. return devm_request_irq(dev, intr->vector, ionic_isr,
  206. 0, intr->name, &qcq->napi);
  207. }
  208. static int ionic_intr_alloc(struct ionic_lif *lif, struct ionic_intr_info *intr)
  209. {
  210. struct ionic *ionic = lif->ionic;
  211. int index;
  212. index = find_first_zero_bit(ionic->intrs, ionic->nintrs);
  213. if (index == ionic->nintrs) {
  214. netdev_warn(lif->netdev, "%s: no intr, index=%d nintrs=%d\n",
  215. __func__, index, ionic->nintrs);
  216. return -ENOSPC;
  217. }
  218. set_bit(index, ionic->intrs);
  219. ionic_intr_init(&ionic->idev, intr, index);
  220. return 0;
  221. }
  222. static void ionic_intr_free(struct ionic *ionic, int index)
  223. {
  224. if (index != IONIC_INTR_INDEX_NOT_ASSIGNED && index < ionic->nintrs)
  225. clear_bit(index, ionic->intrs);
  226. }
  227. static void ionic_irq_aff_notify(struct irq_affinity_notify *notify,
  228. const cpumask_t *mask)
  229. {
  230. struct ionic_intr_info *intr = container_of(notify, struct ionic_intr_info, aff_notify);
  231. cpumask_copy(*intr->affinity_mask, mask);
  232. }
  233. static void ionic_irq_aff_release(struct kref __always_unused *ref)
  234. {
  235. }
  236. static int ionic_qcq_enable(struct ionic_qcq *qcq)
  237. {
  238. struct ionic_queue *q = &qcq->q;
  239. struct ionic_lif *lif = q->lif;
  240. struct ionic_dev *idev;
  241. struct device *dev;
  242. struct ionic_admin_ctx ctx = {
  243. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  244. .cmd.q_control = {
  245. .opcode = IONIC_CMD_Q_CONTROL,
  246. .lif_index = cpu_to_le16(lif->index),
  247. .type = q->type,
  248. .index = cpu_to_le32(q->index),
  249. .oper = IONIC_Q_ENABLE,
  250. },
  251. };
  252. int ret;
  253. idev = &lif->ionic->idev;
  254. dev = lif->ionic->dev;
  255. dev_dbg(dev, "q_enable.index %d q_enable.qtype %d\n",
  256. ctx.cmd.q_control.index, ctx.cmd.q_control.type);
  257. if (qcq->flags & IONIC_QCQ_F_INTR)
  258. ionic_intr_clean(idev->intr_ctrl, qcq->intr.index);
  259. ret = ionic_adminq_post_wait(lif, &ctx);
  260. if (ret)
  261. return ret;
  262. if (qcq->flags & IONIC_QCQ_F_INTR) {
  263. napi_enable(&qcq->napi);
  264. irq_set_affinity_notifier(qcq->intr.vector,
  265. &qcq->intr.aff_notify);
  266. irq_set_affinity_hint(qcq->intr.vector,
  267. *qcq->intr.affinity_mask);
  268. ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
  269. IONIC_INTR_MASK_CLEAR);
  270. }
  271. return 0;
  272. }
  273. static int ionic_qcq_disable(struct ionic_lif *lif, struct ionic_qcq *qcq, int fw_err)
  274. {
  275. struct ionic_queue *q;
  276. struct ionic_admin_ctx ctx = {
  277. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  278. .cmd.q_control = {
  279. .opcode = IONIC_CMD_Q_CONTROL,
  280. .oper = IONIC_Q_DISABLE,
  281. },
  282. };
  283. if (!qcq) {
  284. netdev_err(lif->netdev, "%s: bad qcq\n", __func__);
  285. return -ENXIO;
  286. }
  287. q = &qcq->q;
  288. if (qcq->flags & IONIC_QCQ_F_INTR) {
  289. struct ionic_dev *idev = &lif->ionic->idev;
  290. if (lif->doorbell_wa)
  291. cancel_work_sync(&qcq->doorbell_napi_work);
  292. cancel_work_sync(&qcq->dim.work);
  293. ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
  294. IONIC_INTR_MASK_SET);
  295. synchronize_irq(qcq->intr.vector);
  296. irq_set_affinity_notifier(qcq->intr.vector, NULL);
  297. irq_set_affinity_hint(qcq->intr.vector, NULL);
  298. napi_disable(&qcq->napi);
  299. }
  300. /* If there was a previous fw communcation error, don't bother with
  301. * sending the adminq command and just return the same error value.
  302. */
  303. if (fw_err == -ETIMEDOUT || fw_err == -ENXIO)
  304. return fw_err;
  305. ctx.cmd.q_control.lif_index = cpu_to_le16(lif->index);
  306. ctx.cmd.q_control.type = q->type;
  307. ctx.cmd.q_control.index = cpu_to_le32(q->index);
  308. dev_dbg(lif->ionic->dev, "q_disable.index %d q_disable.qtype %d\n",
  309. ctx.cmd.q_control.index, ctx.cmd.q_control.type);
  310. return ionic_adminq_post_wait(lif, &ctx);
  311. }
  312. static void ionic_lif_qcq_deinit(struct ionic_lif *lif, struct ionic_qcq *qcq)
  313. {
  314. struct ionic_dev *idev = &lif->ionic->idev;
  315. if (!qcq)
  316. return;
  317. if (!(qcq->flags & IONIC_QCQ_F_INITED))
  318. return;
  319. ionic_unregister_rxq_info(&qcq->q);
  320. if (qcq->flags & IONIC_QCQ_F_INTR) {
  321. ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
  322. IONIC_INTR_MASK_SET);
  323. netif_napi_del(&qcq->napi);
  324. }
  325. qcq->flags &= ~IONIC_QCQ_F_INITED;
  326. }
  327. static void ionic_qcq_intr_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
  328. {
  329. if (!(qcq->flags & IONIC_QCQ_F_INTR) || qcq->intr.vector == 0)
  330. return;
  331. irq_set_affinity_hint(qcq->intr.vector, NULL);
  332. devm_free_irq(lif->ionic->dev, qcq->intr.vector, &qcq->napi);
  333. qcq->intr.vector = 0;
  334. ionic_intr_free(lif->ionic, qcq->intr.index);
  335. qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
  336. }
  337. static void ionic_qcq_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
  338. {
  339. struct device *dev = lif->ionic->dev;
  340. if (!qcq)
  341. return;
  342. ionic_debugfs_del_qcq(qcq);
  343. if (qcq->q_base) {
  344. dma_free_coherent(dev, qcq->q_size, qcq->q_base, qcq->q_base_pa);
  345. qcq->q_base = NULL;
  346. qcq->q_base_pa = 0;
  347. }
  348. if (qcq->cmb_q_base) {
  349. iounmap(qcq->cmb_q_base);
  350. ionic_put_cmb(lif, qcq->cmb_pgid, qcq->cmb_order);
  351. qcq->cmb_pgid = 0;
  352. qcq->cmb_order = 0;
  353. qcq->cmb_q_base = NULL;
  354. qcq->cmb_q_base_pa = 0;
  355. }
  356. if (qcq->cq_base) {
  357. dma_free_coherent(dev, qcq->cq_size, qcq->cq_base, qcq->cq_base_pa);
  358. qcq->cq_base = NULL;
  359. qcq->cq_base_pa = 0;
  360. }
  361. if (qcq->sg_base) {
  362. dma_free_coherent(dev, qcq->sg_size, qcq->sg_base, qcq->sg_base_pa);
  363. qcq->sg_base = NULL;
  364. qcq->sg_base_pa = 0;
  365. }
  366. page_pool_destroy(qcq->q.page_pool);
  367. qcq->q.page_pool = NULL;
  368. ionic_qcq_intr_free(lif, qcq);
  369. vfree(qcq->q.info);
  370. qcq->q.info = NULL;
  371. }
  372. void ionic_qcqs_free(struct ionic_lif *lif)
  373. {
  374. struct device *dev = lif->ionic->dev;
  375. struct ionic_qcq *adminqcq;
  376. unsigned long irqflags;
  377. if (lif->notifyqcq) {
  378. ionic_qcq_free(lif, lif->notifyqcq);
  379. devm_kfree(dev, lif->notifyqcq);
  380. lif->notifyqcq = NULL;
  381. }
  382. if (lif->adminqcq) {
  383. spin_lock_irqsave(&lif->adminq_lock, irqflags);
  384. adminqcq = READ_ONCE(lif->adminqcq);
  385. lif->adminqcq = NULL;
  386. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  387. if (adminqcq) {
  388. ionic_qcq_free(lif, adminqcq);
  389. devm_kfree(dev, adminqcq);
  390. }
  391. }
  392. if (lif->rxqcqs) {
  393. devm_kfree(dev, lif->rxqstats);
  394. lif->rxqstats = NULL;
  395. devm_kfree(dev, lif->rxqcqs);
  396. lif->rxqcqs = NULL;
  397. }
  398. if (lif->txqcqs) {
  399. devm_kfree(dev, lif->txqstats);
  400. lif->txqstats = NULL;
  401. devm_kfree(dev, lif->txqcqs);
  402. lif->txqcqs = NULL;
  403. }
  404. }
  405. static void ionic_link_qcq_interrupts(struct ionic_qcq *src_qcq,
  406. struct ionic_qcq *n_qcq)
  407. {
  408. n_qcq->intr.vector = src_qcq->intr.vector;
  409. n_qcq->intr.index = src_qcq->intr.index;
  410. }
  411. static int ionic_alloc_qcq_interrupt(struct ionic_lif *lif, struct ionic_qcq *qcq)
  412. {
  413. cpumask_var_t *affinity_mask;
  414. int err;
  415. if (!(qcq->flags & IONIC_QCQ_F_INTR)) {
  416. qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
  417. return 0;
  418. }
  419. err = ionic_intr_alloc(lif, &qcq->intr);
  420. if (err) {
  421. netdev_warn(lif->netdev, "no intr for %s: %d\n",
  422. qcq->q.name, err);
  423. goto err_out;
  424. }
  425. err = ionic_bus_get_irq(lif->ionic, qcq->intr.index);
  426. if (err < 0) {
  427. netdev_warn(lif->netdev, "no vector for %s: %d\n",
  428. qcq->q.name, err);
  429. goto err_out_free_intr;
  430. }
  431. qcq->intr.vector = err;
  432. ionic_intr_mask_assert(lif->ionic->idev.intr_ctrl, qcq->intr.index,
  433. IONIC_INTR_MASK_SET);
  434. err = ionic_request_irq(lif, qcq);
  435. if (err) {
  436. netdev_warn(lif->netdev, "irq request failed %d\n", err);
  437. goto err_out_free_intr;
  438. }
  439. /* try to get the irq on the local numa node first */
  440. affinity_mask = &lif->ionic->affinity_masks[qcq->intr.index];
  441. if (cpumask_empty(*affinity_mask)) {
  442. unsigned int cpu;
  443. cpu = cpumask_local_spread(qcq->intr.index,
  444. dev_to_node(lif->ionic->dev));
  445. if (cpu != -1)
  446. cpumask_set_cpu(cpu, *affinity_mask);
  447. }
  448. qcq->intr.affinity_mask = affinity_mask;
  449. qcq->intr.aff_notify.notify = ionic_irq_aff_notify;
  450. qcq->intr.aff_notify.release = ionic_irq_aff_release;
  451. netdev_dbg(lif->netdev, "%s: Interrupt index %d\n", qcq->q.name, qcq->intr.index);
  452. return 0;
  453. err_out_free_intr:
  454. ionic_intr_free(lif->ionic, qcq->intr.index);
  455. err_out:
  456. return err;
  457. }
  458. static int ionic_qcq_alloc(struct ionic_lif *lif, unsigned int type,
  459. unsigned int index,
  460. const char *name, unsigned int flags,
  461. unsigned int num_descs, unsigned int desc_size,
  462. unsigned int cq_desc_size,
  463. unsigned int sg_desc_size,
  464. unsigned int desc_info_size,
  465. unsigned int pid, struct bpf_prog *xdp_prog,
  466. struct ionic_qcq **qcq)
  467. {
  468. struct ionic_dev *idev = &lif->ionic->idev;
  469. struct device *dev = lif->ionic->dev;
  470. struct ionic_qcq *new;
  471. int err;
  472. *qcq = NULL;
  473. new = devm_kzalloc(dev, sizeof(*new), GFP_KERNEL);
  474. if (!new) {
  475. netdev_err(lif->netdev, "Cannot allocate queue structure\n");
  476. err = -ENOMEM;
  477. goto err_out;
  478. }
  479. new->q.dev = dev;
  480. new->flags = flags;
  481. new->q.info = vcalloc(num_descs, desc_info_size);
  482. if (!new->q.info) {
  483. netdev_err(lif->netdev, "Cannot allocate queue info\n");
  484. err = -ENOMEM;
  485. goto err_out_free_qcq;
  486. }
  487. if (type == IONIC_QTYPE_RXQ) {
  488. struct page_pool_params pp_params = {
  489. .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
  490. .order = 0,
  491. .pool_size = num_descs,
  492. .nid = NUMA_NO_NODE,
  493. .dev = lif->ionic->dev,
  494. .napi = &new->napi,
  495. .dma_dir = DMA_FROM_DEVICE,
  496. .max_len = PAGE_SIZE,
  497. .netdev = lif->netdev,
  498. };
  499. if (xdp_prog)
  500. pp_params.dma_dir = DMA_BIDIRECTIONAL;
  501. new->q.page_pool = page_pool_create(&pp_params);
  502. if (IS_ERR(new->q.page_pool)) {
  503. netdev_err(lif->netdev, "Cannot create page_pool\n");
  504. err = PTR_ERR(new->q.page_pool);
  505. new->q.page_pool = NULL;
  506. goto err_out_free_q_info;
  507. }
  508. }
  509. new->q.type = type;
  510. new->q.max_sg_elems = lif->qtype_info[type].max_sg_elems;
  511. err = ionic_q_init(lif, idev, &new->q, index, name, num_descs,
  512. desc_size, sg_desc_size, pid);
  513. if (err) {
  514. netdev_err(lif->netdev, "Cannot initialize queue\n");
  515. goto err_out_free_page_pool;
  516. }
  517. err = ionic_alloc_qcq_interrupt(lif, new);
  518. if (err)
  519. goto err_out_free_page_pool;
  520. err = ionic_cq_init(lif, &new->cq, &new->intr, num_descs, cq_desc_size);
  521. if (err) {
  522. netdev_err(lif->netdev, "Cannot initialize completion queue\n");
  523. goto err_out_free_irq;
  524. }
  525. if (flags & IONIC_QCQ_F_NOTIFYQ) {
  526. int q_size;
  527. /* q & cq need to be contiguous in NotifyQ, so alloc it all in q
  528. * and don't alloc qc. We leave new->qc_size and new->qc_base
  529. * as 0 to be sure we don't try to free it later.
  530. */
  531. q_size = ALIGN(num_descs * desc_size, PAGE_SIZE);
  532. new->q_size = PAGE_SIZE + q_size +
  533. ALIGN(num_descs * cq_desc_size, PAGE_SIZE);
  534. new->q_base = dma_alloc_coherent(dev, new->q_size,
  535. &new->q_base_pa, GFP_KERNEL);
  536. if (!new->q_base) {
  537. netdev_err(lif->netdev, "Cannot allocate qcq DMA memory\n");
  538. err = -ENOMEM;
  539. goto err_out_free_irq;
  540. }
  541. new->q.base = PTR_ALIGN(new->q_base, PAGE_SIZE);
  542. new->q.base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
  543. /* Base the NotifyQ cq.base off of the ALIGNed q.base */
  544. new->cq.base = PTR_ALIGN(new->q.base + q_size, PAGE_SIZE);
  545. new->cq.base_pa = ALIGN(new->q_base_pa + q_size, PAGE_SIZE);
  546. new->cq.bound_q = &new->q;
  547. } else {
  548. /* regular DMA q descriptors */
  549. new->q_size = PAGE_SIZE + (num_descs * desc_size);
  550. new->q_base = dma_alloc_coherent(dev, new->q_size, &new->q_base_pa,
  551. GFP_KERNEL);
  552. if (!new->q_base) {
  553. netdev_err(lif->netdev, "Cannot allocate queue DMA memory\n");
  554. err = -ENOMEM;
  555. goto err_out_free_irq;
  556. }
  557. new->q.base = PTR_ALIGN(new->q_base, PAGE_SIZE);
  558. new->q.base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
  559. if (flags & IONIC_QCQ_F_CMB_RINGS) {
  560. /* on-chip CMB q descriptors */
  561. new->cmb_q_size = num_descs * desc_size;
  562. new->cmb_order = order_base_2(new->cmb_q_size / PAGE_SIZE);
  563. err = ionic_get_cmb(lif, &new->cmb_pgid, &new->cmb_q_base_pa,
  564. new->cmb_order);
  565. if (err) {
  566. netdev_err(lif->netdev,
  567. "Cannot allocate queue order %d from cmb: err %d\n",
  568. new->cmb_order, err);
  569. goto err_out_free_q;
  570. }
  571. new->cmb_q_base = ioremap_wc(new->cmb_q_base_pa, new->cmb_q_size);
  572. if (!new->cmb_q_base) {
  573. netdev_err(lif->netdev, "Cannot map queue from cmb\n");
  574. ionic_put_cmb(lif, new->cmb_pgid, new->cmb_order);
  575. err = -ENOMEM;
  576. goto err_out_free_q;
  577. }
  578. new->cmb_q_base_pa -= idev->phy_cmb_pages;
  579. new->q.cmb_base = new->cmb_q_base;
  580. new->q.cmb_base_pa = new->cmb_q_base_pa;
  581. }
  582. /* cq DMA descriptors */
  583. new->cq_size = PAGE_SIZE + (num_descs * cq_desc_size);
  584. new->cq_base = dma_alloc_coherent(dev, new->cq_size, &new->cq_base_pa,
  585. GFP_KERNEL);
  586. if (!new->cq_base) {
  587. netdev_err(lif->netdev, "Cannot allocate cq DMA memory\n");
  588. err = -ENOMEM;
  589. goto err_out_free_q;
  590. }
  591. new->cq.base = PTR_ALIGN(new->cq_base, PAGE_SIZE);
  592. new->cq.base_pa = ALIGN(new->cq_base_pa, PAGE_SIZE);
  593. new->cq.bound_q = &new->q;
  594. }
  595. if (flags & IONIC_QCQ_F_SG) {
  596. new->sg_size = PAGE_SIZE + (num_descs * sg_desc_size);
  597. new->sg_base = dma_alloc_coherent(dev, new->sg_size, &new->sg_base_pa,
  598. GFP_KERNEL);
  599. if (!new->sg_base) {
  600. netdev_err(lif->netdev, "Cannot allocate sg DMA memory\n");
  601. err = -ENOMEM;
  602. goto err_out_free_cq;
  603. }
  604. new->q.sg_base = PTR_ALIGN(new->sg_base, PAGE_SIZE);
  605. new->q.sg_base_pa = ALIGN(new->sg_base_pa, PAGE_SIZE);
  606. }
  607. INIT_WORK(&new->dim.work, ionic_dim_work);
  608. new->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
  609. if (lif->doorbell_wa)
  610. INIT_WORK(&new->doorbell_napi_work, ionic_doorbell_napi_work);
  611. *qcq = new;
  612. return 0;
  613. err_out_free_cq:
  614. dma_free_coherent(dev, new->cq_size, new->cq_base, new->cq_base_pa);
  615. err_out_free_q:
  616. if (new->cmb_q_base) {
  617. iounmap(new->cmb_q_base);
  618. ionic_put_cmb(lif, new->cmb_pgid, new->cmb_order);
  619. }
  620. dma_free_coherent(dev, new->q_size, new->q_base, new->q_base_pa);
  621. err_out_free_irq:
  622. if (flags & IONIC_QCQ_F_INTR) {
  623. devm_free_irq(dev, new->intr.vector, &new->napi);
  624. ionic_intr_free(lif->ionic, new->intr.index);
  625. }
  626. err_out_free_page_pool:
  627. page_pool_destroy(new->q.page_pool);
  628. err_out_free_q_info:
  629. vfree(new->q.info);
  630. err_out_free_qcq:
  631. devm_kfree(dev, new);
  632. err_out:
  633. dev_err(dev, "qcq alloc of %s%d failed %d\n", name, index, err);
  634. return err;
  635. }
  636. static int ionic_qcqs_alloc(struct ionic_lif *lif)
  637. {
  638. struct device *dev = lif->ionic->dev;
  639. unsigned int flags;
  640. int err;
  641. flags = IONIC_QCQ_F_INTR;
  642. err = ionic_qcq_alloc(lif, IONIC_QTYPE_ADMINQ, 0, "admin", flags,
  643. IONIC_ADMINQ_LENGTH,
  644. sizeof(struct ionic_admin_cmd),
  645. sizeof(struct ionic_admin_comp),
  646. 0,
  647. sizeof(struct ionic_admin_desc_info),
  648. lif->kern_pid, NULL, &lif->adminqcq);
  649. if (err)
  650. return err;
  651. ionic_debugfs_add_qcq(lif, lif->adminqcq);
  652. if (lif->ionic->nnqs_per_lif) {
  653. flags = IONIC_QCQ_F_NOTIFYQ;
  654. err = ionic_qcq_alloc(lif, IONIC_QTYPE_NOTIFYQ, 0, "notifyq",
  655. flags, IONIC_NOTIFYQ_LENGTH,
  656. sizeof(struct ionic_notifyq_cmd),
  657. sizeof(union ionic_notifyq_comp),
  658. 0,
  659. sizeof(struct ionic_admin_desc_info),
  660. lif->kern_pid, NULL, &lif->notifyqcq);
  661. if (err)
  662. goto err_out;
  663. ionic_debugfs_add_qcq(lif, lif->notifyqcq);
  664. /* Let the notifyq ride on the adminq interrupt */
  665. ionic_link_qcq_interrupts(lif->adminqcq, lif->notifyqcq);
  666. }
  667. err = -ENOMEM;
  668. lif->txqcqs = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif,
  669. sizeof(*lif->txqcqs), GFP_KERNEL);
  670. if (!lif->txqcqs)
  671. goto err_out;
  672. lif->rxqcqs = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif,
  673. sizeof(*lif->rxqcqs), GFP_KERNEL);
  674. if (!lif->rxqcqs)
  675. goto err_out;
  676. lif->txqstats = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif + 1,
  677. sizeof(*lif->txqstats), GFP_KERNEL);
  678. if (!lif->txqstats)
  679. goto err_out;
  680. lif->rxqstats = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif + 1,
  681. sizeof(*lif->rxqstats), GFP_KERNEL);
  682. if (!lif->rxqstats)
  683. goto err_out;
  684. return 0;
  685. err_out:
  686. ionic_qcqs_free(lif);
  687. return err;
  688. }
  689. static void ionic_qcq_sanitize(struct ionic_qcq *qcq)
  690. {
  691. qcq->q.tail_idx = 0;
  692. qcq->q.head_idx = 0;
  693. qcq->cq.tail_idx = 0;
  694. qcq->cq.done_color = 1;
  695. memset(qcq->q_base, 0, qcq->q_size);
  696. if (qcq->cmb_q_base)
  697. memset_io(qcq->cmb_q_base, 0, qcq->cmb_q_size);
  698. memset(qcq->cq_base, 0, qcq->cq_size);
  699. memset(qcq->sg_base, 0, qcq->sg_size);
  700. }
  701. static int ionic_lif_txq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
  702. {
  703. struct device *dev = lif->ionic->dev;
  704. struct ionic_queue *q = &qcq->q;
  705. struct ionic_cq *cq = &qcq->cq;
  706. struct ionic_admin_ctx ctx = {
  707. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  708. .cmd.q_init = {
  709. .opcode = IONIC_CMD_Q_INIT,
  710. .lif_index = cpu_to_le16(lif->index),
  711. .type = q->type,
  712. .ver = lif->qtype_info[q->type].version,
  713. .index = cpu_to_le32(q->index),
  714. .flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
  715. IONIC_QINIT_F_SG),
  716. .intr_index = cpu_to_le16(qcq->intr.index),
  717. .pid = cpu_to_le16(q->pid),
  718. .ring_size = ilog2(q->num_descs),
  719. .ring_base = cpu_to_le64(q->base_pa),
  720. .cq_ring_base = cpu_to_le64(cq->base_pa),
  721. .sg_ring_base = cpu_to_le64(q->sg_base_pa),
  722. .features = cpu_to_le64(q->features),
  723. },
  724. };
  725. int err;
  726. if (qcq->flags & IONIC_QCQ_F_CMB_RINGS) {
  727. ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_CMB);
  728. ctx.cmd.q_init.ring_base = cpu_to_le64(qcq->cmb_q_base_pa);
  729. }
  730. dev_dbg(dev, "txq_init.pid %d\n", ctx.cmd.q_init.pid);
  731. dev_dbg(dev, "txq_init.index %d\n", ctx.cmd.q_init.index);
  732. dev_dbg(dev, "txq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
  733. dev_dbg(dev, "txq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
  734. dev_dbg(dev, "txq_init.cq_ring_base 0x%llx\n", ctx.cmd.q_init.cq_ring_base);
  735. dev_dbg(dev, "txq_init.sg_ring_base 0x%llx\n", ctx.cmd.q_init.sg_ring_base);
  736. dev_dbg(dev, "txq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
  737. dev_dbg(dev, "txq_init.ver %d\n", ctx.cmd.q_init.ver);
  738. dev_dbg(dev, "txq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
  739. ionic_qcq_sanitize(qcq);
  740. err = ionic_adminq_post_wait(lif, &ctx);
  741. if (err)
  742. return err;
  743. q->hw_type = ctx.comp.q_init.hw_type;
  744. q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
  745. q->dbval = IONIC_DBELL_QID(q->hw_index);
  746. dev_dbg(dev, "txq->hw_type %d\n", q->hw_type);
  747. dev_dbg(dev, "txq->hw_index %d\n", q->hw_index);
  748. q->dbell_deadline = IONIC_TX_DOORBELL_DEADLINE;
  749. q->dbell_jiffies = jiffies;
  750. if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
  751. netif_napi_add(lif->netdev, &qcq->napi, ionic_tx_napi);
  752. qcq->flags |= IONIC_QCQ_F_INITED;
  753. return 0;
  754. }
  755. static int ionic_lif_rxq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
  756. {
  757. struct device *dev = lif->ionic->dev;
  758. struct ionic_queue *q = &qcq->q;
  759. struct ionic_cq *cq = &qcq->cq;
  760. struct ionic_admin_ctx ctx = {
  761. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  762. .cmd.q_init = {
  763. .opcode = IONIC_CMD_Q_INIT,
  764. .lif_index = cpu_to_le16(lif->index),
  765. .type = q->type,
  766. .ver = lif->qtype_info[q->type].version,
  767. .index = cpu_to_le32(q->index),
  768. .flags = cpu_to_le16(IONIC_QINIT_F_IRQ),
  769. .intr_index = cpu_to_le16(cq->bound_intr->index),
  770. .pid = cpu_to_le16(q->pid),
  771. .ring_size = ilog2(q->num_descs),
  772. .ring_base = cpu_to_le64(q->base_pa),
  773. .cq_ring_base = cpu_to_le64(cq->base_pa),
  774. .sg_ring_base = cpu_to_le64(q->sg_base_pa),
  775. .features = cpu_to_le64(q->features),
  776. },
  777. };
  778. int err;
  779. q->partner = &lif->txqcqs[q->index]->q;
  780. q->partner->partner = q;
  781. if (!lif->xdp_prog ||
  782. (lif->xdp_prog->aux && lif->xdp_prog->aux->xdp_has_frags))
  783. ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_SG);
  784. if (qcq->flags & IONIC_QCQ_F_CMB_RINGS) {
  785. ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_CMB);
  786. ctx.cmd.q_init.ring_base = cpu_to_le64(qcq->cmb_q_base_pa);
  787. }
  788. dev_dbg(dev, "rxq_init.pid %d\n", ctx.cmd.q_init.pid);
  789. dev_dbg(dev, "rxq_init.index %d\n", ctx.cmd.q_init.index);
  790. dev_dbg(dev, "rxq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
  791. dev_dbg(dev, "rxq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
  792. dev_dbg(dev, "rxq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
  793. dev_dbg(dev, "rxq_init.ver %d\n", ctx.cmd.q_init.ver);
  794. dev_dbg(dev, "rxq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
  795. ionic_qcq_sanitize(qcq);
  796. err = ionic_adminq_post_wait(lif, &ctx);
  797. if (err)
  798. return err;
  799. q->hw_type = ctx.comp.q_init.hw_type;
  800. q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
  801. q->dbval = IONIC_DBELL_QID(q->hw_index);
  802. dev_dbg(dev, "rxq->hw_type %d\n", q->hw_type);
  803. dev_dbg(dev, "rxq->hw_index %d\n", q->hw_index);
  804. q->dbell_deadline = IONIC_RX_MIN_DOORBELL_DEADLINE;
  805. q->dbell_jiffies = jiffies;
  806. if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
  807. netif_napi_add(lif->netdev, &qcq->napi, ionic_rx_napi);
  808. else
  809. netif_napi_add(lif->netdev, &qcq->napi, ionic_txrx_napi);
  810. err = ionic_register_rxq_info(q, qcq->napi.napi_id);
  811. if (err) {
  812. netif_napi_del(&qcq->napi);
  813. return err;
  814. }
  815. qcq->flags |= IONIC_QCQ_F_INITED;
  816. return 0;
  817. }
  818. int ionic_lif_create_hwstamp_txq(struct ionic_lif *lif)
  819. {
  820. unsigned int num_desc, desc_sz, comp_sz, sg_desc_sz;
  821. unsigned int txq_i, flags;
  822. struct ionic_qcq *txq;
  823. u64 features;
  824. int err;
  825. if (lif->hwstamp_txq)
  826. return 0;
  827. features = IONIC_Q_F_2X_CQ_DESC | IONIC_TXQ_F_HWSTAMP;
  828. num_desc = IONIC_MIN_TXRX_DESC;
  829. desc_sz = sizeof(struct ionic_txq_desc);
  830. comp_sz = 2 * sizeof(struct ionic_txq_comp);
  831. if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
  832. lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz == sizeof(struct ionic_txq_sg_desc_v1))
  833. sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
  834. else
  835. sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
  836. txq_i = lif->ionic->ntxqs_per_lif;
  837. flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
  838. err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, txq_i, "hwstamp_tx", flags,
  839. num_desc, desc_sz, comp_sz, sg_desc_sz,
  840. sizeof(struct ionic_tx_desc_info),
  841. lif->kern_pid, NULL, &txq);
  842. if (err)
  843. goto err_qcq_alloc;
  844. txq->q.features = features;
  845. ionic_link_qcq_interrupts(lif->adminqcq, txq);
  846. ionic_debugfs_add_qcq(lif, txq);
  847. lif->hwstamp_txq = txq;
  848. if (netif_running(lif->netdev)) {
  849. err = ionic_lif_txq_init(lif, txq);
  850. if (err)
  851. goto err_qcq_init;
  852. if (test_bit(IONIC_LIF_F_UP, lif->state)) {
  853. err = ionic_qcq_enable(txq);
  854. if (err)
  855. goto err_qcq_enable;
  856. }
  857. }
  858. return 0;
  859. err_qcq_enable:
  860. ionic_lif_qcq_deinit(lif, txq);
  861. err_qcq_init:
  862. lif->hwstamp_txq = NULL;
  863. ionic_debugfs_del_qcq(txq);
  864. ionic_qcq_free(lif, txq);
  865. devm_kfree(lif->ionic->dev, txq);
  866. err_qcq_alloc:
  867. return err;
  868. }
  869. int ionic_lif_create_hwstamp_rxq(struct ionic_lif *lif)
  870. {
  871. unsigned int num_desc, desc_sz, comp_sz, sg_desc_sz;
  872. unsigned int rxq_i, flags;
  873. struct ionic_qcq *rxq;
  874. u64 features;
  875. int err;
  876. if (lif->hwstamp_rxq)
  877. return 0;
  878. features = IONIC_Q_F_2X_CQ_DESC | IONIC_RXQ_F_HWSTAMP;
  879. num_desc = IONIC_MIN_TXRX_DESC;
  880. desc_sz = sizeof(struct ionic_rxq_desc);
  881. comp_sz = 2 * sizeof(struct ionic_rxq_comp);
  882. sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
  883. rxq_i = lif->ionic->nrxqs_per_lif;
  884. flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG;
  885. err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, rxq_i, "hwstamp_rx", flags,
  886. num_desc, desc_sz, comp_sz, sg_desc_sz,
  887. sizeof(struct ionic_rx_desc_info),
  888. lif->kern_pid, NULL, &rxq);
  889. if (err)
  890. goto err_qcq_alloc;
  891. rxq->q.features = features;
  892. ionic_link_qcq_interrupts(lif->adminqcq, rxq);
  893. ionic_debugfs_add_qcq(lif, rxq);
  894. lif->hwstamp_rxq = rxq;
  895. if (netif_running(lif->netdev)) {
  896. err = ionic_lif_rxq_init(lif, rxq);
  897. if (err)
  898. goto err_qcq_init;
  899. if (test_bit(IONIC_LIF_F_UP, lif->state)) {
  900. ionic_rx_fill(&rxq->q, NULL);
  901. err = ionic_qcq_enable(rxq);
  902. if (err)
  903. goto err_qcq_enable;
  904. }
  905. }
  906. return 0;
  907. err_qcq_enable:
  908. ionic_lif_qcq_deinit(lif, rxq);
  909. err_qcq_init:
  910. lif->hwstamp_rxq = NULL;
  911. ionic_debugfs_del_qcq(rxq);
  912. ionic_qcq_free(lif, rxq);
  913. devm_kfree(lif->ionic->dev, rxq);
  914. err_qcq_alloc:
  915. return err;
  916. }
  917. int ionic_lif_config_hwstamp_rxq_all(struct ionic_lif *lif, bool rx_all)
  918. {
  919. struct ionic_queue_params qparam;
  920. ionic_init_queue_params(lif, &qparam);
  921. if (rx_all)
  922. qparam.rxq_features = IONIC_Q_F_2X_CQ_DESC | IONIC_RXQ_F_HWSTAMP;
  923. else
  924. qparam.rxq_features = 0;
  925. /* if we're not running, just set the values and return */
  926. if (!netif_running(lif->netdev)) {
  927. lif->rxq_features = qparam.rxq_features;
  928. return 0;
  929. }
  930. return ionic_reconfigure_queues(lif, &qparam);
  931. }
  932. int ionic_lif_set_hwstamp_txmode(struct ionic_lif *lif, u16 txstamp_mode)
  933. {
  934. struct ionic_admin_ctx ctx = {
  935. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  936. .cmd.lif_setattr = {
  937. .opcode = IONIC_CMD_LIF_SETATTR,
  938. .index = cpu_to_le16(lif->index),
  939. .attr = IONIC_LIF_ATTR_TXSTAMP,
  940. .txstamp_mode = cpu_to_le16(txstamp_mode),
  941. },
  942. };
  943. return ionic_adminq_post_wait(lif, &ctx);
  944. }
  945. static void ionic_lif_del_hwstamp_rxfilt(struct ionic_lif *lif)
  946. {
  947. struct ionic_admin_ctx ctx = {
  948. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  949. .cmd.rx_filter_del = {
  950. .opcode = IONIC_CMD_RX_FILTER_DEL,
  951. .lif_index = cpu_to_le16(lif->index),
  952. },
  953. };
  954. struct ionic_rx_filter *f;
  955. u32 filter_id;
  956. int err;
  957. spin_lock_bh(&lif->rx_filters.lock);
  958. f = ionic_rx_filter_rxsteer(lif);
  959. if (!f) {
  960. spin_unlock_bh(&lif->rx_filters.lock);
  961. return;
  962. }
  963. filter_id = f->filter_id;
  964. ionic_rx_filter_free(lif, f);
  965. spin_unlock_bh(&lif->rx_filters.lock);
  966. netdev_dbg(lif->netdev, "rx_filter del RXSTEER (id %d)\n", filter_id);
  967. ctx.cmd.rx_filter_del.filter_id = cpu_to_le32(filter_id);
  968. err = ionic_adminq_post_wait(lif, &ctx);
  969. if (err && err != -EEXIST)
  970. netdev_dbg(lif->netdev, "failed to delete rx_filter RXSTEER (id %d)\n", filter_id);
  971. }
  972. static int ionic_lif_add_hwstamp_rxfilt(struct ionic_lif *lif, u64 pkt_class)
  973. {
  974. struct ionic_admin_ctx ctx = {
  975. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  976. .cmd.rx_filter_add = {
  977. .opcode = IONIC_CMD_RX_FILTER_ADD,
  978. .lif_index = cpu_to_le16(lif->index),
  979. .match = cpu_to_le16(IONIC_RX_FILTER_STEER_PKTCLASS),
  980. .pkt_class = cpu_to_le64(pkt_class),
  981. },
  982. };
  983. u8 qtype;
  984. u32 qid;
  985. int err;
  986. if (!lif->hwstamp_rxq)
  987. return -EINVAL;
  988. qtype = lif->hwstamp_rxq->q.type;
  989. ctx.cmd.rx_filter_add.qtype = qtype;
  990. qid = lif->hwstamp_rxq->q.index;
  991. ctx.cmd.rx_filter_add.qid = cpu_to_le32(qid);
  992. netdev_dbg(lif->netdev, "rx_filter add RXSTEER\n");
  993. err = ionic_adminq_post_wait(lif, &ctx);
  994. if (err && err != -EEXIST)
  995. return err;
  996. spin_lock_bh(&lif->rx_filters.lock);
  997. err = ionic_rx_filter_save(lif, 0, qid, 0, &ctx, IONIC_FILTER_STATE_SYNCED);
  998. spin_unlock_bh(&lif->rx_filters.lock);
  999. return err;
  1000. }
  1001. int ionic_lif_set_hwstamp_rxfilt(struct ionic_lif *lif, u64 pkt_class)
  1002. {
  1003. ionic_lif_del_hwstamp_rxfilt(lif);
  1004. if (!pkt_class)
  1005. return 0;
  1006. return ionic_lif_add_hwstamp_rxfilt(lif, pkt_class);
  1007. }
  1008. static int ionic_adminq_napi(struct napi_struct *napi, int budget)
  1009. {
  1010. struct ionic_intr_info *intr = napi_to_cq(napi)->bound_intr;
  1011. struct ionic_lif *lif = napi_to_cq(napi)->lif;
  1012. struct ionic_dev *idev = &lif->ionic->idev;
  1013. unsigned long irqflags;
  1014. unsigned int flags = 0;
  1015. int rx_work = 0;
  1016. int tx_work = 0;
  1017. int n_work = 0;
  1018. int a_work = 0;
  1019. int work_done;
  1020. int credits;
  1021. if (lif->notifyqcq && lif->notifyqcq->flags & IONIC_QCQ_F_INITED)
  1022. n_work = ionic_cq_service(&lif->notifyqcq->cq, budget,
  1023. ionic_notifyq_service, NULL, NULL);
  1024. spin_lock_irqsave(&lif->adminq_lock, irqflags);
  1025. if (lif->adminqcq && lif->adminqcq->flags & IONIC_QCQ_F_INITED)
  1026. a_work = ionic_cq_service(&lif->adminqcq->cq, budget,
  1027. ionic_adminq_service, NULL, NULL);
  1028. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  1029. if (lif->hwstamp_rxq)
  1030. rx_work = ionic_cq_service(&lif->hwstamp_rxq->cq, budget,
  1031. ionic_rx_service, NULL, NULL);
  1032. if (lif->hwstamp_txq)
  1033. tx_work = ionic_tx_cq_service(&lif->hwstamp_txq->cq, budget, !!budget);
  1034. work_done = max(max(n_work, a_work), max(rx_work, tx_work));
  1035. if (work_done < budget && napi_complete_done(napi, work_done)) {
  1036. flags |= IONIC_INTR_CRED_UNMASK;
  1037. intr->rearm_count++;
  1038. }
  1039. if (work_done || flags) {
  1040. flags |= IONIC_INTR_CRED_RESET_COALESCE;
  1041. credits = n_work + a_work + rx_work + tx_work;
  1042. ionic_intr_credits(idev->intr_ctrl, intr->index, credits, flags);
  1043. }
  1044. if (lif->doorbell_wa) {
  1045. if (!a_work)
  1046. ionic_adminq_poke_doorbell(&lif->adminqcq->q);
  1047. if (lif->hwstamp_rxq && !rx_work)
  1048. ionic_rxq_poke_doorbell(&lif->hwstamp_rxq->q);
  1049. if (lif->hwstamp_txq && !tx_work)
  1050. ionic_txq_poke_doorbell(&lif->hwstamp_txq->q);
  1051. }
  1052. return work_done;
  1053. }
  1054. void ionic_get_stats64(struct net_device *netdev,
  1055. struct rtnl_link_stats64 *ns)
  1056. {
  1057. struct ionic_lif *lif = netdev_priv(netdev);
  1058. struct ionic_lif_stats *ls;
  1059. memset(ns, 0, sizeof(*ns));
  1060. ls = &lif->info->stats;
  1061. ns->rx_packets = le64_to_cpu(ls->rx_ucast_packets) +
  1062. le64_to_cpu(ls->rx_mcast_packets) +
  1063. le64_to_cpu(ls->rx_bcast_packets);
  1064. ns->tx_packets = le64_to_cpu(ls->tx_ucast_packets) +
  1065. le64_to_cpu(ls->tx_mcast_packets) +
  1066. le64_to_cpu(ls->tx_bcast_packets);
  1067. ns->rx_bytes = le64_to_cpu(ls->rx_ucast_bytes) +
  1068. le64_to_cpu(ls->rx_mcast_bytes) +
  1069. le64_to_cpu(ls->rx_bcast_bytes);
  1070. ns->tx_bytes = le64_to_cpu(ls->tx_ucast_bytes) +
  1071. le64_to_cpu(ls->tx_mcast_bytes) +
  1072. le64_to_cpu(ls->tx_bcast_bytes);
  1073. ns->rx_dropped = le64_to_cpu(ls->rx_ucast_drop_packets) +
  1074. le64_to_cpu(ls->rx_mcast_drop_packets) +
  1075. le64_to_cpu(ls->rx_bcast_drop_packets);
  1076. ns->tx_dropped = le64_to_cpu(ls->tx_ucast_drop_packets) +
  1077. le64_to_cpu(ls->tx_mcast_drop_packets) +
  1078. le64_to_cpu(ls->tx_bcast_drop_packets);
  1079. ns->multicast = le64_to_cpu(ls->rx_mcast_packets);
  1080. ns->rx_over_errors = le64_to_cpu(ls->rx_queue_empty);
  1081. ns->rx_missed_errors = le64_to_cpu(ls->rx_dma_error) +
  1082. le64_to_cpu(ls->rx_queue_disabled) +
  1083. le64_to_cpu(ls->rx_desc_fetch_error) +
  1084. le64_to_cpu(ls->rx_desc_data_error);
  1085. ns->tx_aborted_errors = le64_to_cpu(ls->tx_dma_error) +
  1086. le64_to_cpu(ls->tx_queue_disabled) +
  1087. le64_to_cpu(ls->tx_desc_fetch_error) +
  1088. le64_to_cpu(ls->tx_desc_data_error);
  1089. ns->rx_errors = ns->rx_over_errors +
  1090. ns->rx_missed_errors;
  1091. ns->tx_errors = ns->tx_aborted_errors;
  1092. }
  1093. static int ionic_addr_add(struct net_device *netdev, const u8 *addr)
  1094. {
  1095. return ionic_lif_list_addr(netdev_priv(netdev), addr, ADD_ADDR);
  1096. }
  1097. static int ionic_addr_del(struct net_device *netdev, const u8 *addr)
  1098. {
  1099. /* Don't delete our own address from the uc list */
  1100. if (ether_addr_equal(addr, netdev->dev_addr))
  1101. return 0;
  1102. return ionic_lif_list_addr(netdev_priv(netdev), addr, DEL_ADDR);
  1103. }
  1104. void ionic_lif_rx_mode(struct ionic_lif *lif)
  1105. {
  1106. struct net_device *netdev = lif->netdev;
  1107. unsigned int nfilters;
  1108. unsigned int nd_flags;
  1109. char buf[128];
  1110. u16 rx_mode;
  1111. int i;
  1112. #define REMAIN(__x) (sizeof(buf) - (__x))
  1113. mutex_lock(&lif->config_lock);
  1114. /* grab the flags once for local use */
  1115. nd_flags = netdev->flags;
  1116. rx_mode = IONIC_RX_MODE_F_UNICAST;
  1117. rx_mode |= (nd_flags & IFF_MULTICAST) ? IONIC_RX_MODE_F_MULTICAST : 0;
  1118. rx_mode |= (nd_flags & IFF_BROADCAST) ? IONIC_RX_MODE_F_BROADCAST : 0;
  1119. rx_mode |= (nd_flags & IFF_PROMISC) ? IONIC_RX_MODE_F_PROMISC : 0;
  1120. rx_mode |= (nd_flags & IFF_ALLMULTI) ? IONIC_RX_MODE_F_ALLMULTI : 0;
  1121. /* sync the filters */
  1122. ionic_rx_filter_sync(lif);
  1123. /* check for overflow state
  1124. * if so, we track that we overflowed and enable NIC PROMISC
  1125. * else if the overflow is set and not needed
  1126. * we remove our overflow flag and check the netdev flags
  1127. * to see if we can disable NIC PROMISC
  1128. */
  1129. nfilters = le32_to_cpu(lif->identity->eth.max_ucast_filters);
  1130. if (((lif->nucast + lif->nmcast) >= nfilters) ||
  1131. (lif->max_vlans && lif->nvlans >= lif->max_vlans)) {
  1132. rx_mode |= IONIC_RX_MODE_F_PROMISC;
  1133. rx_mode |= IONIC_RX_MODE_F_ALLMULTI;
  1134. } else {
  1135. if (!(nd_flags & IFF_PROMISC))
  1136. rx_mode &= ~IONIC_RX_MODE_F_PROMISC;
  1137. if (!(nd_flags & IFF_ALLMULTI))
  1138. rx_mode &= ~IONIC_RX_MODE_F_ALLMULTI;
  1139. }
  1140. i = scnprintf(buf, sizeof(buf), "rx_mode 0x%04x -> 0x%04x:",
  1141. lif->rx_mode, rx_mode);
  1142. if (rx_mode & IONIC_RX_MODE_F_UNICAST)
  1143. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_UNICAST");
  1144. if (rx_mode & IONIC_RX_MODE_F_MULTICAST)
  1145. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_MULTICAST");
  1146. if (rx_mode & IONIC_RX_MODE_F_BROADCAST)
  1147. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_BROADCAST");
  1148. if (rx_mode & IONIC_RX_MODE_F_PROMISC)
  1149. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_PROMISC");
  1150. if (rx_mode & IONIC_RX_MODE_F_ALLMULTI)
  1151. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_ALLMULTI");
  1152. if (rx_mode & IONIC_RX_MODE_F_RDMA_SNIFFER)
  1153. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_RDMA_SNIFFER");
  1154. netdev_dbg(netdev, "lif%d %s\n", lif->index, buf);
  1155. if (lif->rx_mode != rx_mode) {
  1156. struct ionic_admin_ctx ctx = {
  1157. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1158. .cmd.rx_mode_set = {
  1159. .opcode = IONIC_CMD_RX_MODE_SET,
  1160. .lif_index = cpu_to_le16(lif->index),
  1161. },
  1162. };
  1163. int err;
  1164. ctx.cmd.rx_mode_set.rx_mode = cpu_to_le16(rx_mode);
  1165. err = ionic_adminq_post_wait(lif, &ctx);
  1166. if (err)
  1167. netdev_warn(netdev, "set rx_mode 0x%04x failed: %d\n",
  1168. rx_mode, err);
  1169. else
  1170. lif->rx_mode = rx_mode;
  1171. }
  1172. mutex_unlock(&lif->config_lock);
  1173. }
  1174. static void ionic_ndo_set_rx_mode(struct net_device *netdev)
  1175. {
  1176. struct ionic_lif *lif = netdev_priv(netdev);
  1177. struct ionic_deferred_work *work;
  1178. /* Sync the kernel filter list with the driver filter list */
  1179. __dev_uc_sync(netdev, ionic_addr_add, ionic_addr_del);
  1180. __dev_mc_sync(netdev, ionic_addr_add, ionic_addr_del);
  1181. /* Shove off the rest of the rxmode work to the work task
  1182. * which will include syncing the filters to the firmware.
  1183. */
  1184. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  1185. if (!work) {
  1186. netdev_err(lif->netdev, "rxmode change dropped\n");
  1187. return;
  1188. }
  1189. work->type = IONIC_DW_TYPE_RX_MODE;
  1190. netdev_dbg(lif->netdev, "deferred: rx_mode\n");
  1191. ionic_lif_deferred_enqueue(lif, work);
  1192. }
  1193. static __le64 ionic_netdev_features_to_nic(netdev_features_t features)
  1194. {
  1195. u64 wanted = 0;
  1196. if (features & NETIF_F_HW_VLAN_CTAG_TX)
  1197. wanted |= IONIC_ETH_HW_VLAN_TX_TAG;
  1198. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1199. wanted |= IONIC_ETH_HW_VLAN_RX_STRIP;
  1200. if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
  1201. wanted |= IONIC_ETH_HW_VLAN_RX_FILTER;
  1202. if (features & NETIF_F_RXHASH)
  1203. wanted |= IONIC_ETH_HW_RX_HASH;
  1204. if (features & NETIF_F_RXCSUM)
  1205. wanted |= IONIC_ETH_HW_RX_CSUM;
  1206. if (features & NETIF_F_SG)
  1207. wanted |= IONIC_ETH_HW_TX_SG;
  1208. if (features & NETIF_F_HW_CSUM)
  1209. wanted |= IONIC_ETH_HW_TX_CSUM;
  1210. if (features & NETIF_F_TSO)
  1211. wanted |= IONIC_ETH_HW_TSO;
  1212. if (features & NETIF_F_TSO6)
  1213. wanted |= IONIC_ETH_HW_TSO_IPV6;
  1214. if (features & NETIF_F_TSO_ECN)
  1215. wanted |= IONIC_ETH_HW_TSO_ECN;
  1216. if (features & NETIF_F_GSO_GRE)
  1217. wanted |= IONIC_ETH_HW_TSO_GRE;
  1218. if (features & NETIF_F_GSO_GRE_CSUM)
  1219. wanted |= IONIC_ETH_HW_TSO_GRE_CSUM;
  1220. if (features & NETIF_F_GSO_IPXIP4)
  1221. wanted |= IONIC_ETH_HW_TSO_IPXIP4;
  1222. if (features & NETIF_F_GSO_IPXIP6)
  1223. wanted |= IONIC_ETH_HW_TSO_IPXIP6;
  1224. if (features & NETIF_F_GSO_UDP_TUNNEL)
  1225. wanted |= IONIC_ETH_HW_TSO_UDP;
  1226. if (features & NETIF_F_GSO_UDP_TUNNEL_CSUM)
  1227. wanted |= IONIC_ETH_HW_TSO_UDP_CSUM;
  1228. return cpu_to_le64(wanted);
  1229. }
  1230. static int ionic_set_nic_features(struct ionic_lif *lif,
  1231. netdev_features_t features)
  1232. {
  1233. struct device *dev = lif->ionic->dev;
  1234. struct ionic_admin_ctx ctx = {
  1235. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1236. .cmd.lif_setattr = {
  1237. .opcode = IONIC_CMD_LIF_SETATTR,
  1238. .index = cpu_to_le16(lif->index),
  1239. .attr = IONIC_LIF_ATTR_FEATURES,
  1240. },
  1241. };
  1242. u64 vlan_flags = IONIC_ETH_HW_VLAN_TX_TAG |
  1243. IONIC_ETH_HW_VLAN_RX_STRIP |
  1244. IONIC_ETH_HW_VLAN_RX_FILTER;
  1245. u64 old_hw_features;
  1246. int err;
  1247. ctx.cmd.lif_setattr.features = ionic_netdev_features_to_nic(features);
  1248. if (lif->phc)
  1249. ctx.cmd.lif_setattr.features |= cpu_to_le64(IONIC_ETH_HW_TIMESTAMP);
  1250. err = ionic_adminq_post_wait(lif, &ctx);
  1251. if (err)
  1252. return err;
  1253. old_hw_features = lif->hw_features;
  1254. lif->hw_features = le64_to_cpu(ctx.cmd.lif_setattr.features &
  1255. ctx.comp.lif_setattr.features);
  1256. if ((old_hw_features ^ lif->hw_features) & IONIC_ETH_HW_RX_HASH)
  1257. ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);
  1258. if ((vlan_flags & le64_to_cpu(ctx.cmd.lif_setattr.features)) &&
  1259. !(vlan_flags & le64_to_cpu(ctx.comp.lif_setattr.features)))
  1260. dev_info_once(lif->ionic->dev, "NIC is not supporting vlan offload, likely in SmartNIC mode\n");
  1261. if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
  1262. dev_dbg(dev, "feature ETH_HW_VLAN_TX_TAG\n");
  1263. if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
  1264. dev_dbg(dev, "feature ETH_HW_VLAN_RX_STRIP\n");
  1265. if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
  1266. dev_dbg(dev, "feature ETH_HW_VLAN_RX_FILTER\n");
  1267. if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
  1268. dev_dbg(dev, "feature ETH_HW_RX_HASH\n");
  1269. if (lif->hw_features & IONIC_ETH_HW_TX_SG)
  1270. dev_dbg(dev, "feature ETH_HW_TX_SG\n");
  1271. if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
  1272. dev_dbg(dev, "feature ETH_HW_TX_CSUM\n");
  1273. if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
  1274. dev_dbg(dev, "feature ETH_HW_RX_CSUM\n");
  1275. if (lif->hw_features & IONIC_ETH_HW_TSO)
  1276. dev_dbg(dev, "feature ETH_HW_TSO\n");
  1277. if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
  1278. dev_dbg(dev, "feature ETH_HW_TSO_IPV6\n");
  1279. if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
  1280. dev_dbg(dev, "feature ETH_HW_TSO_ECN\n");
  1281. if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
  1282. dev_dbg(dev, "feature ETH_HW_TSO_GRE\n");
  1283. if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
  1284. dev_dbg(dev, "feature ETH_HW_TSO_GRE_CSUM\n");
  1285. if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
  1286. dev_dbg(dev, "feature ETH_HW_TSO_IPXIP4\n");
  1287. if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
  1288. dev_dbg(dev, "feature ETH_HW_TSO_IPXIP6\n");
  1289. if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
  1290. dev_dbg(dev, "feature ETH_HW_TSO_UDP\n");
  1291. if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
  1292. dev_dbg(dev, "feature ETH_HW_TSO_UDP_CSUM\n");
  1293. if (lif->hw_features & IONIC_ETH_HW_TIMESTAMP)
  1294. dev_dbg(dev, "feature ETH_HW_TIMESTAMP\n");
  1295. return 0;
  1296. }
  1297. static int ionic_init_nic_features(struct ionic_lif *lif)
  1298. {
  1299. struct net_device *netdev = lif->netdev;
  1300. netdev_features_t features;
  1301. int err;
  1302. /* set up what we expect to support by default */
  1303. features = NETIF_F_HW_VLAN_CTAG_TX |
  1304. NETIF_F_HW_VLAN_CTAG_RX |
  1305. NETIF_F_HW_VLAN_CTAG_FILTER |
  1306. NETIF_F_SG |
  1307. NETIF_F_HW_CSUM |
  1308. NETIF_F_RXCSUM |
  1309. NETIF_F_TSO |
  1310. NETIF_F_TSO6 |
  1311. NETIF_F_TSO_ECN |
  1312. NETIF_F_GSO_GRE |
  1313. NETIF_F_GSO_GRE_CSUM |
  1314. NETIF_F_GSO_IPXIP4 |
  1315. NETIF_F_GSO_IPXIP6 |
  1316. NETIF_F_GSO_UDP_TUNNEL |
  1317. NETIF_F_GSO_UDP_TUNNEL_CSUM;
  1318. if (lif->nxqs > 1)
  1319. features |= NETIF_F_RXHASH;
  1320. err = ionic_set_nic_features(lif, features);
  1321. if (err)
  1322. return err;
  1323. /* tell the netdev what we actually can support */
  1324. netdev->features |= NETIF_F_HIGHDMA;
  1325. if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
  1326. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
  1327. if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
  1328. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
  1329. if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
  1330. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1331. if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
  1332. netdev->hw_features |= NETIF_F_RXHASH;
  1333. if (lif->hw_features & IONIC_ETH_HW_TX_SG)
  1334. netdev->hw_features |= NETIF_F_SG;
  1335. if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
  1336. netdev->hw_enc_features |= NETIF_F_HW_CSUM;
  1337. if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
  1338. netdev->hw_enc_features |= NETIF_F_RXCSUM;
  1339. if (lif->hw_features & IONIC_ETH_HW_TSO)
  1340. netdev->hw_enc_features |= NETIF_F_TSO;
  1341. if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
  1342. netdev->hw_enc_features |= NETIF_F_TSO6;
  1343. if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
  1344. netdev->hw_enc_features |= NETIF_F_TSO_ECN;
  1345. if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
  1346. netdev->hw_enc_features |= NETIF_F_GSO_GRE;
  1347. if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
  1348. netdev->hw_enc_features |= NETIF_F_GSO_GRE_CSUM;
  1349. if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
  1350. netdev->hw_enc_features |= NETIF_F_GSO_IPXIP4;
  1351. if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
  1352. netdev->hw_enc_features |= NETIF_F_GSO_IPXIP6;
  1353. if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
  1354. netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
  1355. if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
  1356. netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  1357. netdev->hw_features |= netdev->hw_enc_features;
  1358. netdev->features |= netdev->hw_features;
  1359. netdev->vlan_features |= netdev->features & ~NETIF_F_VLAN_FEATURES;
  1360. netdev->priv_flags |= IFF_UNICAST_FLT |
  1361. IFF_LIVE_ADDR_CHANGE;
  1362. netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
  1363. NETDEV_XDP_ACT_REDIRECT |
  1364. NETDEV_XDP_ACT_RX_SG |
  1365. NETDEV_XDP_ACT_NDO_XMIT |
  1366. NETDEV_XDP_ACT_NDO_XMIT_SG;
  1367. return 0;
  1368. }
  1369. static int ionic_set_features(struct net_device *netdev,
  1370. netdev_features_t features)
  1371. {
  1372. struct ionic_lif *lif = netdev_priv(netdev);
  1373. int err;
  1374. netdev_dbg(netdev, "%s: lif->features=0x%08llx new_features=0x%08llx\n",
  1375. __func__, (u64)lif->netdev->features, (u64)features);
  1376. err = ionic_set_nic_features(lif, features);
  1377. return err;
  1378. }
  1379. static int ionic_set_attr_mac(struct ionic_lif *lif, u8 *mac)
  1380. {
  1381. struct ionic_admin_ctx ctx = {
  1382. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1383. .cmd.lif_setattr = {
  1384. .opcode = IONIC_CMD_LIF_SETATTR,
  1385. .index = cpu_to_le16(lif->index),
  1386. .attr = IONIC_LIF_ATTR_MAC,
  1387. },
  1388. };
  1389. ether_addr_copy(ctx.cmd.lif_setattr.mac, mac);
  1390. return ionic_adminq_post_wait(lif, &ctx);
  1391. }
  1392. static int ionic_get_attr_mac(struct ionic_lif *lif, u8 *mac_addr)
  1393. {
  1394. struct ionic_admin_ctx ctx = {
  1395. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1396. .cmd.lif_getattr = {
  1397. .opcode = IONIC_CMD_LIF_GETATTR,
  1398. .index = cpu_to_le16(lif->index),
  1399. .attr = IONIC_LIF_ATTR_MAC,
  1400. },
  1401. };
  1402. int err;
  1403. err = ionic_adminq_post_wait(lif, &ctx);
  1404. if (err)
  1405. return err;
  1406. ether_addr_copy(mac_addr, ctx.comp.lif_getattr.mac);
  1407. return 0;
  1408. }
  1409. static int ionic_program_mac(struct ionic_lif *lif, u8 *mac)
  1410. {
  1411. u8 get_mac[ETH_ALEN];
  1412. int err;
  1413. err = ionic_set_attr_mac(lif, mac);
  1414. if (err)
  1415. return err;
  1416. err = ionic_get_attr_mac(lif, get_mac);
  1417. if (err)
  1418. return err;
  1419. /* To deal with older firmware that silently ignores the set attr mac:
  1420. * doesn't actually change the mac and doesn't return an error, so we
  1421. * do the get attr to verify whether or not the set actually happened
  1422. */
  1423. if (!ether_addr_equal(get_mac, mac))
  1424. return 1;
  1425. return 0;
  1426. }
  1427. static int ionic_set_mac_address(struct net_device *netdev, void *sa)
  1428. {
  1429. struct ionic_lif *lif = netdev_priv(netdev);
  1430. struct sockaddr *addr = sa;
  1431. u8 *mac;
  1432. int err;
  1433. mac = (u8 *)addr->sa_data;
  1434. if (ether_addr_equal(netdev->dev_addr, mac))
  1435. return 0;
  1436. err = ionic_program_mac(lif, mac);
  1437. if (err < 0)
  1438. return err;
  1439. if (err > 0)
  1440. netdev_dbg(netdev, "%s: SET and GET ATTR Mac are not equal-due to old FW running\n",
  1441. __func__);
  1442. err = eth_prepare_mac_addr_change(netdev, addr);
  1443. if (err)
  1444. return err;
  1445. if (!is_zero_ether_addr(netdev->dev_addr)) {
  1446. netdev_info(netdev, "deleting mac addr %pM\n",
  1447. netdev->dev_addr);
  1448. ionic_lif_addr_del(netdev_priv(netdev), netdev->dev_addr);
  1449. }
  1450. eth_commit_mac_addr_change(netdev, addr);
  1451. netdev_info(netdev, "updating mac addr %pM\n", mac);
  1452. return ionic_lif_addr_add(netdev_priv(netdev), mac);
  1453. }
  1454. void ionic_stop_queues_reconfig(struct ionic_lif *lif)
  1455. {
  1456. /* Stop and clean the queues before reconfiguration */
  1457. netif_device_detach(lif->netdev);
  1458. ionic_stop_queues(lif);
  1459. ionic_txrx_deinit(lif);
  1460. }
  1461. static int ionic_start_queues_reconfig(struct ionic_lif *lif)
  1462. {
  1463. int err;
  1464. /* Re-init the queues after reconfiguration */
  1465. /* The only way txrx_init can fail here is if communication
  1466. * with FW is suddenly broken. There's not much we can do
  1467. * at this point - error messages have already been printed,
  1468. * so we can continue on and the user can eventually do a
  1469. * DOWN and UP to try to reset and clear the issue.
  1470. */
  1471. err = ionic_txrx_init(lif);
  1472. ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
  1473. netif_device_attach(lif->netdev);
  1474. return err;
  1475. }
  1476. static bool ionic_xdp_is_valid_mtu(struct ionic_lif *lif, u32 mtu,
  1477. struct bpf_prog *xdp_prog)
  1478. {
  1479. if (!xdp_prog)
  1480. return true;
  1481. if (mtu <= IONIC_XDP_MAX_LINEAR_MTU)
  1482. return true;
  1483. if (xdp_prog->aux && xdp_prog->aux->xdp_has_frags)
  1484. return true;
  1485. return false;
  1486. }
  1487. static int ionic_change_mtu(struct net_device *netdev, int new_mtu)
  1488. {
  1489. struct ionic_lif *lif = netdev_priv(netdev);
  1490. struct ionic_admin_ctx ctx = {
  1491. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1492. .cmd.lif_setattr = {
  1493. .opcode = IONIC_CMD_LIF_SETATTR,
  1494. .index = cpu_to_le16(lif->index),
  1495. .attr = IONIC_LIF_ATTR_MTU,
  1496. .mtu = cpu_to_le32(new_mtu),
  1497. },
  1498. };
  1499. struct bpf_prog *xdp_prog;
  1500. int err;
  1501. xdp_prog = READ_ONCE(lif->xdp_prog);
  1502. if (!ionic_xdp_is_valid_mtu(lif, new_mtu, xdp_prog))
  1503. return -EINVAL;
  1504. err = ionic_adminq_post_wait(lif, &ctx);
  1505. if (err)
  1506. return err;
  1507. /* if we're not running, nothing more to do */
  1508. if (!netif_running(netdev)) {
  1509. WRITE_ONCE(netdev->mtu, new_mtu);
  1510. return 0;
  1511. }
  1512. mutex_lock(&lif->queue_lock);
  1513. ionic_stop_queues_reconfig(lif);
  1514. WRITE_ONCE(netdev->mtu, new_mtu);
  1515. err = ionic_start_queues_reconfig(lif);
  1516. mutex_unlock(&lif->queue_lock);
  1517. return err;
  1518. }
  1519. static void ionic_tx_timeout_work(struct work_struct *ws)
  1520. {
  1521. struct ionic_lif *lif = container_of(ws, struct ionic_lif, tx_timeout_work);
  1522. int err;
  1523. if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  1524. return;
  1525. /* if we were stopped before this scheduled job was launched,
  1526. * don't bother the queues as they are already stopped.
  1527. */
  1528. if (!netif_running(lif->netdev))
  1529. return;
  1530. mutex_lock(&lif->queue_lock);
  1531. ionic_stop_queues_reconfig(lif);
  1532. err = ionic_start_queues_reconfig(lif);
  1533. mutex_unlock(&lif->queue_lock);
  1534. if (err)
  1535. dev_err(lif->ionic->dev, "%s: Restarting queues failed\n", __func__);
  1536. }
  1537. static void ionic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  1538. {
  1539. struct ionic_lif *lif = netdev_priv(netdev);
  1540. netdev_info(lif->netdev, "Tx Timeout triggered - txq %d\n", txqueue);
  1541. schedule_work(&lif->tx_timeout_work);
  1542. }
  1543. static int ionic_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
  1544. u16 vid)
  1545. {
  1546. struct ionic_lif *lif = netdev_priv(netdev);
  1547. int err;
  1548. err = ionic_lif_vlan_add(lif, vid);
  1549. if (err)
  1550. return err;
  1551. ionic_lif_rx_mode(lif);
  1552. return 0;
  1553. }
  1554. static int ionic_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
  1555. u16 vid)
  1556. {
  1557. struct ionic_lif *lif = netdev_priv(netdev);
  1558. int err;
  1559. err = ionic_lif_vlan_del(lif, vid);
  1560. if (err)
  1561. return err;
  1562. ionic_lif_rx_mode(lif);
  1563. return 0;
  1564. }
  1565. int ionic_lif_rss_config(struct ionic_lif *lif, const u16 types,
  1566. const u8 *key, const u32 *indir)
  1567. {
  1568. struct ionic_admin_ctx ctx = {
  1569. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1570. .cmd.lif_setattr = {
  1571. .opcode = IONIC_CMD_LIF_SETATTR,
  1572. .attr = IONIC_LIF_ATTR_RSS,
  1573. .rss.addr = cpu_to_le64(lif->rss_ind_tbl_pa),
  1574. },
  1575. };
  1576. unsigned int i, tbl_sz;
  1577. if (lif->hw_features & IONIC_ETH_HW_RX_HASH) {
  1578. lif->rss_types = types;
  1579. ctx.cmd.lif_setattr.rss.types = cpu_to_le16(types);
  1580. }
  1581. if (key)
  1582. memcpy(lif->rss_hash_key, key, IONIC_RSS_HASH_KEY_SIZE);
  1583. if (indir) {
  1584. tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
  1585. for (i = 0; i < tbl_sz; i++)
  1586. lif->rss_ind_tbl[i] = indir[i];
  1587. }
  1588. memcpy(ctx.cmd.lif_setattr.rss.key, lif->rss_hash_key,
  1589. IONIC_RSS_HASH_KEY_SIZE);
  1590. return ionic_adminq_post_wait(lif, &ctx);
  1591. }
  1592. static int ionic_lif_rss_init(struct ionic_lif *lif)
  1593. {
  1594. unsigned int tbl_sz;
  1595. unsigned int i;
  1596. lif->rss_types = IONIC_RSS_TYPE_IPV4 |
  1597. IONIC_RSS_TYPE_IPV4_TCP |
  1598. IONIC_RSS_TYPE_IPV4_UDP |
  1599. IONIC_RSS_TYPE_IPV6 |
  1600. IONIC_RSS_TYPE_IPV6_TCP |
  1601. IONIC_RSS_TYPE_IPV6_UDP;
  1602. /* Fill indirection table with 'default' values */
  1603. tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
  1604. for (i = 0; i < tbl_sz; i++)
  1605. lif->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, lif->nxqs);
  1606. return ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);
  1607. }
  1608. static void ionic_lif_rss_deinit(struct ionic_lif *lif)
  1609. {
  1610. int tbl_sz;
  1611. tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
  1612. memset(lif->rss_ind_tbl, 0, tbl_sz);
  1613. memset(lif->rss_hash_key, 0, IONIC_RSS_HASH_KEY_SIZE);
  1614. ionic_lif_rss_config(lif, 0x0, NULL, NULL);
  1615. }
  1616. static void ionic_lif_quiesce(struct ionic_lif *lif)
  1617. {
  1618. struct ionic_admin_ctx ctx = {
  1619. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1620. .cmd.lif_setattr = {
  1621. .opcode = IONIC_CMD_LIF_SETATTR,
  1622. .index = cpu_to_le16(lif->index),
  1623. .attr = IONIC_LIF_ATTR_STATE,
  1624. .state = IONIC_LIF_QUIESCE,
  1625. },
  1626. };
  1627. int err;
  1628. err = ionic_adminq_post_wait(lif, &ctx);
  1629. if (err)
  1630. netdev_dbg(lif->netdev, "lif quiesce failed %d\n", err);
  1631. }
  1632. static void ionic_txrx_disable(struct ionic_lif *lif)
  1633. {
  1634. unsigned int i;
  1635. int err = 0;
  1636. if (lif->txqcqs) {
  1637. for (i = 0; i < lif->nxqs; i++)
  1638. err = ionic_qcq_disable(lif, lif->txqcqs[i], err);
  1639. }
  1640. if (lif->hwstamp_txq)
  1641. err = ionic_qcq_disable(lif, lif->hwstamp_txq, err);
  1642. if (lif->rxqcqs) {
  1643. for (i = 0; i < lif->nxqs; i++)
  1644. err = ionic_qcq_disable(lif, lif->rxqcqs[i], err);
  1645. }
  1646. if (lif->hwstamp_rxq)
  1647. err = ionic_qcq_disable(lif, lif->hwstamp_rxq, err);
  1648. ionic_lif_quiesce(lif);
  1649. }
  1650. static void ionic_txrx_deinit(struct ionic_lif *lif)
  1651. {
  1652. unsigned int i;
  1653. if (lif->txqcqs) {
  1654. for (i = 0; i < lif->nxqs && lif->txqcqs[i]; i++) {
  1655. ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
  1656. ionic_tx_flush(&lif->txqcqs[i]->cq);
  1657. ionic_tx_empty(&lif->txqcqs[i]->q);
  1658. }
  1659. }
  1660. if (lif->rxqcqs) {
  1661. for (i = 0; i < lif->nxqs && lif->rxqcqs[i]; i++) {
  1662. ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
  1663. ionic_rx_empty(&lif->rxqcqs[i]->q);
  1664. }
  1665. }
  1666. lif->rx_mode = 0;
  1667. if (lif->hwstamp_txq) {
  1668. ionic_lif_qcq_deinit(lif, lif->hwstamp_txq);
  1669. ionic_tx_flush(&lif->hwstamp_txq->cq);
  1670. ionic_tx_empty(&lif->hwstamp_txq->q);
  1671. }
  1672. if (lif->hwstamp_rxq) {
  1673. ionic_lif_qcq_deinit(lif, lif->hwstamp_rxq);
  1674. ionic_rx_empty(&lif->hwstamp_rxq->q);
  1675. }
  1676. }
  1677. void ionic_txrx_free(struct ionic_lif *lif)
  1678. {
  1679. unsigned int i;
  1680. if (lif->txqcqs) {
  1681. for (i = 0; i < lif->ionic->ntxqs_per_lif && lif->txqcqs[i]; i++) {
  1682. ionic_qcq_free(lif, lif->txqcqs[i]);
  1683. devm_kfree(lif->ionic->dev, lif->txqcqs[i]);
  1684. lif->txqcqs[i] = NULL;
  1685. }
  1686. }
  1687. if (lif->rxqcqs) {
  1688. for (i = 0; i < lif->ionic->nrxqs_per_lif && lif->rxqcqs[i]; i++) {
  1689. ionic_qcq_free(lif, lif->rxqcqs[i]);
  1690. devm_kfree(lif->ionic->dev, lif->rxqcqs[i]);
  1691. lif->rxqcqs[i] = NULL;
  1692. }
  1693. }
  1694. if (lif->hwstamp_txq) {
  1695. ionic_qcq_free(lif, lif->hwstamp_txq);
  1696. devm_kfree(lif->ionic->dev, lif->hwstamp_txq);
  1697. lif->hwstamp_txq = NULL;
  1698. }
  1699. if (lif->hwstamp_rxq) {
  1700. ionic_qcq_free(lif, lif->hwstamp_rxq);
  1701. devm_kfree(lif->ionic->dev, lif->hwstamp_rxq);
  1702. lif->hwstamp_rxq = NULL;
  1703. }
  1704. }
  1705. static int ionic_txrx_alloc(struct ionic_lif *lif)
  1706. {
  1707. unsigned int comp_sz, desc_sz, num_desc, sg_desc_sz;
  1708. unsigned int flags, i;
  1709. int err = 0;
  1710. num_desc = lif->ntxq_descs;
  1711. desc_sz = sizeof(struct ionic_txq_desc);
  1712. comp_sz = sizeof(struct ionic_txq_comp);
  1713. if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
  1714. lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
  1715. sizeof(struct ionic_txq_sg_desc_v1))
  1716. sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
  1717. else
  1718. sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
  1719. flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
  1720. if (test_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state))
  1721. flags |= IONIC_QCQ_F_CMB_RINGS;
  1722. if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
  1723. flags |= IONIC_QCQ_F_INTR;
  1724. for (i = 0; i < lif->nxqs; i++) {
  1725. err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
  1726. num_desc, desc_sz, comp_sz, sg_desc_sz,
  1727. sizeof(struct ionic_tx_desc_info),
  1728. lif->kern_pid, NULL, &lif->txqcqs[i]);
  1729. if (err)
  1730. goto err_out;
  1731. if (flags & IONIC_QCQ_F_INTR) {
  1732. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  1733. lif->txqcqs[i]->intr.index,
  1734. lif->tx_coalesce_hw);
  1735. if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
  1736. lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
  1737. }
  1738. ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
  1739. }
  1740. flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG | IONIC_QCQ_F_INTR;
  1741. if (test_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state))
  1742. flags |= IONIC_QCQ_F_CMB_RINGS;
  1743. num_desc = lif->nrxq_descs;
  1744. desc_sz = sizeof(struct ionic_rxq_desc);
  1745. comp_sz = sizeof(struct ionic_rxq_comp);
  1746. sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
  1747. if (lif->rxq_features & IONIC_Q_F_2X_CQ_DESC)
  1748. comp_sz *= 2;
  1749. for (i = 0; i < lif->nxqs; i++) {
  1750. err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
  1751. num_desc, desc_sz, comp_sz, sg_desc_sz,
  1752. sizeof(struct ionic_rx_desc_info),
  1753. lif->kern_pid, lif->xdp_prog,
  1754. &lif->rxqcqs[i]);
  1755. if (err)
  1756. goto err_out;
  1757. lif->rxqcqs[i]->q.features = lif->rxq_features;
  1758. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  1759. lif->rxqcqs[i]->intr.index,
  1760. lif->rx_coalesce_hw);
  1761. if (test_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state))
  1762. lif->rxqcqs[i]->intr.dim_coal_hw = lif->rx_coalesce_hw;
  1763. if (!test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
  1764. ionic_link_qcq_interrupts(lif->rxqcqs[i],
  1765. lif->txqcqs[i]);
  1766. ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
  1767. }
  1768. return 0;
  1769. err_out:
  1770. ionic_txrx_free(lif);
  1771. return err;
  1772. }
  1773. static int ionic_txrx_init(struct ionic_lif *lif)
  1774. {
  1775. unsigned int i;
  1776. int err;
  1777. for (i = 0; i < lif->nxqs; i++) {
  1778. err = ionic_lif_txq_init(lif, lif->txqcqs[i]);
  1779. if (err)
  1780. goto err_out;
  1781. err = ionic_lif_rxq_init(lif, lif->rxqcqs[i]);
  1782. if (err) {
  1783. ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
  1784. goto err_out;
  1785. }
  1786. }
  1787. if (lif->netdev->features & NETIF_F_RXHASH)
  1788. ionic_lif_rss_init(lif);
  1789. ionic_lif_rx_mode(lif);
  1790. return 0;
  1791. err_out:
  1792. while (i--) {
  1793. ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
  1794. ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
  1795. }
  1796. return err;
  1797. }
  1798. static int ionic_txrx_enable(struct ionic_lif *lif)
  1799. {
  1800. int derr = 0;
  1801. int i, err;
  1802. ionic_xdp_rxqs_prog_update(lif);
  1803. for (i = 0; i < lif->nxqs; i++) {
  1804. if (!(lif->rxqcqs[i] && lif->txqcqs[i])) {
  1805. dev_err(lif->ionic->dev, "%s: bad qcq %d\n", __func__, i);
  1806. err = -ENXIO;
  1807. goto err_out;
  1808. }
  1809. ionic_rx_fill(&lif->rxqcqs[i]->q,
  1810. READ_ONCE(lif->rxqcqs[i]->q.xdp_prog));
  1811. err = ionic_qcq_enable(lif->rxqcqs[i]);
  1812. if (err)
  1813. goto err_out;
  1814. err = ionic_qcq_enable(lif->txqcqs[i]);
  1815. if (err) {
  1816. derr = ionic_qcq_disable(lif, lif->rxqcqs[i], err);
  1817. goto err_out;
  1818. }
  1819. }
  1820. if (lif->hwstamp_rxq) {
  1821. ionic_rx_fill(&lif->hwstamp_rxq->q, NULL);
  1822. err = ionic_qcq_enable(lif->hwstamp_rxq);
  1823. if (err)
  1824. goto err_out_hwstamp_rx;
  1825. }
  1826. if (lif->hwstamp_txq) {
  1827. err = ionic_qcq_enable(lif->hwstamp_txq);
  1828. if (err)
  1829. goto err_out_hwstamp_tx;
  1830. }
  1831. return 0;
  1832. err_out_hwstamp_tx:
  1833. if (lif->hwstamp_rxq)
  1834. derr = ionic_qcq_disable(lif, lif->hwstamp_rxq, derr);
  1835. err_out_hwstamp_rx:
  1836. i = lif->nxqs;
  1837. err_out:
  1838. while (i--) {
  1839. derr = ionic_qcq_disable(lif, lif->txqcqs[i], derr);
  1840. derr = ionic_qcq_disable(lif, lif->rxqcqs[i], derr);
  1841. }
  1842. ionic_xdp_rxqs_prog_update(lif);
  1843. return err;
  1844. }
  1845. static int ionic_start_queues(struct ionic_lif *lif)
  1846. {
  1847. int err;
  1848. if (test_bit(IONIC_LIF_F_BROKEN, lif->state))
  1849. return -EIO;
  1850. if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  1851. return -EBUSY;
  1852. if (test_and_set_bit(IONIC_LIF_F_UP, lif->state))
  1853. return 0;
  1854. err = ionic_txrx_enable(lif);
  1855. if (err) {
  1856. clear_bit(IONIC_LIF_F_UP, lif->state);
  1857. return err;
  1858. }
  1859. netif_tx_wake_all_queues(lif->netdev);
  1860. return 0;
  1861. }
  1862. static int ionic_open(struct net_device *netdev)
  1863. {
  1864. struct ionic_lif *lif = netdev_priv(netdev);
  1865. int err;
  1866. /* If recovering from a broken state, clear the bit and we'll try again */
  1867. if (test_and_clear_bit(IONIC_LIF_F_BROKEN, lif->state))
  1868. netdev_info(netdev, "clearing broken state\n");
  1869. mutex_lock(&lif->queue_lock);
  1870. err = ionic_txrx_alloc(lif);
  1871. if (err)
  1872. goto err_unlock;
  1873. err = ionic_txrx_init(lif);
  1874. if (err)
  1875. goto err_txrx_free;
  1876. err = netif_set_real_num_tx_queues(netdev, lif->nxqs);
  1877. if (err)
  1878. goto err_txrx_deinit;
  1879. err = netif_set_real_num_rx_queues(netdev, lif->nxqs);
  1880. if (err)
  1881. goto err_txrx_deinit;
  1882. /* don't start the queues until we have link */
  1883. if (netif_carrier_ok(netdev)) {
  1884. err = ionic_start_queues(lif);
  1885. if (err)
  1886. goto err_txrx_deinit;
  1887. }
  1888. /* If hardware timestamping is enabled, but the queues were freed by
  1889. * ionic_stop, those need to be reallocated and initialized, too.
  1890. */
  1891. ionic_lif_hwstamp_recreate_queues(lif);
  1892. mutex_unlock(&lif->queue_lock);
  1893. return 0;
  1894. err_txrx_deinit:
  1895. ionic_txrx_deinit(lif);
  1896. err_txrx_free:
  1897. ionic_txrx_free(lif);
  1898. err_unlock:
  1899. mutex_unlock(&lif->queue_lock);
  1900. return err;
  1901. }
  1902. static void ionic_stop_queues(struct ionic_lif *lif)
  1903. {
  1904. if (!test_and_clear_bit(IONIC_LIF_F_UP, lif->state))
  1905. return;
  1906. netif_tx_disable(lif->netdev);
  1907. ionic_txrx_disable(lif);
  1908. }
  1909. static int ionic_stop(struct net_device *netdev)
  1910. {
  1911. struct ionic_lif *lif = netdev_priv(netdev);
  1912. if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  1913. return 0;
  1914. mutex_lock(&lif->queue_lock);
  1915. ionic_stop_queues(lif);
  1916. ionic_txrx_deinit(lif);
  1917. ionic_txrx_free(lif);
  1918. mutex_unlock(&lif->queue_lock);
  1919. return 0;
  1920. }
  1921. static int ionic_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1922. {
  1923. struct ionic_lif *lif = netdev_priv(netdev);
  1924. switch (cmd) {
  1925. case SIOCSHWTSTAMP:
  1926. return ionic_lif_hwstamp_set(lif, ifr);
  1927. case SIOCGHWTSTAMP:
  1928. return ionic_lif_hwstamp_get(lif, ifr);
  1929. default:
  1930. return -EOPNOTSUPP;
  1931. }
  1932. }
  1933. static int ionic_get_vf_config(struct net_device *netdev,
  1934. int vf, struct ifla_vf_info *ivf)
  1935. {
  1936. struct ionic_lif *lif = netdev_priv(netdev);
  1937. struct ionic *ionic = lif->ionic;
  1938. int ret = 0;
  1939. if (!netif_device_present(netdev))
  1940. return -EBUSY;
  1941. down_read(&ionic->vf_op_lock);
  1942. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  1943. ret = -EINVAL;
  1944. } else {
  1945. struct ionic_vf *vfdata = &ionic->vfs[vf];
  1946. ivf->vf = vf;
  1947. ivf->qos = 0;
  1948. ivf->vlan = le16_to_cpu(vfdata->vlanid);
  1949. ivf->spoofchk = vfdata->spoofchk;
  1950. ivf->linkstate = vfdata->linkstate;
  1951. ivf->max_tx_rate = le32_to_cpu(vfdata->maxrate);
  1952. ivf->trusted = vfdata->trusted;
  1953. ether_addr_copy(ivf->mac, vfdata->macaddr);
  1954. }
  1955. up_read(&ionic->vf_op_lock);
  1956. return ret;
  1957. }
  1958. static int ionic_get_vf_stats(struct net_device *netdev, int vf,
  1959. struct ifla_vf_stats *vf_stats)
  1960. {
  1961. struct ionic_lif *lif = netdev_priv(netdev);
  1962. struct ionic *ionic = lif->ionic;
  1963. struct ionic_lif_stats *vs;
  1964. int ret = 0;
  1965. if (!netif_device_present(netdev))
  1966. return -EBUSY;
  1967. down_read(&ionic->vf_op_lock);
  1968. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  1969. ret = -EINVAL;
  1970. } else {
  1971. memset(vf_stats, 0, sizeof(*vf_stats));
  1972. vs = &ionic->vfs[vf].stats;
  1973. vf_stats->rx_packets = le64_to_cpu(vs->rx_ucast_packets);
  1974. vf_stats->tx_packets = le64_to_cpu(vs->tx_ucast_packets);
  1975. vf_stats->rx_bytes = le64_to_cpu(vs->rx_ucast_bytes);
  1976. vf_stats->tx_bytes = le64_to_cpu(vs->tx_ucast_bytes);
  1977. vf_stats->broadcast = le64_to_cpu(vs->rx_bcast_packets);
  1978. vf_stats->multicast = le64_to_cpu(vs->rx_mcast_packets);
  1979. vf_stats->rx_dropped = le64_to_cpu(vs->rx_ucast_drop_packets) +
  1980. le64_to_cpu(vs->rx_mcast_drop_packets) +
  1981. le64_to_cpu(vs->rx_bcast_drop_packets);
  1982. vf_stats->tx_dropped = le64_to_cpu(vs->tx_ucast_drop_packets) +
  1983. le64_to_cpu(vs->tx_mcast_drop_packets) +
  1984. le64_to_cpu(vs->tx_bcast_drop_packets);
  1985. }
  1986. up_read(&ionic->vf_op_lock);
  1987. return ret;
  1988. }
  1989. static int ionic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
  1990. {
  1991. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_MAC };
  1992. struct ionic_lif *lif = netdev_priv(netdev);
  1993. struct ionic *ionic = lif->ionic;
  1994. int ret;
  1995. if (!(is_zero_ether_addr(mac) || is_valid_ether_addr(mac)))
  1996. return -EINVAL;
  1997. if (!netif_device_present(netdev))
  1998. return -EBUSY;
  1999. down_write(&ionic->vf_op_lock);
  2000. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2001. ret = -EINVAL;
  2002. } else {
  2003. ether_addr_copy(vfc.macaddr, mac);
  2004. dev_dbg(ionic->dev, "%s: vf %d macaddr %pM\n",
  2005. __func__, vf, vfc.macaddr);
  2006. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2007. if (!ret)
  2008. ether_addr_copy(ionic->vfs[vf].macaddr, mac);
  2009. }
  2010. up_write(&ionic->vf_op_lock);
  2011. return ret;
  2012. }
  2013. static int ionic_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
  2014. u8 qos, __be16 proto)
  2015. {
  2016. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_VLAN };
  2017. struct ionic_lif *lif = netdev_priv(netdev);
  2018. struct ionic *ionic = lif->ionic;
  2019. int ret;
  2020. /* until someday when we support qos */
  2021. if (qos)
  2022. return -EINVAL;
  2023. if (vlan > 4095)
  2024. return -EINVAL;
  2025. if (proto != htons(ETH_P_8021Q))
  2026. return -EPROTONOSUPPORT;
  2027. if (!netif_device_present(netdev))
  2028. return -EBUSY;
  2029. down_write(&ionic->vf_op_lock);
  2030. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2031. ret = -EINVAL;
  2032. } else {
  2033. vfc.vlanid = cpu_to_le16(vlan);
  2034. dev_dbg(ionic->dev, "%s: vf %d vlan %d\n",
  2035. __func__, vf, le16_to_cpu(vfc.vlanid));
  2036. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2037. if (!ret)
  2038. ionic->vfs[vf].vlanid = cpu_to_le16(vlan);
  2039. }
  2040. up_write(&ionic->vf_op_lock);
  2041. return ret;
  2042. }
  2043. static int ionic_set_vf_rate(struct net_device *netdev, int vf,
  2044. int tx_min, int tx_max)
  2045. {
  2046. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_RATE };
  2047. struct ionic_lif *lif = netdev_priv(netdev);
  2048. struct ionic *ionic = lif->ionic;
  2049. int ret;
  2050. /* setting the min just seems silly */
  2051. if (tx_min)
  2052. return -EINVAL;
  2053. if (!netif_device_present(netdev))
  2054. return -EBUSY;
  2055. down_write(&ionic->vf_op_lock);
  2056. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2057. ret = -EINVAL;
  2058. } else {
  2059. vfc.maxrate = cpu_to_le32(tx_max);
  2060. dev_dbg(ionic->dev, "%s: vf %d maxrate %d\n",
  2061. __func__, vf, le32_to_cpu(vfc.maxrate));
  2062. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2063. if (!ret)
  2064. ionic->vfs[vf].maxrate = cpu_to_le32(tx_max);
  2065. }
  2066. up_write(&ionic->vf_op_lock);
  2067. return ret;
  2068. }
  2069. static int ionic_set_vf_spoofchk(struct net_device *netdev, int vf, bool set)
  2070. {
  2071. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_SPOOFCHK };
  2072. struct ionic_lif *lif = netdev_priv(netdev);
  2073. struct ionic *ionic = lif->ionic;
  2074. int ret;
  2075. if (!netif_device_present(netdev))
  2076. return -EBUSY;
  2077. down_write(&ionic->vf_op_lock);
  2078. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2079. ret = -EINVAL;
  2080. } else {
  2081. vfc.spoofchk = set;
  2082. dev_dbg(ionic->dev, "%s: vf %d spoof %d\n",
  2083. __func__, vf, vfc.spoofchk);
  2084. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2085. if (!ret)
  2086. ionic->vfs[vf].spoofchk = set;
  2087. }
  2088. up_write(&ionic->vf_op_lock);
  2089. return ret;
  2090. }
  2091. static int ionic_set_vf_trust(struct net_device *netdev, int vf, bool set)
  2092. {
  2093. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_TRUST };
  2094. struct ionic_lif *lif = netdev_priv(netdev);
  2095. struct ionic *ionic = lif->ionic;
  2096. int ret;
  2097. if (!netif_device_present(netdev))
  2098. return -EBUSY;
  2099. down_write(&ionic->vf_op_lock);
  2100. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2101. ret = -EINVAL;
  2102. } else {
  2103. vfc.trust = set;
  2104. dev_dbg(ionic->dev, "%s: vf %d trust %d\n",
  2105. __func__, vf, vfc.trust);
  2106. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2107. if (!ret)
  2108. ionic->vfs[vf].trusted = set;
  2109. }
  2110. up_write(&ionic->vf_op_lock);
  2111. return ret;
  2112. }
  2113. static int ionic_set_vf_link_state(struct net_device *netdev, int vf, int set)
  2114. {
  2115. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_LINKSTATE };
  2116. struct ionic_lif *lif = netdev_priv(netdev);
  2117. struct ionic *ionic = lif->ionic;
  2118. u8 vfls;
  2119. int ret;
  2120. switch (set) {
  2121. case IFLA_VF_LINK_STATE_ENABLE:
  2122. vfls = IONIC_VF_LINK_STATUS_UP;
  2123. break;
  2124. case IFLA_VF_LINK_STATE_DISABLE:
  2125. vfls = IONIC_VF_LINK_STATUS_DOWN;
  2126. break;
  2127. case IFLA_VF_LINK_STATE_AUTO:
  2128. vfls = IONIC_VF_LINK_STATUS_AUTO;
  2129. break;
  2130. default:
  2131. return -EINVAL;
  2132. }
  2133. if (!netif_device_present(netdev))
  2134. return -EBUSY;
  2135. down_write(&ionic->vf_op_lock);
  2136. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2137. ret = -EINVAL;
  2138. } else {
  2139. vfc.linkstate = vfls;
  2140. dev_dbg(ionic->dev, "%s: vf %d linkstate %d\n",
  2141. __func__, vf, vfc.linkstate);
  2142. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2143. if (!ret)
  2144. ionic->vfs[vf].linkstate = set;
  2145. }
  2146. up_write(&ionic->vf_op_lock);
  2147. return ret;
  2148. }
  2149. static void ionic_vf_attr_replay(struct ionic_lif *lif)
  2150. {
  2151. struct ionic_vf_setattr_cmd vfc = { };
  2152. struct ionic *ionic = lif->ionic;
  2153. struct ionic_vf *v;
  2154. int i;
  2155. if (!ionic->vfs)
  2156. return;
  2157. down_read(&ionic->vf_op_lock);
  2158. for (i = 0; i < ionic->num_vfs; i++) {
  2159. v = &ionic->vfs[i];
  2160. if (v->stats_pa) {
  2161. vfc.attr = IONIC_VF_ATTR_STATSADDR;
  2162. vfc.stats_pa = cpu_to_le64(v->stats_pa);
  2163. ionic_set_vf_config(ionic, i, &vfc);
  2164. vfc.stats_pa = 0;
  2165. }
  2166. if (!is_zero_ether_addr(v->macaddr)) {
  2167. vfc.attr = IONIC_VF_ATTR_MAC;
  2168. ether_addr_copy(vfc.macaddr, v->macaddr);
  2169. ionic_set_vf_config(ionic, i, &vfc);
  2170. eth_zero_addr(vfc.macaddr);
  2171. }
  2172. if (v->vlanid) {
  2173. vfc.attr = IONIC_VF_ATTR_VLAN;
  2174. vfc.vlanid = v->vlanid;
  2175. ionic_set_vf_config(ionic, i, &vfc);
  2176. vfc.vlanid = 0;
  2177. }
  2178. if (v->maxrate) {
  2179. vfc.attr = IONIC_VF_ATTR_RATE;
  2180. vfc.maxrate = v->maxrate;
  2181. ionic_set_vf_config(ionic, i, &vfc);
  2182. vfc.maxrate = 0;
  2183. }
  2184. if (v->spoofchk) {
  2185. vfc.attr = IONIC_VF_ATTR_SPOOFCHK;
  2186. vfc.spoofchk = v->spoofchk;
  2187. ionic_set_vf_config(ionic, i, &vfc);
  2188. vfc.spoofchk = 0;
  2189. }
  2190. if (v->trusted) {
  2191. vfc.attr = IONIC_VF_ATTR_TRUST;
  2192. vfc.trust = v->trusted;
  2193. ionic_set_vf_config(ionic, i, &vfc);
  2194. vfc.trust = 0;
  2195. }
  2196. if (v->linkstate) {
  2197. vfc.attr = IONIC_VF_ATTR_LINKSTATE;
  2198. vfc.linkstate = v->linkstate;
  2199. ionic_set_vf_config(ionic, i, &vfc);
  2200. vfc.linkstate = 0;
  2201. }
  2202. }
  2203. up_read(&ionic->vf_op_lock);
  2204. ionic_vf_start(ionic);
  2205. }
  2206. static void ionic_unregister_rxq_info(struct ionic_queue *q)
  2207. {
  2208. struct xdp_rxq_info *xi;
  2209. if (!q->xdp_rxq_info)
  2210. return;
  2211. xi = q->xdp_rxq_info;
  2212. q->xdp_rxq_info = NULL;
  2213. xdp_rxq_info_unreg(xi);
  2214. kfree(xi);
  2215. }
  2216. static int ionic_register_rxq_info(struct ionic_queue *q, unsigned int napi_id)
  2217. {
  2218. struct xdp_rxq_info *rxq_info;
  2219. int err;
  2220. rxq_info = kzalloc(sizeof(*rxq_info), GFP_KERNEL);
  2221. if (!rxq_info)
  2222. return -ENOMEM;
  2223. err = xdp_rxq_info_reg(rxq_info, q->lif->netdev, q->index, napi_id);
  2224. if (err) {
  2225. netdev_err(q->lif->netdev, "q%d xdp_rxq_info_reg failed, err %d\n",
  2226. q->index, err);
  2227. goto err_out;
  2228. }
  2229. err = xdp_rxq_info_reg_mem_model(rxq_info, MEM_TYPE_PAGE_POOL, q->page_pool);
  2230. if (err) {
  2231. netdev_err(q->lif->netdev, "q%d xdp_rxq_info_reg_mem_model failed, err %d\n",
  2232. q->index, err);
  2233. xdp_rxq_info_unreg(rxq_info);
  2234. goto err_out;
  2235. }
  2236. q->xdp_rxq_info = rxq_info;
  2237. return 0;
  2238. err_out:
  2239. kfree(rxq_info);
  2240. return err;
  2241. }
  2242. static void ionic_xdp_rxqs_prog_update(struct ionic_lif *lif)
  2243. {
  2244. struct bpf_prog *xdp_prog;
  2245. unsigned int i;
  2246. if (!lif->rxqcqs)
  2247. return;
  2248. xdp_prog = READ_ONCE(lif->xdp_prog);
  2249. for (i = 0; i < lif->ionic->nrxqs_per_lif && lif->rxqcqs[i]; i++) {
  2250. struct ionic_queue *q = &lif->rxqcqs[i]->q;
  2251. WRITE_ONCE(q->xdp_prog, xdp_prog);
  2252. }
  2253. }
  2254. static int ionic_xdp_config(struct net_device *netdev, struct netdev_bpf *bpf)
  2255. {
  2256. struct ionic_lif *lif = netdev_priv(netdev);
  2257. struct bpf_prog *old_prog;
  2258. u32 maxfs;
  2259. if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state)) {
  2260. #define XDP_ERR_SPLIT "XDP not available with split Tx/Rx interrupts"
  2261. NL_SET_ERR_MSG_MOD(bpf->extack, XDP_ERR_SPLIT);
  2262. netdev_info(lif->netdev, XDP_ERR_SPLIT);
  2263. return -EOPNOTSUPP;
  2264. }
  2265. if (!ionic_xdp_is_valid_mtu(lif, netdev->mtu, bpf->prog)) {
  2266. #define XDP_ERR_MTU "MTU is too large for XDP without frags support"
  2267. NL_SET_ERR_MSG_MOD(bpf->extack, XDP_ERR_MTU);
  2268. netdev_info(lif->netdev, XDP_ERR_MTU);
  2269. return -EINVAL;
  2270. }
  2271. maxfs = __le32_to_cpu(lif->identity->eth.max_frame_size) - VLAN_ETH_HLEN;
  2272. if (bpf->prog && !(bpf->prog->aux && bpf->prog->aux->xdp_has_frags))
  2273. maxfs = min_t(u32, maxfs, IONIC_XDP_MAX_LINEAR_MTU);
  2274. netdev->max_mtu = maxfs;
  2275. if (!netif_running(netdev)) {
  2276. old_prog = xchg(&lif->xdp_prog, bpf->prog);
  2277. } else if (lif->xdp_prog && bpf->prog) {
  2278. old_prog = xchg(&lif->xdp_prog, bpf->prog);
  2279. ionic_xdp_rxqs_prog_update(lif);
  2280. } else {
  2281. struct ionic_queue_params qparams;
  2282. ionic_init_queue_params(lif, &qparams);
  2283. qparams.xdp_prog = bpf->prog;
  2284. mutex_lock(&lif->queue_lock);
  2285. ionic_reconfigure_queues(lif, &qparams);
  2286. old_prog = xchg(&lif->xdp_prog, bpf->prog);
  2287. mutex_unlock(&lif->queue_lock);
  2288. }
  2289. if (old_prog)
  2290. bpf_prog_put(old_prog);
  2291. return 0;
  2292. }
  2293. static int ionic_xdp(struct net_device *netdev, struct netdev_bpf *bpf)
  2294. {
  2295. switch (bpf->command) {
  2296. case XDP_SETUP_PROG:
  2297. return ionic_xdp_config(netdev, bpf);
  2298. default:
  2299. return -EINVAL;
  2300. }
  2301. }
  2302. static const struct net_device_ops ionic_netdev_ops = {
  2303. .ndo_open = ionic_open,
  2304. .ndo_stop = ionic_stop,
  2305. .ndo_eth_ioctl = ionic_eth_ioctl,
  2306. .ndo_start_xmit = ionic_start_xmit,
  2307. .ndo_bpf = ionic_xdp,
  2308. .ndo_xdp_xmit = ionic_xdp_xmit,
  2309. .ndo_get_stats64 = ionic_get_stats64,
  2310. .ndo_set_rx_mode = ionic_ndo_set_rx_mode,
  2311. .ndo_set_features = ionic_set_features,
  2312. .ndo_set_mac_address = ionic_set_mac_address,
  2313. .ndo_validate_addr = eth_validate_addr,
  2314. .ndo_tx_timeout = ionic_tx_timeout,
  2315. .ndo_change_mtu = ionic_change_mtu,
  2316. .ndo_vlan_rx_add_vid = ionic_vlan_rx_add_vid,
  2317. .ndo_vlan_rx_kill_vid = ionic_vlan_rx_kill_vid,
  2318. .ndo_set_vf_vlan = ionic_set_vf_vlan,
  2319. .ndo_set_vf_trust = ionic_set_vf_trust,
  2320. .ndo_set_vf_mac = ionic_set_vf_mac,
  2321. .ndo_set_vf_rate = ionic_set_vf_rate,
  2322. .ndo_set_vf_spoofchk = ionic_set_vf_spoofchk,
  2323. .ndo_get_vf_config = ionic_get_vf_config,
  2324. .ndo_set_vf_link_state = ionic_set_vf_link_state,
  2325. .ndo_get_vf_stats = ionic_get_vf_stats,
  2326. };
  2327. static int ionic_cmb_reconfig(struct ionic_lif *lif,
  2328. struct ionic_queue_params *qparam)
  2329. {
  2330. struct ionic_queue_params start_qparams;
  2331. int err = 0;
  2332. /* When changing CMB queue parameters, we're using limited
  2333. * on-device memory and don't have extra memory to use for
  2334. * duplicate allocations, so we free it all first then
  2335. * re-allocate with the new parameters.
  2336. */
  2337. /* Checkpoint for possible unwind */
  2338. ionic_init_queue_params(lif, &start_qparams);
  2339. /* Stop and free the queues */
  2340. ionic_stop_queues_reconfig(lif);
  2341. ionic_txrx_free(lif);
  2342. /* Set up new qparams */
  2343. ionic_set_queue_params(lif, qparam);
  2344. if (netif_running(lif->netdev)) {
  2345. /* Alloc and start the new configuration */
  2346. err = ionic_txrx_alloc(lif);
  2347. if (err) {
  2348. dev_warn(lif->ionic->dev,
  2349. "CMB reconfig failed, restoring values: %d\n", err);
  2350. /* Back out the changes */
  2351. ionic_set_queue_params(lif, &start_qparams);
  2352. err = ionic_txrx_alloc(lif);
  2353. if (err) {
  2354. dev_err(lif->ionic->dev,
  2355. "CMB restore failed: %d\n", err);
  2356. goto err_out;
  2357. }
  2358. }
  2359. err = ionic_start_queues_reconfig(lif);
  2360. if (err) {
  2361. dev_err(lif->ionic->dev,
  2362. "CMB reconfig failed: %d\n", err);
  2363. goto err_out;
  2364. }
  2365. }
  2366. err_out:
  2367. /* This was detached in ionic_stop_queues_reconfig() */
  2368. netif_device_attach(lif->netdev);
  2369. return err;
  2370. }
  2371. static void ionic_swap_queues(struct ionic_qcq *a, struct ionic_qcq *b)
  2372. {
  2373. /* only swapping the queues and napi, not flags or other stuff */
  2374. swap(a->napi, b->napi);
  2375. if (a->q.type == IONIC_QTYPE_RXQ) {
  2376. swap(a->q.page_pool, b->q.page_pool);
  2377. a->q.page_pool->p.napi = &a->napi;
  2378. if (b->q.page_pool) /* is NULL when increasing queue count */
  2379. b->q.page_pool->p.napi = &b->napi;
  2380. }
  2381. swap(a->q.features, b->q.features);
  2382. swap(a->q.num_descs, b->q.num_descs);
  2383. swap(a->q.desc_size, b->q.desc_size);
  2384. swap(a->q.base, b->q.base);
  2385. swap(a->q.base_pa, b->q.base_pa);
  2386. swap(a->q.info, b->q.info);
  2387. swap(a->q.xdp_prog, b->q.xdp_prog);
  2388. swap(a->q.xdp_rxq_info, b->q.xdp_rxq_info);
  2389. swap(a->q.partner, b->q.partner);
  2390. swap(a->q_base, b->q_base);
  2391. swap(a->q_base_pa, b->q_base_pa);
  2392. swap(a->q_size, b->q_size);
  2393. swap(a->q.sg_desc_size, b->q.sg_desc_size);
  2394. swap(a->q.sg_base, b->q.sg_base);
  2395. swap(a->q.sg_base_pa, b->q.sg_base_pa);
  2396. swap(a->sg_base, b->sg_base);
  2397. swap(a->sg_base_pa, b->sg_base_pa);
  2398. swap(a->sg_size, b->sg_size);
  2399. swap(a->cq.num_descs, b->cq.num_descs);
  2400. swap(a->cq.desc_size, b->cq.desc_size);
  2401. swap(a->cq.base, b->cq.base);
  2402. swap(a->cq.base_pa, b->cq.base_pa);
  2403. swap(a->cq_base, b->cq_base);
  2404. swap(a->cq_base_pa, b->cq_base_pa);
  2405. swap(a->cq_size, b->cq_size);
  2406. ionic_debugfs_del_qcq(a);
  2407. ionic_debugfs_add_qcq(a->q.lif, a);
  2408. }
  2409. int ionic_reconfigure_queues(struct ionic_lif *lif,
  2410. struct ionic_queue_params *qparam)
  2411. {
  2412. unsigned int comp_sz, desc_sz, num_desc, sg_desc_sz;
  2413. struct ionic_qcq **tx_qcqs = NULL;
  2414. struct ionic_qcq **rx_qcqs = NULL;
  2415. unsigned int flags, i;
  2416. int err = 0;
  2417. /* Are we changing q params while CMB is on */
  2418. if ((test_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state) && qparam->cmb_tx) ||
  2419. (test_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state) && qparam->cmb_rx))
  2420. return ionic_cmb_reconfig(lif, qparam);
  2421. /* allocate temporary qcq arrays to hold new queue structs */
  2422. if (qparam->nxqs != lif->nxqs || qparam->ntxq_descs != lif->ntxq_descs) {
  2423. tx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->ntxqs_per_lif,
  2424. sizeof(struct ionic_qcq *), GFP_KERNEL);
  2425. if (!tx_qcqs) {
  2426. err = -ENOMEM;
  2427. goto err_out;
  2428. }
  2429. }
  2430. if (qparam->nxqs != lif->nxqs ||
  2431. qparam->nrxq_descs != lif->nrxq_descs ||
  2432. qparam->rxq_features != lif->rxq_features ||
  2433. qparam->xdp_prog != lif->xdp_prog) {
  2434. rx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->nrxqs_per_lif,
  2435. sizeof(struct ionic_qcq *), GFP_KERNEL);
  2436. if (!rx_qcqs) {
  2437. err = -ENOMEM;
  2438. goto err_out;
  2439. }
  2440. }
  2441. /* allocate new desc_info and rings, but leave the interrupt setup
  2442. * until later so as to not mess with the still-running queues
  2443. */
  2444. if (tx_qcqs) {
  2445. num_desc = qparam->ntxq_descs;
  2446. desc_sz = sizeof(struct ionic_txq_desc);
  2447. comp_sz = sizeof(struct ionic_txq_comp);
  2448. if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
  2449. lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
  2450. sizeof(struct ionic_txq_sg_desc_v1))
  2451. sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
  2452. else
  2453. sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
  2454. for (i = 0; i < qparam->nxqs; i++) {
  2455. /* If missing, short placeholder qcq needed for swap */
  2456. if (!lif->txqcqs[i]) {
  2457. flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
  2458. err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
  2459. 4, desc_sz, comp_sz, sg_desc_sz,
  2460. sizeof(struct ionic_tx_desc_info),
  2461. lif->kern_pid, NULL, &lif->txqcqs[i]);
  2462. if (err)
  2463. goto err_out;
  2464. }
  2465. flags = lif->txqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
  2466. err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
  2467. num_desc, desc_sz, comp_sz, sg_desc_sz,
  2468. sizeof(struct ionic_tx_desc_info),
  2469. lif->kern_pid, NULL, &tx_qcqs[i]);
  2470. if (err)
  2471. goto err_out;
  2472. }
  2473. }
  2474. if (rx_qcqs) {
  2475. num_desc = qparam->nrxq_descs;
  2476. desc_sz = sizeof(struct ionic_rxq_desc);
  2477. comp_sz = sizeof(struct ionic_rxq_comp);
  2478. sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
  2479. if (qparam->rxq_features & IONIC_Q_F_2X_CQ_DESC)
  2480. comp_sz *= 2;
  2481. for (i = 0; i < qparam->nxqs; i++) {
  2482. /* If missing, short placeholder qcq needed for swap */
  2483. if (!lif->rxqcqs[i]) {
  2484. flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG;
  2485. err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
  2486. 4, desc_sz, comp_sz, sg_desc_sz,
  2487. sizeof(struct ionic_rx_desc_info),
  2488. lif->kern_pid, NULL, &lif->rxqcqs[i]);
  2489. if (err)
  2490. goto err_out;
  2491. }
  2492. flags = lif->rxqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
  2493. err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
  2494. num_desc, desc_sz, comp_sz, sg_desc_sz,
  2495. sizeof(struct ionic_rx_desc_info),
  2496. lif->kern_pid, qparam->xdp_prog, &rx_qcqs[i]);
  2497. if (err)
  2498. goto err_out;
  2499. rx_qcqs[i]->q.features = qparam->rxq_features;
  2500. rx_qcqs[i]->q.xdp_prog = qparam->xdp_prog;
  2501. }
  2502. }
  2503. /* stop and clean the queues */
  2504. ionic_stop_queues_reconfig(lif);
  2505. if (qparam->nxqs != lif->nxqs) {
  2506. err = netif_set_real_num_tx_queues(lif->netdev, qparam->nxqs);
  2507. if (err)
  2508. goto err_out_reinit_unlock;
  2509. err = netif_set_real_num_rx_queues(lif->netdev, qparam->nxqs);
  2510. if (err) {
  2511. netif_set_real_num_tx_queues(lif->netdev, lif->nxqs);
  2512. goto err_out_reinit_unlock;
  2513. }
  2514. }
  2515. /* swap new desc_info and rings, keeping existing interrupt config */
  2516. if (tx_qcqs) {
  2517. lif->ntxq_descs = qparam->ntxq_descs;
  2518. for (i = 0; i < qparam->nxqs; i++)
  2519. ionic_swap_queues(lif->txqcqs[i], tx_qcqs[i]);
  2520. }
  2521. if (rx_qcqs) {
  2522. lif->nrxq_descs = qparam->nrxq_descs;
  2523. for (i = 0; i < qparam->nxqs; i++)
  2524. ionic_swap_queues(lif->rxqcqs[i], rx_qcqs[i]);
  2525. }
  2526. /* if we need to change the interrupt layout, this is the time */
  2527. if (qparam->intr_split != test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state) ||
  2528. qparam->nxqs != lif->nxqs) {
  2529. if (qparam->intr_split) {
  2530. set_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
  2531. } else {
  2532. clear_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
  2533. lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
  2534. lif->tx_coalesce_hw = lif->rx_coalesce_hw;
  2535. }
  2536. /* Clear existing interrupt assignments. We check for NULL here
  2537. * because we're checking the whole array for potential qcqs, not
  2538. * just those qcqs that have just been set up.
  2539. */
  2540. for (i = 0; i < lif->ionic->ntxqs_per_lif; i++) {
  2541. if (lif->txqcqs[i])
  2542. ionic_qcq_intr_free(lif, lif->txqcqs[i]);
  2543. if (lif->rxqcqs[i])
  2544. ionic_qcq_intr_free(lif, lif->rxqcqs[i]);
  2545. }
  2546. /* re-assign the interrupts */
  2547. for (i = 0; i < qparam->nxqs; i++) {
  2548. lif->rxqcqs[i]->flags |= IONIC_QCQ_F_INTR;
  2549. err = ionic_alloc_qcq_interrupt(lif, lif->rxqcqs[i]);
  2550. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  2551. lif->rxqcqs[i]->intr.index,
  2552. lif->rx_coalesce_hw);
  2553. if (qparam->intr_split) {
  2554. lif->txqcqs[i]->flags |= IONIC_QCQ_F_INTR;
  2555. err = ionic_alloc_qcq_interrupt(lif, lif->txqcqs[i]);
  2556. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  2557. lif->txqcqs[i]->intr.index,
  2558. lif->tx_coalesce_hw);
  2559. if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
  2560. lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
  2561. } else {
  2562. lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2563. ionic_link_qcq_interrupts(lif->rxqcqs[i], lif->txqcqs[i]);
  2564. }
  2565. }
  2566. }
  2567. /* now we can rework the debugfs mappings */
  2568. if (tx_qcqs) {
  2569. for (i = 0; i < qparam->nxqs; i++) {
  2570. ionic_debugfs_del_qcq(lif->txqcqs[i]);
  2571. ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
  2572. }
  2573. }
  2574. if (rx_qcqs) {
  2575. for (i = 0; i < qparam->nxqs; i++) {
  2576. ionic_debugfs_del_qcq(lif->rxqcqs[i]);
  2577. ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
  2578. }
  2579. }
  2580. swap(lif->nxqs, qparam->nxqs);
  2581. swap(lif->rxq_features, qparam->rxq_features);
  2582. err_out_reinit_unlock:
  2583. /* re-init the queues, but don't lose an error code */
  2584. if (err)
  2585. ionic_start_queues_reconfig(lif);
  2586. else
  2587. err = ionic_start_queues_reconfig(lif);
  2588. err_out:
  2589. /* free old allocs without cleaning intr */
  2590. for (i = 0; i < qparam->nxqs; i++) {
  2591. if (tx_qcqs && tx_qcqs[i]) {
  2592. tx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2593. ionic_qcq_free(lif, tx_qcqs[i]);
  2594. devm_kfree(lif->ionic->dev, tx_qcqs[i]);
  2595. tx_qcqs[i] = NULL;
  2596. }
  2597. if (rx_qcqs && rx_qcqs[i]) {
  2598. rx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2599. ionic_qcq_free(lif, rx_qcqs[i]);
  2600. devm_kfree(lif->ionic->dev, rx_qcqs[i]);
  2601. rx_qcqs[i] = NULL;
  2602. }
  2603. }
  2604. /* free q array */
  2605. if (rx_qcqs) {
  2606. devm_kfree(lif->ionic->dev, rx_qcqs);
  2607. rx_qcqs = NULL;
  2608. }
  2609. if (tx_qcqs) {
  2610. devm_kfree(lif->ionic->dev, tx_qcqs);
  2611. tx_qcqs = NULL;
  2612. }
  2613. /* clean the unused dma and info allocations when new set is smaller
  2614. * than the full array, but leave the qcq shells in place
  2615. */
  2616. for (i = lif->nxqs; i < lif->ionic->ntxqs_per_lif; i++) {
  2617. if (lif->txqcqs && lif->txqcqs[i]) {
  2618. lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2619. ionic_qcq_free(lif, lif->txqcqs[i]);
  2620. }
  2621. if (lif->rxqcqs && lif->rxqcqs[i]) {
  2622. lif->rxqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2623. ionic_qcq_free(lif, lif->rxqcqs[i]);
  2624. }
  2625. }
  2626. if (err)
  2627. netdev_info(lif->netdev, "%s: failed %d\n", __func__, err);
  2628. return err;
  2629. }
  2630. static int ionic_affinity_masks_alloc(struct ionic *ionic)
  2631. {
  2632. cpumask_var_t *affinity_masks;
  2633. int nintrs = ionic->nintrs;
  2634. int i;
  2635. affinity_masks = kcalloc(nintrs, sizeof(cpumask_var_t), GFP_KERNEL);
  2636. if (!affinity_masks)
  2637. return -ENOMEM;
  2638. for (i = 0; i < nintrs; i++) {
  2639. if (!zalloc_cpumask_var_node(&affinity_masks[i], GFP_KERNEL,
  2640. dev_to_node(ionic->dev)))
  2641. goto err_out;
  2642. }
  2643. ionic->affinity_masks = affinity_masks;
  2644. return 0;
  2645. err_out:
  2646. for (--i; i >= 0; i--)
  2647. free_cpumask_var(affinity_masks[i]);
  2648. kfree(affinity_masks);
  2649. return -ENOMEM;
  2650. }
  2651. static void ionic_affinity_masks_free(struct ionic *ionic)
  2652. {
  2653. int i;
  2654. for (i = 0; i < ionic->nintrs; i++)
  2655. free_cpumask_var(ionic->affinity_masks[i]);
  2656. kfree(ionic->affinity_masks);
  2657. ionic->affinity_masks = NULL;
  2658. }
  2659. int ionic_lif_alloc(struct ionic *ionic)
  2660. {
  2661. struct device *dev = ionic->dev;
  2662. union ionic_lif_identity *lid;
  2663. struct net_device *netdev;
  2664. struct ionic_lif *lif;
  2665. int tbl_sz;
  2666. int err;
  2667. lid = kzalloc(sizeof(*lid), GFP_KERNEL);
  2668. if (!lid)
  2669. return -ENOMEM;
  2670. netdev = alloc_etherdev_mqs(sizeof(*lif),
  2671. ionic->ntxqs_per_lif, ionic->ntxqs_per_lif);
  2672. if (!netdev) {
  2673. dev_err(dev, "Cannot allocate netdev, aborting\n");
  2674. err = -ENOMEM;
  2675. goto err_out_free_lid;
  2676. }
  2677. SET_NETDEV_DEV(netdev, dev);
  2678. lif = netdev_priv(netdev);
  2679. lif->netdev = netdev;
  2680. ionic->lif = lif;
  2681. lif->ionic = ionic;
  2682. netdev->netdev_ops = &ionic_netdev_ops;
  2683. ionic_ethtool_set_ops(netdev);
  2684. netdev->watchdog_timeo = 5 * HZ;
  2685. netif_carrier_off(netdev);
  2686. lif->identity = lid;
  2687. lif->lif_type = IONIC_LIF_TYPE_CLASSIC;
  2688. err = ionic_lif_identify(ionic, lif->lif_type, lif->identity);
  2689. if (err) {
  2690. dev_err(ionic->dev, "Cannot identify type %d: %d\n",
  2691. lif->lif_type, err);
  2692. goto err_out_free_netdev;
  2693. }
  2694. lif->netdev->min_mtu = max_t(unsigned int, ETH_MIN_MTU,
  2695. le32_to_cpu(lif->identity->eth.min_frame_size));
  2696. lif->netdev->max_mtu =
  2697. le32_to_cpu(lif->identity->eth.max_frame_size) - ETH_HLEN - VLAN_HLEN;
  2698. lif->neqs = ionic->neqs_per_lif;
  2699. lif->nxqs = ionic->ntxqs_per_lif;
  2700. lif->index = 0;
  2701. if (is_kdump_kernel()) {
  2702. lif->ntxq_descs = IONIC_MIN_TXRX_DESC;
  2703. lif->nrxq_descs = IONIC_MIN_TXRX_DESC;
  2704. } else {
  2705. lif->ntxq_descs = IONIC_DEF_TXRX_DESC;
  2706. lif->nrxq_descs = IONIC_DEF_TXRX_DESC;
  2707. }
  2708. /* Convert the default coalesce value to actual hw resolution */
  2709. lif->rx_coalesce_usecs = IONIC_ITR_COAL_USEC_DEFAULT;
  2710. lif->rx_coalesce_hw = ionic_coal_usec_to_hw(lif->ionic,
  2711. lif->rx_coalesce_usecs);
  2712. lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
  2713. lif->tx_coalesce_hw = lif->rx_coalesce_hw;
  2714. set_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state);
  2715. set_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state);
  2716. snprintf(lif->name, sizeof(lif->name), "lif%u", lif->index);
  2717. mutex_init(&lif->queue_lock);
  2718. mutex_init(&lif->config_lock);
  2719. spin_lock_init(&lif->adminq_lock);
  2720. spin_lock_init(&lif->deferred.lock);
  2721. INIT_LIST_HEAD(&lif->deferred.list);
  2722. INIT_WORK(&lif->deferred.work, ionic_lif_deferred_work);
  2723. /* allocate lif info */
  2724. lif->info_sz = ALIGN(sizeof(*lif->info), PAGE_SIZE);
  2725. lif->info = dma_alloc_coherent(dev, lif->info_sz,
  2726. &lif->info_pa, GFP_KERNEL);
  2727. if (!lif->info) {
  2728. dev_err(dev, "Failed to allocate lif info, aborting\n");
  2729. err = -ENOMEM;
  2730. goto err_out_free_mutex;
  2731. }
  2732. ionic_debugfs_add_lif(lif);
  2733. err = ionic_affinity_masks_alloc(ionic);
  2734. if (err)
  2735. goto err_out_free_lif_info;
  2736. /* allocate control queues and txrx queue arrays */
  2737. ionic_lif_queue_identify(lif);
  2738. err = ionic_qcqs_alloc(lif);
  2739. if (err)
  2740. goto err_out_free_affinity_masks;
  2741. /* allocate rss indirection table */
  2742. tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
  2743. lif->rss_ind_tbl_sz = sizeof(*lif->rss_ind_tbl) * tbl_sz;
  2744. lif->rss_ind_tbl = dma_alloc_coherent(dev, lif->rss_ind_tbl_sz,
  2745. &lif->rss_ind_tbl_pa,
  2746. GFP_KERNEL);
  2747. if (!lif->rss_ind_tbl) {
  2748. err = -ENOMEM;
  2749. dev_err(dev, "Failed to allocate rss indirection table, aborting\n");
  2750. goto err_out_free_qcqs;
  2751. }
  2752. netdev_rss_key_fill(lif->rss_hash_key, IONIC_RSS_HASH_KEY_SIZE);
  2753. ionic_lif_alloc_phc(lif);
  2754. return 0;
  2755. err_out_free_qcqs:
  2756. ionic_qcqs_free(lif);
  2757. err_out_free_affinity_masks:
  2758. ionic_affinity_masks_free(lif->ionic);
  2759. err_out_free_lif_info:
  2760. dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
  2761. lif->info = NULL;
  2762. lif->info_pa = 0;
  2763. err_out_free_mutex:
  2764. mutex_destroy(&lif->config_lock);
  2765. mutex_destroy(&lif->queue_lock);
  2766. err_out_free_netdev:
  2767. free_netdev(lif->netdev);
  2768. lif = NULL;
  2769. err_out_free_lid:
  2770. kfree(lid);
  2771. return err;
  2772. }
  2773. static void ionic_lif_reset(struct ionic_lif *lif)
  2774. {
  2775. struct ionic_dev *idev = &lif->ionic->idev;
  2776. if (!ionic_is_fw_running(idev))
  2777. return;
  2778. mutex_lock(&lif->ionic->dev_cmd_lock);
  2779. ionic_dev_cmd_lif_reset(idev, lif->index);
  2780. ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
  2781. mutex_unlock(&lif->ionic->dev_cmd_lock);
  2782. }
  2783. static void ionic_lif_handle_fw_down(struct ionic_lif *lif)
  2784. {
  2785. struct ionic *ionic = lif->ionic;
  2786. if (test_and_set_bit(IONIC_LIF_F_FW_RESET, lif->state))
  2787. return;
  2788. dev_info(ionic->dev, "FW Down: Stopping LIFs\n");
  2789. netif_device_detach(lif->netdev);
  2790. mutex_lock(&lif->queue_lock);
  2791. if (test_bit(IONIC_LIF_F_UP, lif->state)) {
  2792. dev_info(ionic->dev, "Surprise FW stop, stopping queues\n");
  2793. ionic_stop_queues(lif);
  2794. }
  2795. if (netif_running(lif->netdev)) {
  2796. ionic_txrx_deinit(lif);
  2797. ionic_txrx_free(lif);
  2798. }
  2799. ionic_lif_deinit(lif);
  2800. ionic_reset(ionic);
  2801. ionic_qcqs_free(lif);
  2802. mutex_unlock(&lif->queue_lock);
  2803. clear_bit(IONIC_LIF_F_FW_STOPPING, lif->state);
  2804. dev_info(ionic->dev, "FW Down: LIFs stopped\n");
  2805. }
  2806. int ionic_restart_lif(struct ionic_lif *lif)
  2807. {
  2808. struct ionic *ionic = lif->ionic;
  2809. int err;
  2810. mutex_lock(&lif->queue_lock);
  2811. if (test_and_clear_bit(IONIC_LIF_F_BROKEN, lif->state))
  2812. dev_info(ionic->dev, "FW Up: clearing broken state\n");
  2813. err = ionic_qcqs_alloc(lif);
  2814. if (err)
  2815. goto err_unlock;
  2816. err = ionic_lif_init(lif);
  2817. if (err)
  2818. goto err_qcqs_free;
  2819. ionic_vf_attr_replay(lif);
  2820. if (lif->registered)
  2821. ionic_lif_set_netdev_info(lif);
  2822. ionic_rx_filter_replay(lif);
  2823. if (netif_running(lif->netdev)) {
  2824. err = ionic_txrx_alloc(lif);
  2825. if (err)
  2826. goto err_lifs_deinit;
  2827. err = ionic_txrx_init(lif);
  2828. if (err)
  2829. goto err_txrx_free;
  2830. }
  2831. mutex_unlock(&lif->queue_lock);
  2832. clear_bit(IONIC_LIF_F_FW_RESET, lif->state);
  2833. ionic_link_status_check_request(lif, CAN_SLEEP);
  2834. netif_device_attach(lif->netdev);
  2835. ionic_queue_doorbell_check(ionic, IONIC_NAPI_DEADLINE);
  2836. return 0;
  2837. err_txrx_free:
  2838. ionic_txrx_free(lif);
  2839. err_lifs_deinit:
  2840. ionic_lif_deinit(lif);
  2841. err_qcqs_free:
  2842. ionic_qcqs_free(lif);
  2843. err_unlock:
  2844. mutex_unlock(&lif->queue_lock);
  2845. return err;
  2846. }
  2847. static void ionic_lif_handle_fw_up(struct ionic_lif *lif)
  2848. {
  2849. struct ionic *ionic = lif->ionic;
  2850. int err;
  2851. if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  2852. return;
  2853. dev_info(ionic->dev, "FW Up: restarting LIFs\n");
  2854. /* This is a little different from what happens at
  2855. * probe time because the LIF already exists so we
  2856. * just need to reanimate it.
  2857. */
  2858. ionic_init_devinfo(ionic);
  2859. ionic_reset(ionic);
  2860. err = ionic_identify(ionic);
  2861. if (err)
  2862. goto err_out;
  2863. err = ionic_port_identify(ionic);
  2864. if (err)
  2865. goto err_out;
  2866. err = ionic_port_init(ionic);
  2867. if (err)
  2868. goto err_out;
  2869. err = ionic_restart_lif(lif);
  2870. if (err)
  2871. goto err_out;
  2872. dev_info(ionic->dev, "FW Up: LIFs restarted\n");
  2873. /* restore the hardware timestamping queues */
  2874. ionic_lif_hwstamp_replay(lif);
  2875. return;
  2876. err_out:
  2877. dev_err(ionic->dev, "FW Up: LIFs restart failed - err %d\n", err);
  2878. }
  2879. void ionic_lif_free(struct ionic_lif *lif)
  2880. {
  2881. struct device *dev = lif->ionic->dev;
  2882. ionic_lif_free_phc(lif);
  2883. /* free rss indirection table */
  2884. dma_free_coherent(dev, lif->rss_ind_tbl_sz, lif->rss_ind_tbl,
  2885. lif->rss_ind_tbl_pa);
  2886. lif->rss_ind_tbl = NULL;
  2887. lif->rss_ind_tbl_pa = 0;
  2888. /* free queues */
  2889. ionic_qcqs_free(lif);
  2890. if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  2891. ionic_lif_reset(lif);
  2892. ionic_affinity_masks_free(lif->ionic);
  2893. /* free lif info */
  2894. kfree(lif->identity);
  2895. dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
  2896. lif->info = NULL;
  2897. lif->info_pa = 0;
  2898. mutex_destroy(&lif->config_lock);
  2899. mutex_destroy(&lif->queue_lock);
  2900. /* free netdev & lif */
  2901. ionic_debugfs_del_lif(lif);
  2902. free_netdev(lif->netdev);
  2903. }
  2904. void ionic_lif_deinit(struct ionic_lif *lif)
  2905. {
  2906. if (!test_and_clear_bit(IONIC_LIF_F_INITED, lif->state))
  2907. return;
  2908. if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
  2909. cancel_work_sync(&lif->deferred.work);
  2910. cancel_work_sync(&lif->tx_timeout_work);
  2911. ionic_rx_filters_deinit(lif);
  2912. if (lif->netdev->features & NETIF_F_RXHASH)
  2913. ionic_lif_rss_deinit(lif);
  2914. }
  2915. napi_disable(&lif->adminqcq->napi);
  2916. ionic_lif_qcq_deinit(lif, lif->notifyqcq);
  2917. ionic_lif_qcq_deinit(lif, lif->adminqcq);
  2918. ionic_bus_unmap_dbpage(lif->ionic, lif->kern_dbpage);
  2919. lif->kern_dbpage = NULL;
  2920. ionic_lif_reset(lif);
  2921. }
  2922. static int ionic_lif_adminq_init(struct ionic_lif *lif)
  2923. {
  2924. struct device *dev = lif->ionic->dev;
  2925. struct ionic_q_init_comp comp;
  2926. struct ionic_dev *idev;
  2927. struct ionic_qcq *qcq;
  2928. struct ionic_queue *q;
  2929. int err;
  2930. idev = &lif->ionic->idev;
  2931. qcq = lif->adminqcq;
  2932. q = &qcq->q;
  2933. mutex_lock(&lif->ionic->dev_cmd_lock);
  2934. ionic_dev_cmd_adminq_init(idev, qcq, lif->index, qcq->intr.index);
  2935. err = ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
  2936. ionic_dev_cmd_comp(idev, (union ionic_dev_cmd_comp *)&comp);
  2937. mutex_unlock(&lif->ionic->dev_cmd_lock);
  2938. if (err) {
  2939. netdev_err(lif->netdev, "adminq init failed %d\n", err);
  2940. return err;
  2941. }
  2942. q->hw_type = comp.hw_type;
  2943. q->hw_index = le32_to_cpu(comp.hw_index);
  2944. q->dbval = IONIC_DBELL_QID(q->hw_index);
  2945. dev_dbg(dev, "adminq->hw_type %d\n", q->hw_type);
  2946. dev_dbg(dev, "adminq->hw_index %d\n", q->hw_index);
  2947. q->dbell_deadline = IONIC_ADMIN_DOORBELL_DEADLINE;
  2948. q->dbell_jiffies = jiffies;
  2949. netif_napi_add(lif->netdev, &qcq->napi, ionic_adminq_napi);
  2950. napi_enable(&qcq->napi);
  2951. if (qcq->flags & IONIC_QCQ_F_INTR) {
  2952. irq_set_affinity_hint(qcq->intr.vector,
  2953. *qcq->intr.affinity_mask);
  2954. ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
  2955. IONIC_INTR_MASK_CLEAR);
  2956. }
  2957. qcq->flags |= IONIC_QCQ_F_INITED;
  2958. return 0;
  2959. }
  2960. static int ionic_lif_notifyq_init(struct ionic_lif *lif)
  2961. {
  2962. struct ionic_qcq *qcq = lif->notifyqcq;
  2963. struct device *dev = lif->ionic->dev;
  2964. struct ionic_queue *q = &qcq->q;
  2965. int err;
  2966. struct ionic_admin_ctx ctx = {
  2967. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  2968. .cmd.q_init = {
  2969. .opcode = IONIC_CMD_Q_INIT,
  2970. .lif_index = cpu_to_le16(lif->index),
  2971. .type = q->type,
  2972. .ver = lif->qtype_info[q->type].version,
  2973. .index = cpu_to_le32(q->index),
  2974. .flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
  2975. IONIC_QINIT_F_ENA),
  2976. .intr_index = cpu_to_le16(lif->adminqcq->intr.index),
  2977. .pid = cpu_to_le16(q->pid),
  2978. .ring_size = ilog2(q->num_descs),
  2979. .ring_base = cpu_to_le64(q->base_pa),
  2980. }
  2981. };
  2982. dev_dbg(dev, "notifyq_init.pid %d\n", ctx.cmd.q_init.pid);
  2983. dev_dbg(dev, "notifyq_init.index %d\n", ctx.cmd.q_init.index);
  2984. dev_dbg(dev, "notifyq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
  2985. dev_dbg(dev, "notifyq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
  2986. err = ionic_adminq_post_wait(lif, &ctx);
  2987. if (err)
  2988. return err;
  2989. lif->last_eid = 0;
  2990. q->hw_type = ctx.comp.q_init.hw_type;
  2991. q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
  2992. q->dbval = IONIC_DBELL_QID(q->hw_index);
  2993. dev_dbg(dev, "notifyq->hw_type %d\n", q->hw_type);
  2994. dev_dbg(dev, "notifyq->hw_index %d\n", q->hw_index);
  2995. /* preset the callback info */
  2996. q->admin_info[0].ctx = lif;
  2997. qcq->flags |= IONIC_QCQ_F_INITED;
  2998. return 0;
  2999. }
  3000. static int ionic_station_set(struct ionic_lif *lif)
  3001. {
  3002. struct net_device *netdev = lif->netdev;
  3003. struct ionic_admin_ctx ctx = {
  3004. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  3005. .cmd.lif_getattr = {
  3006. .opcode = IONIC_CMD_LIF_GETATTR,
  3007. .index = cpu_to_le16(lif->index),
  3008. .attr = IONIC_LIF_ATTR_MAC,
  3009. },
  3010. };
  3011. u8 mac_address[ETH_ALEN];
  3012. struct sockaddr addr;
  3013. int err;
  3014. err = ionic_adminq_post_wait(lif, &ctx);
  3015. if (err)
  3016. return err;
  3017. netdev_dbg(lif->netdev, "found initial MAC addr %pM\n",
  3018. ctx.comp.lif_getattr.mac);
  3019. ether_addr_copy(mac_address, ctx.comp.lif_getattr.mac);
  3020. if (is_zero_ether_addr(mac_address)) {
  3021. eth_hw_addr_random(netdev);
  3022. netdev_dbg(netdev, "Random Mac generated: %pM\n", netdev->dev_addr);
  3023. ether_addr_copy(mac_address, netdev->dev_addr);
  3024. err = ionic_program_mac(lif, mac_address);
  3025. if (err < 0)
  3026. return err;
  3027. if (err > 0) {
  3028. netdev_dbg(netdev, "%s:SET/GET ATTR Mac are not same-due to old FW running\n",
  3029. __func__);
  3030. return 0;
  3031. }
  3032. }
  3033. if (!is_zero_ether_addr(netdev->dev_addr)) {
  3034. /* If the netdev mac is non-zero and doesn't match the default
  3035. * device address, it was set by something earlier and we're
  3036. * likely here again after a fw-upgrade reset. We need to be
  3037. * sure the netdev mac is in our filter list.
  3038. */
  3039. if (!ether_addr_equal(mac_address, netdev->dev_addr))
  3040. ionic_lif_addr_add(lif, netdev->dev_addr);
  3041. } else {
  3042. /* Update the netdev mac with the device's mac */
  3043. ether_addr_copy(addr.sa_data, mac_address);
  3044. addr.sa_family = AF_INET;
  3045. err = eth_prepare_mac_addr_change(netdev, &addr);
  3046. if (err) {
  3047. netdev_warn(lif->netdev, "ignoring bad MAC addr from NIC %pM - err %d\n",
  3048. addr.sa_data, err);
  3049. return 0;
  3050. }
  3051. eth_commit_mac_addr_change(netdev, &addr);
  3052. }
  3053. netdev_dbg(lif->netdev, "adding station MAC addr %pM\n",
  3054. netdev->dev_addr);
  3055. ionic_lif_addr_add(lif, netdev->dev_addr);
  3056. return 0;
  3057. }
  3058. int ionic_lif_init(struct ionic_lif *lif)
  3059. {
  3060. struct ionic_dev *idev = &lif->ionic->idev;
  3061. struct device *dev = lif->ionic->dev;
  3062. struct ionic_lif_init_comp comp;
  3063. int dbpage_num;
  3064. int err;
  3065. mutex_lock(&lif->ionic->dev_cmd_lock);
  3066. ionic_dev_cmd_lif_init(idev, lif->index, lif->info_pa);
  3067. err = ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
  3068. ionic_dev_cmd_comp(idev, (union ionic_dev_cmd_comp *)&comp);
  3069. mutex_unlock(&lif->ionic->dev_cmd_lock);
  3070. if (err)
  3071. return err;
  3072. lif->hw_index = le16_to_cpu(comp.hw_index);
  3073. /* now that we have the hw_index we can figure out our doorbell page */
  3074. lif->dbid_count = le32_to_cpu(lif->ionic->ident.dev.ndbpgs_per_lif);
  3075. if (!lif->dbid_count) {
  3076. dev_err(dev, "No doorbell pages, aborting\n");
  3077. return -EINVAL;
  3078. }
  3079. lif->kern_pid = 0;
  3080. dbpage_num = ionic_db_page_num(lif, lif->kern_pid);
  3081. lif->kern_dbpage = ionic_bus_map_dbpage(lif->ionic, dbpage_num);
  3082. if (!lif->kern_dbpage) {
  3083. dev_err(dev, "Cannot map dbpage, aborting\n");
  3084. return -ENOMEM;
  3085. }
  3086. err = ionic_lif_adminq_init(lif);
  3087. if (err)
  3088. goto err_out_adminq_deinit;
  3089. if (lif->ionic->nnqs_per_lif) {
  3090. err = ionic_lif_notifyq_init(lif);
  3091. if (err)
  3092. goto err_out_notifyq_deinit;
  3093. }
  3094. if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  3095. err = ionic_set_nic_features(lif, lif->netdev->features);
  3096. else
  3097. err = ionic_init_nic_features(lif);
  3098. if (err)
  3099. goto err_out_notifyq_deinit;
  3100. if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
  3101. err = ionic_rx_filters_init(lif);
  3102. if (err)
  3103. goto err_out_notifyq_deinit;
  3104. }
  3105. err = ionic_station_set(lif);
  3106. if (err)
  3107. goto err_out_notifyq_deinit;
  3108. lif->rx_copybreak = IONIC_RX_COPYBREAK_DEFAULT;
  3109. lif->doorbell_wa = ionic_doorbell_wa(lif->ionic);
  3110. set_bit(IONIC_LIF_F_INITED, lif->state);
  3111. INIT_WORK(&lif->tx_timeout_work, ionic_tx_timeout_work);
  3112. return 0;
  3113. err_out_notifyq_deinit:
  3114. napi_disable(&lif->adminqcq->napi);
  3115. ionic_lif_qcq_deinit(lif, lif->notifyqcq);
  3116. err_out_adminq_deinit:
  3117. ionic_lif_qcq_deinit(lif, lif->adminqcq);
  3118. ionic_lif_reset(lif);
  3119. ionic_bus_unmap_dbpage(lif->ionic, lif->kern_dbpage);
  3120. lif->kern_dbpage = NULL;
  3121. return err;
  3122. }
  3123. static void ionic_lif_notify_work(struct work_struct *ws)
  3124. {
  3125. }
  3126. static void ionic_lif_set_netdev_info(struct ionic_lif *lif)
  3127. {
  3128. struct ionic_admin_ctx ctx = {
  3129. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  3130. .cmd.lif_setattr = {
  3131. .opcode = IONIC_CMD_LIF_SETATTR,
  3132. .index = cpu_to_le16(lif->index),
  3133. .attr = IONIC_LIF_ATTR_NAME,
  3134. },
  3135. };
  3136. strscpy(ctx.cmd.lif_setattr.name, netdev_name(lif->netdev),
  3137. sizeof(ctx.cmd.lif_setattr.name));
  3138. ionic_adminq_post_wait(lif, &ctx);
  3139. }
  3140. static struct ionic_lif *ionic_netdev_lif(struct net_device *netdev)
  3141. {
  3142. if (!netdev || netdev->netdev_ops->ndo_start_xmit != ionic_start_xmit)
  3143. return NULL;
  3144. return netdev_priv(netdev);
  3145. }
  3146. static int ionic_lif_notify(struct notifier_block *nb,
  3147. unsigned long event, void *info)
  3148. {
  3149. struct net_device *ndev = netdev_notifier_info_to_dev(info);
  3150. struct ionic *ionic = container_of(nb, struct ionic, nb);
  3151. struct ionic_lif *lif = ionic_netdev_lif(ndev);
  3152. if (!lif || lif->ionic != ionic)
  3153. return NOTIFY_DONE;
  3154. switch (event) {
  3155. case NETDEV_CHANGENAME:
  3156. ionic_lif_set_netdev_info(lif);
  3157. break;
  3158. }
  3159. return NOTIFY_DONE;
  3160. }
  3161. int ionic_lif_register(struct ionic_lif *lif)
  3162. {
  3163. int err;
  3164. ionic_lif_register_phc(lif);
  3165. INIT_WORK(&lif->ionic->nb_work, ionic_lif_notify_work);
  3166. lif->ionic->nb.notifier_call = ionic_lif_notify;
  3167. err = register_netdevice_notifier(&lif->ionic->nb);
  3168. if (err)
  3169. lif->ionic->nb.notifier_call = NULL;
  3170. /* only register LIF0 for now */
  3171. err = register_netdev(lif->netdev);
  3172. if (err) {
  3173. dev_err(lif->ionic->dev, "Cannot register net device: %d, aborting\n", err);
  3174. ionic_lif_unregister(lif);
  3175. return err;
  3176. }
  3177. ionic_link_status_check_request(lif, CAN_SLEEP);
  3178. lif->registered = true;
  3179. ionic_lif_set_netdev_info(lif);
  3180. return 0;
  3181. }
  3182. void ionic_lif_unregister(struct ionic_lif *lif)
  3183. {
  3184. if (lif->ionic->nb.notifier_call) {
  3185. unregister_netdevice_notifier(&lif->ionic->nb);
  3186. cancel_work_sync(&lif->ionic->nb_work);
  3187. lif->ionic->nb.notifier_call = NULL;
  3188. }
  3189. if (lif->netdev->reg_state == NETREG_REGISTERED)
  3190. unregister_netdev(lif->netdev);
  3191. ionic_lif_unregister_phc(lif);
  3192. lif->registered = false;
  3193. }
  3194. static void ionic_lif_queue_identify(struct ionic_lif *lif)
  3195. {
  3196. union ionic_q_identity __iomem *q_ident;
  3197. struct ionic *ionic = lif->ionic;
  3198. struct ionic_dev *idev;
  3199. u16 max_frags;
  3200. int qtype;
  3201. int err;
  3202. idev = &lif->ionic->idev;
  3203. q_ident = (union ionic_q_identity __iomem *)&idev->dev_cmd_regs->data;
  3204. for (qtype = 0; qtype < ARRAY_SIZE(ionic_qtype_versions); qtype++) {
  3205. struct ionic_qtype_info *qti = &lif->qtype_info[qtype];
  3206. /* filter out the ones we know about */
  3207. switch (qtype) {
  3208. case IONIC_QTYPE_ADMINQ:
  3209. case IONIC_QTYPE_NOTIFYQ:
  3210. case IONIC_QTYPE_RXQ:
  3211. case IONIC_QTYPE_TXQ:
  3212. break;
  3213. default:
  3214. continue;
  3215. }
  3216. memset(qti, 0, sizeof(*qti));
  3217. mutex_lock(&ionic->dev_cmd_lock);
  3218. ionic_dev_cmd_queue_identify(idev, lif->lif_type, qtype,
  3219. ionic_qtype_versions[qtype]);
  3220. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  3221. if (!err) {
  3222. qti->version = readb(&q_ident->version);
  3223. qti->supported = readb(&q_ident->supported);
  3224. qti->features = readq(&q_ident->features);
  3225. qti->desc_sz = readw(&q_ident->desc_sz);
  3226. qti->comp_sz = readw(&q_ident->comp_sz);
  3227. qti->sg_desc_sz = readw(&q_ident->sg_desc_sz);
  3228. qti->max_sg_elems = readw(&q_ident->max_sg_elems);
  3229. qti->sg_desc_stride = readw(&q_ident->sg_desc_stride);
  3230. }
  3231. mutex_unlock(&ionic->dev_cmd_lock);
  3232. if (err == -EINVAL) {
  3233. dev_err(ionic->dev, "qtype %d not supported\n", qtype);
  3234. continue;
  3235. } else if (err == -EIO) {
  3236. dev_err(ionic->dev, "q_ident failed, not supported on older FW\n");
  3237. return;
  3238. } else if (err) {
  3239. dev_err(ionic->dev, "q_ident failed, qtype %d: %d\n",
  3240. qtype, err);
  3241. return;
  3242. }
  3243. dev_dbg(ionic->dev, " qtype[%d].version = %d\n",
  3244. qtype, qti->version);
  3245. dev_dbg(ionic->dev, " qtype[%d].supported = 0x%02x\n",
  3246. qtype, qti->supported);
  3247. dev_dbg(ionic->dev, " qtype[%d].features = 0x%04llx\n",
  3248. qtype, qti->features);
  3249. dev_dbg(ionic->dev, " qtype[%d].desc_sz = %d\n",
  3250. qtype, qti->desc_sz);
  3251. dev_dbg(ionic->dev, " qtype[%d].comp_sz = %d\n",
  3252. qtype, qti->comp_sz);
  3253. dev_dbg(ionic->dev, " qtype[%d].sg_desc_sz = %d\n",
  3254. qtype, qti->sg_desc_sz);
  3255. dev_dbg(ionic->dev, " qtype[%d].max_sg_elems = %d\n",
  3256. qtype, qti->max_sg_elems);
  3257. dev_dbg(ionic->dev, " qtype[%d].sg_desc_stride = %d\n",
  3258. qtype, qti->sg_desc_stride);
  3259. if (qtype == IONIC_QTYPE_TXQ)
  3260. max_frags = IONIC_TX_MAX_FRAGS;
  3261. else if (qtype == IONIC_QTYPE_RXQ)
  3262. max_frags = IONIC_RX_MAX_FRAGS;
  3263. else
  3264. max_frags = 1;
  3265. qti->max_sg_elems = min_t(u16, max_frags - 1, MAX_SKB_FRAGS);
  3266. dev_dbg(ionic->dev, "qtype %d max_sg_elems %d\n",
  3267. qtype, qti->max_sg_elems);
  3268. }
  3269. }
  3270. int ionic_lif_identify(struct ionic *ionic, u8 lif_type,
  3271. union ionic_lif_identity *lid)
  3272. {
  3273. struct ionic_dev *idev = &ionic->idev;
  3274. size_t sz;
  3275. int err;
  3276. sz = min(sizeof(*lid), sizeof(idev->dev_cmd_regs->data));
  3277. mutex_lock(&ionic->dev_cmd_lock);
  3278. ionic_dev_cmd_lif_identify(idev, lif_type, IONIC_IDENTITY_VERSION_1);
  3279. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  3280. memcpy_fromio(lid, &idev->dev_cmd_regs->data, sz);
  3281. mutex_unlock(&ionic->dev_cmd_lock);
  3282. if (err)
  3283. return (err);
  3284. dev_dbg(ionic->dev, "capabilities 0x%llx\n",
  3285. le64_to_cpu(lid->capabilities));
  3286. dev_dbg(ionic->dev, "eth.max_ucast_filters %d\n",
  3287. le32_to_cpu(lid->eth.max_ucast_filters));
  3288. dev_dbg(ionic->dev, "eth.max_mcast_filters %d\n",
  3289. le32_to_cpu(lid->eth.max_mcast_filters));
  3290. dev_dbg(ionic->dev, "eth.features 0x%llx\n",
  3291. le64_to_cpu(lid->eth.config.features));
  3292. dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_ADMINQ] %d\n",
  3293. le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_ADMINQ]));
  3294. dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_NOTIFYQ] %d\n",
  3295. le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_NOTIFYQ]));
  3296. dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_RXQ] %d\n",
  3297. le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_RXQ]));
  3298. dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_TXQ] %d\n",
  3299. le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_TXQ]));
  3300. dev_dbg(ionic->dev, "eth.config.name %s\n", lid->eth.config.name);
  3301. dev_dbg(ionic->dev, "eth.config.mac %pM\n", lid->eth.config.mac);
  3302. dev_dbg(ionic->dev, "eth.config.mtu %d\n",
  3303. le32_to_cpu(lid->eth.config.mtu));
  3304. return 0;
  3305. }
  3306. int ionic_lif_size(struct ionic *ionic)
  3307. {
  3308. struct ionic_identity *ident = &ionic->ident;
  3309. unsigned int nintrs, dev_nintrs;
  3310. union ionic_lif_config *lc;
  3311. unsigned int ntxqs_per_lif;
  3312. unsigned int nrxqs_per_lif;
  3313. unsigned int neqs_per_lif;
  3314. unsigned int nnqs_per_lif;
  3315. unsigned int nxqs, neqs;
  3316. unsigned int min_intrs;
  3317. int err;
  3318. /* retrieve basic values from FW */
  3319. lc = &ident->lif.eth.config;
  3320. dev_nintrs = le32_to_cpu(ident->dev.nintrs);
  3321. neqs_per_lif = le32_to_cpu(ident->lif.rdma.eq_qtype.qid_count);
  3322. nnqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_NOTIFYQ]);
  3323. ntxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_TXQ]);
  3324. nrxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_RXQ]);
  3325. /* limit values to play nice with kdump */
  3326. if (is_kdump_kernel()) {
  3327. dev_nintrs = 2;
  3328. neqs_per_lif = 0;
  3329. nnqs_per_lif = 0;
  3330. ntxqs_per_lif = 1;
  3331. nrxqs_per_lif = 1;
  3332. }
  3333. /* reserve last queue id for hardware timestamping */
  3334. if (lc->features & cpu_to_le64(IONIC_ETH_HW_TIMESTAMP)) {
  3335. if (ntxqs_per_lif <= 1 || nrxqs_per_lif <= 1) {
  3336. lc->features &= cpu_to_le64(~IONIC_ETH_HW_TIMESTAMP);
  3337. } else {
  3338. ntxqs_per_lif -= 1;
  3339. nrxqs_per_lif -= 1;
  3340. }
  3341. }
  3342. nxqs = min(ntxqs_per_lif, nrxqs_per_lif);
  3343. nxqs = min(nxqs, num_online_cpus());
  3344. neqs = min(neqs_per_lif, num_online_cpus());
  3345. try_again:
  3346. /* interrupt usage:
  3347. * 1 for master lif adminq/notifyq
  3348. * 1 for each CPU for master lif TxRx queue pairs
  3349. * whatever's left is for RDMA queues
  3350. */
  3351. nintrs = 1 + nxqs + neqs;
  3352. min_intrs = 2; /* adminq + 1 TxRx queue pair */
  3353. if (nintrs > dev_nintrs)
  3354. goto try_fewer;
  3355. err = ionic_bus_alloc_irq_vectors(ionic, nintrs);
  3356. if (err < 0 && err != -ENOSPC) {
  3357. dev_err(ionic->dev, "Can't get intrs from OS: %d\n", err);
  3358. return err;
  3359. }
  3360. if (err == -ENOSPC)
  3361. goto try_fewer;
  3362. if (err != nintrs) {
  3363. ionic_bus_free_irq_vectors(ionic);
  3364. goto try_fewer;
  3365. }
  3366. ionic->nnqs_per_lif = nnqs_per_lif;
  3367. ionic->neqs_per_lif = neqs;
  3368. ionic->ntxqs_per_lif = nxqs;
  3369. ionic->nrxqs_per_lif = nxqs;
  3370. ionic->nintrs = nintrs;
  3371. ionic_debugfs_add_sizes(ionic);
  3372. return 0;
  3373. try_fewer:
  3374. if (nnqs_per_lif > 1) {
  3375. nnqs_per_lif >>= 1;
  3376. goto try_again;
  3377. }
  3378. if (neqs > 1) {
  3379. neqs >>= 1;
  3380. goto try_again;
  3381. }
  3382. if (nxqs > 1) {
  3383. nxqs >>= 1;
  3384. goto try_again;
  3385. }
  3386. dev_err(ionic->dev, "Can't get minimum %d intrs from OS\n", min_intrs);
  3387. return -ENOSPC;
  3388. }