bnx2x_cmn.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956
  1. /* bnx2x_cmn.c: Broadcom Everest network driver.
  2. *
  3. * Copyright (c) 2007-2012 Broadcom Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. *
  9. * Maintained by: Eilon Greenstein <eilong@broadcom.com>
  10. * Written by: Eliezer Tamir
  11. * Based on code from Michael Chan's bnx2 driver
  12. * UDP CSUM errata workaround by Arik Gendelman
  13. * Slowpath and fastpath rework by Vladislav Zolotarov
  14. * Statistics and Link management by Yitchak Gertner
  15. *
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/ip.h>
  22. #include <net/ipv6.h>
  23. #include <net/ip6_checksum.h>
  24. #include <linux/prefetch.h>
  25. #include "bnx2x_cmn.h"
  26. #include "bnx2x_init.h"
  27. #include "bnx2x_sp.h"
  28. /**
  29. * bnx2x_move_fp - move content of the fastpath structure.
  30. *
  31. * @bp: driver handle
  32. * @from: source FP index
  33. * @to: destination FP index
  34. *
  35. * Makes sure the contents of the bp->fp[to].napi is kept
  36. * intact. This is done by first copying the napi struct from
  37. * the target to the source, and then mem copying the entire
  38. * source onto the target
  39. */
  40. static inline void bnx2x_move_fp(struct bnx2x *bp, int from, int to)
  41. {
  42. struct bnx2x_fastpath *from_fp = &bp->fp[from];
  43. struct bnx2x_fastpath *to_fp = &bp->fp[to];
  44. /* Copy the NAPI object as it has been already initialized */
  45. from_fp->napi = to_fp->napi;
  46. /* Move bnx2x_fastpath contents */
  47. memcpy(to_fp, from_fp, sizeof(*to_fp));
  48. to_fp->index = to;
  49. }
  50. int load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
  51. /* free skb in the packet ring at pos idx
  52. * return idx of last bd freed
  53. */
  54. static u16 bnx2x_free_tx_pkt(struct bnx2x *bp, struct bnx2x_fp_txdata *txdata,
  55. u16 idx, unsigned int *pkts_compl,
  56. unsigned int *bytes_compl)
  57. {
  58. struct sw_tx_bd *tx_buf = &txdata->tx_buf_ring[idx];
  59. struct eth_tx_start_bd *tx_start_bd;
  60. struct eth_tx_bd *tx_data_bd;
  61. struct sk_buff *skb = tx_buf->skb;
  62. u16 bd_idx = TX_BD(tx_buf->first_bd), new_cons;
  63. int nbd;
  64. /* prefetch skb end pointer to speedup dev_kfree_skb() */
  65. prefetch(&skb->end);
  66. DP(NETIF_MSG_TX_DONE, "fp[%d]: pkt_idx %d buff @(%p)->skb %p\n",
  67. txdata->txq_index, idx, tx_buf, skb);
  68. /* unmap first bd */
  69. tx_start_bd = &txdata->tx_desc_ring[bd_idx].start_bd;
  70. dma_unmap_single(&bp->pdev->dev, BD_UNMAP_ADDR(tx_start_bd),
  71. BD_UNMAP_LEN(tx_start_bd), DMA_TO_DEVICE);
  72. nbd = le16_to_cpu(tx_start_bd->nbd) - 1;
  73. #ifdef BNX2X_STOP_ON_ERROR
  74. if ((nbd - 1) > (MAX_SKB_FRAGS + 2)) {
  75. BNX2X_ERR("BAD nbd!\n");
  76. bnx2x_panic();
  77. }
  78. #endif
  79. new_cons = nbd + tx_buf->first_bd;
  80. /* Get the next bd */
  81. bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
  82. /* Skip a parse bd... */
  83. --nbd;
  84. bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
  85. /* ...and the TSO split header bd since they have no mapping */
  86. if (tx_buf->flags & BNX2X_TSO_SPLIT_BD) {
  87. --nbd;
  88. bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
  89. }
  90. /* now free frags */
  91. while (nbd > 0) {
  92. tx_data_bd = &txdata->tx_desc_ring[bd_idx].reg_bd;
  93. dma_unmap_page(&bp->pdev->dev, BD_UNMAP_ADDR(tx_data_bd),
  94. BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
  95. if (--nbd)
  96. bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
  97. }
  98. /* release skb */
  99. WARN_ON(!skb);
  100. if (likely(skb)) {
  101. (*pkts_compl)++;
  102. (*bytes_compl) += skb->len;
  103. }
  104. dev_kfree_skb_any(skb);
  105. tx_buf->first_bd = 0;
  106. tx_buf->skb = NULL;
  107. return new_cons;
  108. }
  109. int bnx2x_tx_int(struct bnx2x *bp, struct bnx2x_fp_txdata *txdata)
  110. {
  111. struct netdev_queue *txq;
  112. u16 hw_cons, sw_cons, bd_cons = txdata->tx_bd_cons;
  113. unsigned int pkts_compl = 0, bytes_compl = 0;
  114. #ifdef BNX2X_STOP_ON_ERROR
  115. if (unlikely(bp->panic))
  116. return -1;
  117. #endif
  118. txq = netdev_get_tx_queue(bp->dev, txdata->txq_index);
  119. hw_cons = le16_to_cpu(*txdata->tx_cons_sb);
  120. sw_cons = txdata->tx_pkt_cons;
  121. while (sw_cons != hw_cons) {
  122. u16 pkt_cons;
  123. pkt_cons = TX_BD(sw_cons);
  124. DP(NETIF_MSG_TX_DONE,
  125. "queue[%d]: hw_cons %u sw_cons %u pkt_cons %u\n",
  126. txdata->txq_index, hw_cons, sw_cons, pkt_cons);
  127. bd_cons = bnx2x_free_tx_pkt(bp, txdata, pkt_cons,
  128. &pkts_compl, &bytes_compl);
  129. sw_cons++;
  130. }
  131. netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
  132. txdata->tx_pkt_cons = sw_cons;
  133. txdata->tx_bd_cons = bd_cons;
  134. /* Need to make the tx_bd_cons update visible to start_xmit()
  135. * before checking for netif_tx_queue_stopped(). Without the
  136. * memory barrier, there is a small possibility that
  137. * start_xmit() will miss it and cause the queue to be stopped
  138. * forever.
  139. * On the other hand we need an rmb() here to ensure the proper
  140. * ordering of bit testing in the following
  141. * netif_tx_queue_stopped(txq) call.
  142. */
  143. smp_mb();
  144. if (unlikely(netif_tx_queue_stopped(txq))) {
  145. /* Taking tx_lock() is needed to prevent reenabling the queue
  146. * while it's empty. This could have happen if rx_action() gets
  147. * suspended in bnx2x_tx_int() after the condition before
  148. * netif_tx_wake_queue(), while tx_action (bnx2x_start_xmit()):
  149. *
  150. * stops the queue->sees fresh tx_bd_cons->releases the queue->
  151. * sends some packets consuming the whole queue again->
  152. * stops the queue
  153. */
  154. __netif_tx_lock(txq, smp_processor_id());
  155. if ((netif_tx_queue_stopped(txq)) &&
  156. (bp->state == BNX2X_STATE_OPEN) &&
  157. (bnx2x_tx_avail(bp, txdata) >= MAX_SKB_FRAGS + 3))
  158. netif_tx_wake_queue(txq);
  159. __netif_tx_unlock(txq);
  160. }
  161. return 0;
  162. }
  163. static inline void bnx2x_update_last_max_sge(struct bnx2x_fastpath *fp,
  164. u16 idx)
  165. {
  166. u16 last_max = fp->last_max_sge;
  167. if (SUB_S16(idx, last_max) > 0)
  168. fp->last_max_sge = idx;
  169. }
  170. static inline void bnx2x_update_sge_prod(struct bnx2x_fastpath *fp,
  171. u16 sge_len,
  172. struct eth_end_agg_rx_cqe *cqe)
  173. {
  174. struct bnx2x *bp = fp->bp;
  175. u16 last_max, last_elem, first_elem;
  176. u16 delta = 0;
  177. u16 i;
  178. if (!sge_len)
  179. return;
  180. /* First mark all used pages */
  181. for (i = 0; i < sge_len; i++)
  182. BIT_VEC64_CLEAR_BIT(fp->sge_mask,
  183. RX_SGE(le16_to_cpu(cqe->sgl_or_raw_data.sgl[i])));
  184. DP(NETIF_MSG_RX_STATUS, "fp_cqe->sgl[%d] = %d\n",
  185. sge_len - 1, le16_to_cpu(cqe->sgl_or_raw_data.sgl[sge_len - 1]));
  186. /* Here we assume that the last SGE index is the biggest */
  187. prefetch((void *)(fp->sge_mask));
  188. bnx2x_update_last_max_sge(fp,
  189. le16_to_cpu(cqe->sgl_or_raw_data.sgl[sge_len - 1]));
  190. last_max = RX_SGE(fp->last_max_sge);
  191. last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
  192. first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
  193. /* If ring is not full */
  194. if (last_elem + 1 != first_elem)
  195. last_elem++;
  196. /* Now update the prod */
  197. for (i = first_elem; i != last_elem; i = NEXT_SGE_MASK_ELEM(i)) {
  198. if (likely(fp->sge_mask[i]))
  199. break;
  200. fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
  201. delta += BIT_VEC64_ELEM_SZ;
  202. }
  203. if (delta > 0) {
  204. fp->rx_sge_prod += delta;
  205. /* clear page-end entries */
  206. bnx2x_clear_sge_mask_next_elems(fp);
  207. }
  208. DP(NETIF_MSG_RX_STATUS,
  209. "fp->last_max_sge = %d fp->rx_sge_prod = %d\n",
  210. fp->last_max_sge, fp->rx_sge_prod);
  211. }
  212. /* Set Toeplitz hash value in the skb using the value from the
  213. * CQE (calculated by HW).
  214. */
  215. static u32 bnx2x_get_rxhash(const struct bnx2x *bp,
  216. const struct eth_fast_path_rx_cqe *cqe)
  217. {
  218. /* Set Toeplitz hash from CQE */
  219. if ((bp->dev->features & NETIF_F_RXHASH) &&
  220. (cqe->status_flags & ETH_FAST_PATH_RX_CQE_RSS_HASH_FLG))
  221. return le32_to_cpu(cqe->rss_hash_result);
  222. return 0;
  223. }
  224. static void bnx2x_tpa_start(struct bnx2x_fastpath *fp, u16 queue,
  225. u16 cons, u16 prod,
  226. struct eth_fast_path_rx_cqe *cqe)
  227. {
  228. struct bnx2x *bp = fp->bp;
  229. struct sw_rx_bd *cons_rx_buf = &fp->rx_buf_ring[cons];
  230. struct sw_rx_bd *prod_rx_buf = &fp->rx_buf_ring[prod];
  231. struct eth_rx_bd *prod_bd = &fp->rx_desc_ring[prod];
  232. dma_addr_t mapping;
  233. struct bnx2x_agg_info *tpa_info = &fp->tpa_info[queue];
  234. struct sw_rx_bd *first_buf = &tpa_info->first_buf;
  235. /* print error if current state != stop */
  236. if (tpa_info->tpa_state != BNX2X_TPA_STOP)
  237. BNX2X_ERR("start of bin not in stop [%d]\n", queue);
  238. /* Try to map an empty data buffer from the aggregation info */
  239. mapping = dma_map_single(&bp->pdev->dev,
  240. first_buf->data + NET_SKB_PAD,
  241. fp->rx_buf_size, DMA_FROM_DEVICE);
  242. /*
  243. * ...if it fails - move the skb from the consumer to the producer
  244. * and set the current aggregation state as ERROR to drop it
  245. * when TPA_STOP arrives.
  246. */
  247. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  248. /* Move the BD from the consumer to the producer */
  249. bnx2x_reuse_rx_data(fp, cons, prod);
  250. tpa_info->tpa_state = BNX2X_TPA_ERROR;
  251. return;
  252. }
  253. /* move empty data from pool to prod */
  254. prod_rx_buf->data = first_buf->data;
  255. dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
  256. /* point prod_bd to new data */
  257. prod_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  258. prod_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  259. /* move partial skb from cons to pool (don't unmap yet) */
  260. *first_buf = *cons_rx_buf;
  261. /* mark bin state as START */
  262. tpa_info->parsing_flags =
  263. le16_to_cpu(cqe->pars_flags.flags);
  264. tpa_info->vlan_tag = le16_to_cpu(cqe->vlan_tag);
  265. tpa_info->tpa_state = BNX2X_TPA_START;
  266. tpa_info->len_on_bd = le16_to_cpu(cqe->len_on_bd);
  267. tpa_info->placement_offset = cqe->placement_offset;
  268. tpa_info->rxhash = bnx2x_get_rxhash(bp, cqe);
  269. if (fp->mode == TPA_MODE_GRO) {
  270. u16 gro_size = le16_to_cpu(cqe->pkt_len_or_gro_seg_len);
  271. tpa_info->full_page =
  272. SGE_PAGE_SIZE * PAGES_PER_SGE / gro_size * gro_size;
  273. tpa_info->gro_size = gro_size;
  274. }
  275. #ifdef BNX2X_STOP_ON_ERROR
  276. fp->tpa_queue_used |= (1 << queue);
  277. #ifdef _ASM_GENERIC_INT_L64_H
  278. DP(NETIF_MSG_RX_STATUS, "fp->tpa_queue_used = 0x%lx\n",
  279. #else
  280. DP(NETIF_MSG_RX_STATUS, "fp->tpa_queue_used = 0x%llx\n",
  281. #endif
  282. fp->tpa_queue_used);
  283. #endif
  284. }
  285. /* Timestamp option length allowed for TPA aggregation:
  286. *
  287. * nop nop kind length echo val
  288. */
  289. #define TPA_TSTAMP_OPT_LEN 12
  290. /**
  291. * bnx2x_set_lro_mss - calculate the approximate value of the MSS
  292. *
  293. * @bp: driver handle
  294. * @parsing_flags: parsing flags from the START CQE
  295. * @len_on_bd: total length of the first packet for the
  296. * aggregation.
  297. *
  298. * Approximate value of the MSS for this aggregation calculated using
  299. * the first packet of it.
  300. */
  301. static u16 bnx2x_set_lro_mss(struct bnx2x *bp, u16 parsing_flags,
  302. u16 len_on_bd)
  303. {
  304. /*
  305. * TPA arrgregation won't have either IP options or TCP options
  306. * other than timestamp or IPv6 extension headers.
  307. */
  308. u16 hdrs_len = ETH_HLEN + sizeof(struct tcphdr);
  309. if (GET_FLAG(parsing_flags, PARSING_FLAGS_OVER_ETHERNET_PROTOCOL) ==
  310. PRS_FLAG_OVERETH_IPV6)
  311. hdrs_len += sizeof(struct ipv6hdr);
  312. else /* IPv4 */
  313. hdrs_len += sizeof(struct iphdr);
  314. /* Check if there was a TCP timestamp, if there is it's will
  315. * always be 12 bytes length: nop nop kind length echo val.
  316. *
  317. * Otherwise FW would close the aggregation.
  318. */
  319. if (parsing_flags & PARSING_FLAGS_TIME_STAMP_EXIST_FLAG)
  320. hdrs_len += TPA_TSTAMP_OPT_LEN;
  321. return len_on_bd - hdrs_len;
  322. }
  323. static int bnx2x_alloc_rx_sge(struct bnx2x *bp,
  324. struct bnx2x_fastpath *fp, u16 index)
  325. {
  326. struct page *page = alloc_pages(GFP_ATOMIC, PAGES_PER_SGE_SHIFT);
  327. struct sw_rx_page *sw_buf = &fp->rx_page_ring[index];
  328. struct eth_rx_sge *sge = &fp->rx_sge_ring[index];
  329. dma_addr_t mapping;
  330. if (unlikely(page == NULL)) {
  331. BNX2X_ERR("Can't alloc sge\n");
  332. return -ENOMEM;
  333. }
  334. mapping = dma_map_page(&bp->pdev->dev, page, 0,
  335. SGE_PAGE_SIZE*PAGES_PER_SGE, DMA_FROM_DEVICE);
  336. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  337. __free_pages(page, PAGES_PER_SGE_SHIFT);
  338. BNX2X_ERR("Can't map sge\n");
  339. return -ENOMEM;
  340. }
  341. sw_buf->page = page;
  342. dma_unmap_addr_set(sw_buf, mapping, mapping);
  343. sge->addr_hi = cpu_to_le32(U64_HI(mapping));
  344. sge->addr_lo = cpu_to_le32(U64_LO(mapping));
  345. return 0;
  346. }
  347. static int bnx2x_fill_frag_skb(struct bnx2x *bp, struct bnx2x_fastpath *fp,
  348. struct bnx2x_agg_info *tpa_info,
  349. u16 pages,
  350. struct sk_buff *skb,
  351. struct eth_end_agg_rx_cqe *cqe,
  352. u16 cqe_idx)
  353. {
  354. struct sw_rx_page *rx_pg, old_rx_pg;
  355. u32 i, frag_len, frag_size;
  356. int err, j, frag_id = 0;
  357. u16 len_on_bd = tpa_info->len_on_bd;
  358. u16 full_page = 0, gro_size = 0;
  359. frag_size = le16_to_cpu(cqe->pkt_len) - len_on_bd;
  360. if (fp->mode == TPA_MODE_GRO) {
  361. gro_size = tpa_info->gro_size;
  362. full_page = tpa_info->full_page;
  363. }
  364. /* This is needed in order to enable forwarding support */
  365. if (frag_size) {
  366. skb_shinfo(skb)->gso_size = bnx2x_set_lro_mss(bp,
  367. tpa_info->parsing_flags, len_on_bd);
  368. /* set for GRO */
  369. if (fp->mode == TPA_MODE_GRO)
  370. skb_shinfo(skb)->gso_type =
  371. (GET_FLAG(tpa_info->parsing_flags,
  372. PARSING_FLAGS_OVER_ETHERNET_PROTOCOL) ==
  373. PRS_FLAG_OVERETH_IPV6) ?
  374. SKB_GSO_TCPV6 : SKB_GSO_TCPV4;
  375. }
  376. #ifdef BNX2X_STOP_ON_ERROR
  377. if (pages > min_t(u32, 8, MAX_SKB_FRAGS)*SGE_PAGE_SIZE*PAGES_PER_SGE) {
  378. BNX2X_ERR("SGL length is too long: %d. CQE index is %d\n",
  379. pages, cqe_idx);
  380. BNX2X_ERR("cqe->pkt_len = %d\n", cqe->pkt_len);
  381. bnx2x_panic();
  382. return -EINVAL;
  383. }
  384. #endif
  385. /* Run through the SGL and compose the fragmented skb */
  386. for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
  387. u16 sge_idx = RX_SGE(le16_to_cpu(cqe->sgl_or_raw_data.sgl[j]));
  388. /* FW gives the indices of the SGE as if the ring is an array
  389. (meaning that "next" element will consume 2 indices) */
  390. if (fp->mode == TPA_MODE_GRO)
  391. frag_len = min_t(u32, frag_size, (u32)full_page);
  392. else /* LRO */
  393. frag_len = min_t(u32, frag_size,
  394. (u32)(SGE_PAGE_SIZE * PAGES_PER_SGE));
  395. rx_pg = &fp->rx_page_ring[sge_idx];
  396. old_rx_pg = *rx_pg;
  397. /* If we fail to allocate a substitute page, we simply stop
  398. where we are and drop the whole packet */
  399. err = bnx2x_alloc_rx_sge(bp, fp, sge_idx);
  400. if (unlikely(err)) {
  401. fp->eth_q_stats.rx_skb_alloc_failed++;
  402. return err;
  403. }
  404. /* Unmap the page as we r going to pass it to the stack */
  405. dma_unmap_page(&bp->pdev->dev,
  406. dma_unmap_addr(&old_rx_pg, mapping),
  407. SGE_PAGE_SIZE*PAGES_PER_SGE, DMA_FROM_DEVICE);
  408. /* Add one frag and update the appropriate fields in the skb */
  409. if (fp->mode == TPA_MODE_LRO)
  410. skb_fill_page_desc(skb, j, old_rx_pg.page, 0, frag_len);
  411. else { /* GRO */
  412. int rem;
  413. int offset = 0;
  414. for (rem = frag_len; rem > 0; rem -= gro_size) {
  415. int len = rem > gro_size ? gro_size : rem;
  416. skb_fill_page_desc(skb, frag_id++,
  417. old_rx_pg.page, offset, len);
  418. if (offset)
  419. get_page(old_rx_pg.page);
  420. offset += len;
  421. }
  422. }
  423. skb->data_len += frag_len;
  424. skb->truesize += SGE_PAGE_SIZE * PAGES_PER_SGE;
  425. skb->len += frag_len;
  426. frag_size -= frag_len;
  427. }
  428. return 0;
  429. }
  430. static void bnx2x_tpa_stop(struct bnx2x *bp, struct bnx2x_fastpath *fp,
  431. struct bnx2x_agg_info *tpa_info,
  432. u16 pages,
  433. struct eth_end_agg_rx_cqe *cqe,
  434. u16 cqe_idx)
  435. {
  436. struct sw_rx_bd *rx_buf = &tpa_info->first_buf;
  437. u8 pad = tpa_info->placement_offset;
  438. u16 len = tpa_info->len_on_bd;
  439. struct sk_buff *skb = NULL;
  440. u8 *new_data, *data = rx_buf->data;
  441. u8 old_tpa_state = tpa_info->tpa_state;
  442. tpa_info->tpa_state = BNX2X_TPA_STOP;
  443. /* If we there was an error during the handling of the TPA_START -
  444. * drop this aggregation.
  445. */
  446. if (old_tpa_state == BNX2X_TPA_ERROR)
  447. goto drop;
  448. /* Try to allocate the new data */
  449. new_data = kmalloc(fp->rx_buf_size + NET_SKB_PAD, GFP_ATOMIC);
  450. /* Unmap skb in the pool anyway, as we are going to change
  451. pool entry status to BNX2X_TPA_STOP even if new skb allocation
  452. fails. */
  453. dma_unmap_single(&bp->pdev->dev, dma_unmap_addr(rx_buf, mapping),
  454. fp->rx_buf_size, DMA_FROM_DEVICE);
  455. if (likely(new_data))
  456. skb = build_skb(data, 0);
  457. if (likely(skb)) {
  458. #ifdef BNX2X_STOP_ON_ERROR
  459. if (pad + len > fp->rx_buf_size) {
  460. BNX2X_ERR("skb_put is about to fail... pad %d len %d rx_buf_size %d\n",
  461. pad, len, fp->rx_buf_size);
  462. bnx2x_panic();
  463. return;
  464. }
  465. #endif
  466. skb_reserve(skb, pad + NET_SKB_PAD);
  467. skb_put(skb, len);
  468. skb->rxhash = tpa_info->rxhash;
  469. skb->protocol = eth_type_trans(skb, bp->dev);
  470. skb->ip_summed = CHECKSUM_UNNECESSARY;
  471. if (!bnx2x_fill_frag_skb(bp, fp, tpa_info, pages,
  472. skb, cqe, cqe_idx)) {
  473. if (tpa_info->parsing_flags & PARSING_FLAGS_VLAN)
  474. __vlan_hwaccel_put_tag(skb, tpa_info->vlan_tag);
  475. napi_gro_receive(&fp->napi, skb);
  476. } else {
  477. DP(NETIF_MSG_RX_STATUS,
  478. "Failed to allocate new pages - dropping packet!\n");
  479. dev_kfree_skb_any(skb);
  480. }
  481. /* put new data in bin */
  482. rx_buf->data = new_data;
  483. return;
  484. }
  485. kfree(new_data);
  486. drop:
  487. /* drop the packet and keep the buffer in the bin */
  488. DP(NETIF_MSG_RX_STATUS,
  489. "Failed to allocate or map a new skb - dropping packet!\n");
  490. fp->eth_q_stats.rx_skb_alloc_failed++;
  491. }
  492. static int bnx2x_alloc_rx_data(struct bnx2x *bp,
  493. struct bnx2x_fastpath *fp, u16 index)
  494. {
  495. u8 *data;
  496. struct sw_rx_bd *rx_buf = &fp->rx_buf_ring[index];
  497. struct eth_rx_bd *rx_bd = &fp->rx_desc_ring[index];
  498. dma_addr_t mapping;
  499. data = kmalloc(fp->rx_buf_size + NET_SKB_PAD, GFP_ATOMIC);
  500. if (unlikely(data == NULL))
  501. return -ENOMEM;
  502. mapping = dma_map_single(&bp->pdev->dev, data + NET_SKB_PAD,
  503. fp->rx_buf_size,
  504. DMA_FROM_DEVICE);
  505. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  506. kfree(data);
  507. BNX2X_ERR("Can't map rx data\n");
  508. return -ENOMEM;
  509. }
  510. rx_buf->data = data;
  511. dma_unmap_addr_set(rx_buf, mapping, mapping);
  512. rx_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  513. rx_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  514. return 0;
  515. }
  516. int bnx2x_rx_int(struct bnx2x_fastpath *fp, int budget)
  517. {
  518. struct bnx2x *bp = fp->bp;
  519. u16 bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
  520. u16 hw_comp_cons, sw_comp_cons, sw_comp_prod;
  521. int rx_pkt = 0;
  522. #ifdef BNX2X_STOP_ON_ERROR
  523. if (unlikely(bp->panic))
  524. return 0;
  525. #endif
  526. /* CQ "next element" is of the size of the regular element,
  527. that's why it's ok here */
  528. hw_comp_cons = le16_to_cpu(*fp->rx_cons_sb);
  529. if ((hw_comp_cons & MAX_RCQ_DESC_CNT) == MAX_RCQ_DESC_CNT)
  530. hw_comp_cons++;
  531. bd_cons = fp->rx_bd_cons;
  532. bd_prod = fp->rx_bd_prod;
  533. bd_prod_fw = bd_prod;
  534. sw_comp_cons = fp->rx_comp_cons;
  535. sw_comp_prod = fp->rx_comp_prod;
  536. /* Memory barrier necessary as speculative reads of the rx
  537. * buffer can be ahead of the index in the status block
  538. */
  539. rmb();
  540. DP(NETIF_MSG_RX_STATUS,
  541. "queue[%d]: hw_comp_cons %u sw_comp_cons %u\n",
  542. fp->index, hw_comp_cons, sw_comp_cons);
  543. while (sw_comp_cons != hw_comp_cons) {
  544. struct sw_rx_bd *rx_buf = NULL;
  545. struct sk_buff *skb;
  546. union eth_rx_cqe *cqe;
  547. struct eth_fast_path_rx_cqe *cqe_fp;
  548. u8 cqe_fp_flags;
  549. enum eth_rx_cqe_type cqe_fp_type;
  550. u16 len, pad, queue;
  551. u8 *data;
  552. #ifdef BNX2X_STOP_ON_ERROR
  553. if (unlikely(bp->panic))
  554. return 0;
  555. #endif
  556. comp_ring_cons = RCQ_BD(sw_comp_cons);
  557. bd_prod = RX_BD(bd_prod);
  558. bd_cons = RX_BD(bd_cons);
  559. cqe = &fp->rx_comp_ring[comp_ring_cons];
  560. cqe_fp = &cqe->fast_path_cqe;
  561. cqe_fp_flags = cqe_fp->type_error_flags;
  562. cqe_fp_type = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
  563. DP(NETIF_MSG_RX_STATUS,
  564. "CQE type %x err %x status %x queue %x vlan %x len %u\n",
  565. CQE_TYPE(cqe_fp_flags),
  566. cqe_fp_flags, cqe_fp->status_flags,
  567. le32_to_cpu(cqe_fp->rss_hash_result),
  568. le16_to_cpu(cqe_fp->vlan_tag),
  569. le16_to_cpu(cqe_fp->pkt_len_or_gro_seg_len));
  570. /* is this a slowpath msg? */
  571. if (unlikely(CQE_TYPE_SLOW(cqe_fp_type))) {
  572. bnx2x_sp_event(fp, cqe);
  573. goto next_cqe;
  574. }
  575. rx_buf = &fp->rx_buf_ring[bd_cons];
  576. data = rx_buf->data;
  577. if (!CQE_TYPE_FAST(cqe_fp_type)) {
  578. struct bnx2x_agg_info *tpa_info;
  579. u16 frag_size, pages;
  580. #ifdef BNX2X_STOP_ON_ERROR
  581. /* sanity check */
  582. if (fp->disable_tpa &&
  583. (CQE_TYPE_START(cqe_fp_type) ||
  584. CQE_TYPE_STOP(cqe_fp_type)))
  585. BNX2X_ERR("START/STOP packet while disable_tpa type %x\n",
  586. CQE_TYPE(cqe_fp_type));
  587. #endif
  588. if (CQE_TYPE_START(cqe_fp_type)) {
  589. u16 queue = cqe_fp->queue_index;
  590. DP(NETIF_MSG_RX_STATUS,
  591. "calling tpa_start on queue %d\n",
  592. queue);
  593. bnx2x_tpa_start(fp, queue,
  594. bd_cons, bd_prod,
  595. cqe_fp);
  596. goto next_rx;
  597. }
  598. queue = cqe->end_agg_cqe.queue_index;
  599. tpa_info = &fp->tpa_info[queue];
  600. DP(NETIF_MSG_RX_STATUS,
  601. "calling tpa_stop on queue %d\n",
  602. queue);
  603. frag_size = le16_to_cpu(cqe->end_agg_cqe.pkt_len) -
  604. tpa_info->len_on_bd;
  605. if (fp->mode == TPA_MODE_GRO)
  606. pages = (frag_size + tpa_info->full_page - 1) /
  607. tpa_info->full_page;
  608. else
  609. pages = SGE_PAGE_ALIGN(frag_size) >>
  610. SGE_PAGE_SHIFT;
  611. bnx2x_tpa_stop(bp, fp, tpa_info, pages,
  612. &cqe->end_agg_cqe, comp_ring_cons);
  613. #ifdef BNX2X_STOP_ON_ERROR
  614. if (bp->panic)
  615. return 0;
  616. #endif
  617. bnx2x_update_sge_prod(fp, pages, &cqe->end_agg_cqe);
  618. goto next_cqe;
  619. }
  620. /* non TPA */
  621. len = le16_to_cpu(cqe_fp->pkt_len_or_gro_seg_len);
  622. pad = cqe_fp->placement_offset;
  623. dma_sync_single_for_cpu(&bp->pdev->dev,
  624. dma_unmap_addr(rx_buf, mapping),
  625. pad + RX_COPY_THRESH,
  626. DMA_FROM_DEVICE);
  627. pad += NET_SKB_PAD;
  628. prefetch(data + pad); /* speedup eth_type_trans() */
  629. /* is this an error packet? */
  630. if (unlikely(cqe_fp_flags & ETH_RX_ERROR_FALGS)) {
  631. DP(NETIF_MSG_RX_ERR | NETIF_MSG_RX_STATUS,
  632. "ERROR flags %x rx packet %u\n",
  633. cqe_fp_flags, sw_comp_cons);
  634. fp->eth_q_stats.rx_err_discard_pkt++;
  635. goto reuse_rx;
  636. }
  637. /* Since we don't have a jumbo ring
  638. * copy small packets if mtu > 1500
  639. */
  640. if ((bp->dev->mtu > ETH_MAX_PACKET_SIZE) &&
  641. (len <= RX_COPY_THRESH)) {
  642. skb = netdev_alloc_skb_ip_align(bp->dev, len);
  643. if (skb == NULL) {
  644. DP(NETIF_MSG_RX_ERR | NETIF_MSG_RX_STATUS,
  645. "ERROR packet dropped because of alloc failure\n");
  646. fp->eth_q_stats.rx_skb_alloc_failed++;
  647. goto reuse_rx;
  648. }
  649. memcpy(skb->data, data + pad, len);
  650. bnx2x_reuse_rx_data(fp, bd_cons, bd_prod);
  651. } else {
  652. if (likely(bnx2x_alloc_rx_data(bp, fp, bd_prod) == 0)) {
  653. dma_unmap_single(&bp->pdev->dev,
  654. dma_unmap_addr(rx_buf, mapping),
  655. fp->rx_buf_size,
  656. DMA_FROM_DEVICE);
  657. skb = build_skb(data, 0);
  658. if (unlikely(!skb)) {
  659. kfree(data);
  660. fp->eth_q_stats.rx_skb_alloc_failed++;
  661. goto next_rx;
  662. }
  663. skb_reserve(skb, pad);
  664. } else {
  665. DP(NETIF_MSG_RX_ERR | NETIF_MSG_RX_STATUS,
  666. "ERROR packet dropped because of alloc failure\n");
  667. fp->eth_q_stats.rx_skb_alloc_failed++;
  668. reuse_rx:
  669. bnx2x_reuse_rx_data(fp, bd_cons, bd_prod);
  670. goto next_rx;
  671. }
  672. }
  673. skb_put(skb, len);
  674. skb->protocol = eth_type_trans(skb, bp->dev);
  675. /* Set Toeplitz hash for a none-LRO skb */
  676. skb->rxhash = bnx2x_get_rxhash(bp, cqe_fp);
  677. skb_checksum_none_assert(skb);
  678. if (bp->dev->features & NETIF_F_RXCSUM) {
  679. if (likely(BNX2X_RX_CSUM_OK(cqe)))
  680. skb->ip_summed = CHECKSUM_UNNECESSARY;
  681. else
  682. fp->eth_q_stats.hw_csum_err++;
  683. }
  684. skb_record_rx_queue(skb, fp->rx_queue);
  685. if (le16_to_cpu(cqe_fp->pars_flags.flags) &
  686. PARSING_FLAGS_VLAN)
  687. __vlan_hwaccel_put_tag(skb,
  688. le16_to_cpu(cqe_fp->vlan_tag));
  689. napi_gro_receive(&fp->napi, skb);
  690. next_rx:
  691. rx_buf->data = NULL;
  692. bd_cons = NEXT_RX_IDX(bd_cons);
  693. bd_prod = NEXT_RX_IDX(bd_prod);
  694. bd_prod_fw = NEXT_RX_IDX(bd_prod_fw);
  695. rx_pkt++;
  696. next_cqe:
  697. sw_comp_prod = NEXT_RCQ_IDX(sw_comp_prod);
  698. sw_comp_cons = NEXT_RCQ_IDX(sw_comp_cons);
  699. if (rx_pkt == budget)
  700. break;
  701. } /* while */
  702. fp->rx_bd_cons = bd_cons;
  703. fp->rx_bd_prod = bd_prod_fw;
  704. fp->rx_comp_cons = sw_comp_cons;
  705. fp->rx_comp_prod = sw_comp_prod;
  706. /* Update producers */
  707. bnx2x_update_rx_prod(bp, fp, bd_prod_fw, sw_comp_prod,
  708. fp->rx_sge_prod);
  709. fp->rx_pkt += rx_pkt;
  710. fp->rx_calls++;
  711. return rx_pkt;
  712. }
  713. static irqreturn_t bnx2x_msix_fp_int(int irq, void *fp_cookie)
  714. {
  715. struct bnx2x_fastpath *fp = fp_cookie;
  716. struct bnx2x *bp = fp->bp;
  717. u8 cos;
  718. DP(NETIF_MSG_INTR,
  719. "got an MSI-X interrupt on IDX:SB [fp %d fw_sd %d igusb %d]\n",
  720. fp->index, fp->fw_sb_id, fp->igu_sb_id);
  721. bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
  722. #ifdef BNX2X_STOP_ON_ERROR
  723. if (unlikely(bp->panic))
  724. return IRQ_HANDLED;
  725. #endif
  726. /* Handle Rx and Tx according to MSI-X vector */
  727. prefetch(fp->rx_cons_sb);
  728. for_each_cos_in_tx_queue(fp, cos)
  729. prefetch(fp->txdata[cos].tx_cons_sb);
  730. prefetch(&fp->sb_running_index[SM_RX_ID]);
  731. napi_schedule(&bnx2x_fp(bp, fp->index, napi));
  732. return IRQ_HANDLED;
  733. }
  734. /* HW Lock for shared dual port PHYs */
  735. void bnx2x_acquire_phy_lock(struct bnx2x *bp)
  736. {
  737. mutex_lock(&bp->port.phy_mutex);
  738. if (bp->port.need_hw_lock)
  739. bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_MDIO);
  740. }
  741. void bnx2x_release_phy_lock(struct bnx2x *bp)
  742. {
  743. if (bp->port.need_hw_lock)
  744. bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_MDIO);
  745. mutex_unlock(&bp->port.phy_mutex);
  746. }
  747. /* calculates MF speed according to current linespeed and MF configuration */
  748. u16 bnx2x_get_mf_speed(struct bnx2x *bp)
  749. {
  750. u16 line_speed = bp->link_vars.line_speed;
  751. if (IS_MF(bp)) {
  752. u16 maxCfg = bnx2x_extract_max_cfg(bp,
  753. bp->mf_config[BP_VN(bp)]);
  754. /* Calculate the current MAX line speed limit for the MF
  755. * devices
  756. */
  757. if (IS_MF_SI(bp))
  758. line_speed = (line_speed * maxCfg) / 100;
  759. else { /* SD mode */
  760. u16 vn_max_rate = maxCfg * 100;
  761. if (vn_max_rate < line_speed)
  762. line_speed = vn_max_rate;
  763. }
  764. }
  765. return line_speed;
  766. }
  767. /**
  768. * bnx2x_fill_report_data - fill link report data to report
  769. *
  770. * @bp: driver handle
  771. * @data: link state to update
  772. *
  773. * It uses a none-atomic bit operations because is called under the mutex.
  774. */
  775. static void bnx2x_fill_report_data(struct bnx2x *bp,
  776. struct bnx2x_link_report_data *data)
  777. {
  778. u16 line_speed = bnx2x_get_mf_speed(bp);
  779. memset(data, 0, sizeof(*data));
  780. /* Fill the report data: efective line speed */
  781. data->line_speed = line_speed;
  782. /* Link is down */
  783. if (!bp->link_vars.link_up || (bp->flags & MF_FUNC_DIS))
  784. __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  785. &data->link_report_flags);
  786. /* Full DUPLEX */
  787. if (bp->link_vars.duplex == DUPLEX_FULL)
  788. __set_bit(BNX2X_LINK_REPORT_FD, &data->link_report_flags);
  789. /* Rx Flow Control is ON */
  790. if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_RX)
  791. __set_bit(BNX2X_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
  792. /* Tx Flow Control is ON */
  793. if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_TX)
  794. __set_bit(BNX2X_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
  795. }
  796. /**
  797. * bnx2x_link_report - report link status to OS.
  798. *
  799. * @bp: driver handle
  800. *
  801. * Calls the __bnx2x_link_report() under the same locking scheme
  802. * as a link/PHY state managing code to ensure a consistent link
  803. * reporting.
  804. */
  805. void bnx2x_link_report(struct bnx2x *bp)
  806. {
  807. bnx2x_acquire_phy_lock(bp);
  808. __bnx2x_link_report(bp);
  809. bnx2x_release_phy_lock(bp);
  810. }
  811. /**
  812. * __bnx2x_link_report - report link status to OS.
  813. *
  814. * @bp: driver handle
  815. *
  816. * None atomic inmlementation.
  817. * Should be called under the phy_lock.
  818. */
  819. void __bnx2x_link_report(struct bnx2x *bp)
  820. {
  821. struct bnx2x_link_report_data cur_data;
  822. /* reread mf_cfg */
  823. if (!CHIP_IS_E1(bp))
  824. bnx2x_read_mf_cfg(bp);
  825. /* Read the current link report info */
  826. bnx2x_fill_report_data(bp, &cur_data);
  827. /* Don't report link down or exactly the same link status twice */
  828. if (!memcmp(&cur_data, &bp->last_reported_link, sizeof(cur_data)) ||
  829. (test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  830. &bp->last_reported_link.link_report_flags) &&
  831. test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  832. &cur_data.link_report_flags)))
  833. return;
  834. bp->link_cnt++;
  835. /* We are going to report a new link parameters now -
  836. * remember the current data for the next time.
  837. */
  838. memcpy(&bp->last_reported_link, &cur_data, sizeof(cur_data));
  839. if (test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  840. &cur_data.link_report_flags)) {
  841. netif_carrier_off(bp->dev);
  842. netdev_err(bp->dev, "NIC Link is Down\n");
  843. return;
  844. } else {
  845. const char *duplex;
  846. const char *flow;
  847. netif_carrier_on(bp->dev);
  848. if (test_and_clear_bit(BNX2X_LINK_REPORT_FD,
  849. &cur_data.link_report_flags))
  850. duplex = "full";
  851. else
  852. duplex = "half";
  853. /* Handle the FC at the end so that only these flags would be
  854. * possibly set. This way we may easily check if there is no FC
  855. * enabled.
  856. */
  857. if (cur_data.link_report_flags) {
  858. if (test_bit(BNX2X_LINK_REPORT_RX_FC_ON,
  859. &cur_data.link_report_flags)) {
  860. if (test_bit(BNX2X_LINK_REPORT_TX_FC_ON,
  861. &cur_data.link_report_flags))
  862. flow = "ON - receive & transmit";
  863. else
  864. flow = "ON - receive";
  865. } else {
  866. flow = "ON - transmit";
  867. }
  868. } else {
  869. flow = "none";
  870. }
  871. netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
  872. cur_data.line_speed, duplex, flow);
  873. }
  874. }
  875. static void bnx2x_set_next_page_sgl(struct bnx2x_fastpath *fp)
  876. {
  877. int i;
  878. for (i = 1; i <= NUM_RX_SGE_PAGES; i++) {
  879. struct eth_rx_sge *sge;
  880. sge = &fp->rx_sge_ring[RX_SGE_CNT * i - 2];
  881. sge->addr_hi =
  882. cpu_to_le32(U64_HI(fp->rx_sge_mapping +
  883. BCM_PAGE_SIZE*(i % NUM_RX_SGE_PAGES)));
  884. sge->addr_lo =
  885. cpu_to_le32(U64_LO(fp->rx_sge_mapping +
  886. BCM_PAGE_SIZE*(i % NUM_RX_SGE_PAGES)));
  887. }
  888. }
  889. static void bnx2x_free_tpa_pool(struct bnx2x *bp,
  890. struct bnx2x_fastpath *fp, int last)
  891. {
  892. int i;
  893. for (i = 0; i < last; i++) {
  894. struct bnx2x_agg_info *tpa_info = &fp->tpa_info[i];
  895. struct sw_rx_bd *first_buf = &tpa_info->first_buf;
  896. u8 *data = first_buf->data;
  897. if (data == NULL) {
  898. DP(NETIF_MSG_IFDOWN, "tpa bin %d empty on free\n", i);
  899. continue;
  900. }
  901. if (tpa_info->tpa_state == BNX2X_TPA_START)
  902. dma_unmap_single(&bp->pdev->dev,
  903. dma_unmap_addr(first_buf, mapping),
  904. fp->rx_buf_size, DMA_FROM_DEVICE);
  905. kfree(data);
  906. first_buf->data = NULL;
  907. }
  908. }
  909. void bnx2x_init_rx_rings(struct bnx2x *bp)
  910. {
  911. int func = BP_FUNC(bp);
  912. u16 ring_prod;
  913. int i, j;
  914. /* Allocate TPA resources */
  915. for_each_rx_queue(bp, j) {
  916. struct bnx2x_fastpath *fp = &bp->fp[j];
  917. DP(NETIF_MSG_IFUP,
  918. "mtu %d rx_buf_size %d\n", bp->dev->mtu, fp->rx_buf_size);
  919. if (!fp->disable_tpa) {
  920. /* Fill the per-aggregtion pool */
  921. for (i = 0; i < MAX_AGG_QS(bp); i++) {
  922. struct bnx2x_agg_info *tpa_info =
  923. &fp->tpa_info[i];
  924. struct sw_rx_bd *first_buf =
  925. &tpa_info->first_buf;
  926. first_buf->data = kmalloc(fp->rx_buf_size + NET_SKB_PAD,
  927. GFP_ATOMIC);
  928. if (!first_buf->data) {
  929. BNX2X_ERR("Failed to allocate TPA skb pool for queue[%d] - disabling TPA on this queue!\n",
  930. j);
  931. bnx2x_free_tpa_pool(bp, fp, i);
  932. fp->disable_tpa = 1;
  933. break;
  934. }
  935. dma_unmap_addr_set(first_buf, mapping, 0);
  936. tpa_info->tpa_state = BNX2X_TPA_STOP;
  937. }
  938. /* "next page" elements initialization */
  939. bnx2x_set_next_page_sgl(fp);
  940. /* set SGEs bit mask */
  941. bnx2x_init_sge_ring_bit_mask(fp);
  942. /* Allocate SGEs and initialize the ring elements */
  943. for (i = 0, ring_prod = 0;
  944. i < MAX_RX_SGE_CNT*NUM_RX_SGE_PAGES; i++) {
  945. if (bnx2x_alloc_rx_sge(bp, fp, ring_prod) < 0) {
  946. BNX2X_ERR("was only able to allocate %d rx sges\n",
  947. i);
  948. BNX2X_ERR("disabling TPA for queue[%d]\n",
  949. j);
  950. /* Cleanup already allocated elements */
  951. bnx2x_free_rx_sge_range(bp, fp,
  952. ring_prod);
  953. bnx2x_free_tpa_pool(bp, fp,
  954. MAX_AGG_QS(bp));
  955. fp->disable_tpa = 1;
  956. ring_prod = 0;
  957. break;
  958. }
  959. ring_prod = NEXT_SGE_IDX(ring_prod);
  960. }
  961. fp->rx_sge_prod = ring_prod;
  962. }
  963. }
  964. for_each_rx_queue(bp, j) {
  965. struct bnx2x_fastpath *fp = &bp->fp[j];
  966. fp->rx_bd_cons = 0;
  967. /* Activate BD ring */
  968. /* Warning!
  969. * this will generate an interrupt (to the TSTORM)
  970. * must only be done after chip is initialized
  971. */
  972. bnx2x_update_rx_prod(bp, fp, fp->rx_bd_prod, fp->rx_comp_prod,
  973. fp->rx_sge_prod);
  974. if (j != 0)
  975. continue;
  976. if (CHIP_IS_E1(bp)) {
  977. REG_WR(bp, BAR_USTRORM_INTMEM +
  978. USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func),
  979. U64_LO(fp->rx_comp_mapping));
  980. REG_WR(bp, BAR_USTRORM_INTMEM +
  981. USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func) + 4,
  982. U64_HI(fp->rx_comp_mapping));
  983. }
  984. }
  985. }
  986. static void bnx2x_free_tx_skbs(struct bnx2x *bp)
  987. {
  988. int i;
  989. u8 cos;
  990. for_each_tx_queue(bp, i) {
  991. struct bnx2x_fastpath *fp = &bp->fp[i];
  992. for_each_cos_in_tx_queue(fp, cos) {
  993. struct bnx2x_fp_txdata *txdata = &fp->txdata[cos];
  994. unsigned pkts_compl = 0, bytes_compl = 0;
  995. u16 sw_prod = txdata->tx_pkt_prod;
  996. u16 sw_cons = txdata->tx_pkt_cons;
  997. while (sw_cons != sw_prod) {
  998. bnx2x_free_tx_pkt(bp, txdata, TX_BD(sw_cons),
  999. &pkts_compl, &bytes_compl);
  1000. sw_cons++;
  1001. }
  1002. netdev_tx_reset_queue(
  1003. netdev_get_tx_queue(bp->dev, txdata->txq_index));
  1004. }
  1005. }
  1006. }
  1007. static void bnx2x_free_rx_bds(struct bnx2x_fastpath *fp)
  1008. {
  1009. struct bnx2x *bp = fp->bp;
  1010. int i;
  1011. /* ring wasn't allocated */
  1012. if (fp->rx_buf_ring == NULL)
  1013. return;
  1014. for (i = 0; i < NUM_RX_BD; i++) {
  1015. struct sw_rx_bd *rx_buf = &fp->rx_buf_ring[i];
  1016. u8 *data = rx_buf->data;
  1017. if (data == NULL)
  1018. continue;
  1019. dma_unmap_single(&bp->pdev->dev,
  1020. dma_unmap_addr(rx_buf, mapping),
  1021. fp->rx_buf_size, DMA_FROM_DEVICE);
  1022. rx_buf->data = NULL;
  1023. kfree(data);
  1024. }
  1025. }
  1026. static void bnx2x_free_rx_skbs(struct bnx2x *bp)
  1027. {
  1028. int j;
  1029. for_each_rx_queue(bp, j) {
  1030. struct bnx2x_fastpath *fp = &bp->fp[j];
  1031. bnx2x_free_rx_bds(fp);
  1032. if (!fp->disable_tpa)
  1033. bnx2x_free_tpa_pool(bp, fp, MAX_AGG_QS(bp));
  1034. }
  1035. }
  1036. void bnx2x_free_skbs(struct bnx2x *bp)
  1037. {
  1038. bnx2x_free_tx_skbs(bp);
  1039. bnx2x_free_rx_skbs(bp);
  1040. }
  1041. void bnx2x_update_max_mf_config(struct bnx2x *bp, u32 value)
  1042. {
  1043. /* load old values */
  1044. u32 mf_cfg = bp->mf_config[BP_VN(bp)];
  1045. if (value != bnx2x_extract_max_cfg(bp, mf_cfg)) {
  1046. /* leave all but MAX value */
  1047. mf_cfg &= ~FUNC_MF_CFG_MAX_BW_MASK;
  1048. /* set new MAX value */
  1049. mf_cfg |= (value << FUNC_MF_CFG_MAX_BW_SHIFT)
  1050. & FUNC_MF_CFG_MAX_BW_MASK;
  1051. bnx2x_fw_command(bp, DRV_MSG_CODE_SET_MF_BW, mf_cfg);
  1052. }
  1053. }
  1054. /**
  1055. * bnx2x_free_msix_irqs - free previously requested MSI-X IRQ vectors
  1056. *
  1057. * @bp: driver handle
  1058. * @nvecs: number of vectors to be released
  1059. */
  1060. static void bnx2x_free_msix_irqs(struct bnx2x *bp, int nvecs)
  1061. {
  1062. int i, offset = 0;
  1063. if (nvecs == offset)
  1064. return;
  1065. free_irq(bp->msix_table[offset].vector, bp->dev);
  1066. DP(NETIF_MSG_IFDOWN, "released sp irq (%d)\n",
  1067. bp->msix_table[offset].vector);
  1068. offset++;
  1069. #ifdef BCM_CNIC
  1070. if (nvecs == offset)
  1071. return;
  1072. offset++;
  1073. #endif
  1074. for_each_eth_queue(bp, i) {
  1075. if (nvecs == offset)
  1076. return;
  1077. DP(NETIF_MSG_IFDOWN, "about to release fp #%d->%d irq\n",
  1078. i, bp->msix_table[offset].vector);
  1079. free_irq(bp->msix_table[offset++].vector, &bp->fp[i]);
  1080. }
  1081. }
  1082. void bnx2x_free_irq(struct bnx2x *bp)
  1083. {
  1084. if (bp->flags & USING_MSIX_FLAG &&
  1085. !(bp->flags & USING_SINGLE_MSIX_FLAG))
  1086. bnx2x_free_msix_irqs(bp, BNX2X_NUM_ETH_QUEUES(bp) +
  1087. CNIC_PRESENT + 1);
  1088. else
  1089. free_irq(bp->dev->irq, bp->dev);
  1090. }
  1091. int __devinit bnx2x_enable_msix(struct bnx2x *bp)
  1092. {
  1093. int msix_vec = 0, i, rc, req_cnt;
  1094. bp->msix_table[msix_vec].entry = msix_vec;
  1095. BNX2X_DEV_INFO("msix_table[0].entry = %d (slowpath)\n",
  1096. bp->msix_table[0].entry);
  1097. msix_vec++;
  1098. #ifdef BCM_CNIC
  1099. bp->msix_table[msix_vec].entry = msix_vec;
  1100. BNX2X_DEV_INFO("msix_table[%d].entry = %d (CNIC)\n",
  1101. bp->msix_table[msix_vec].entry, bp->msix_table[msix_vec].entry);
  1102. msix_vec++;
  1103. #endif
  1104. /* We need separate vectors for ETH queues only (not FCoE) */
  1105. for_each_eth_queue(bp, i) {
  1106. bp->msix_table[msix_vec].entry = msix_vec;
  1107. BNX2X_DEV_INFO("msix_table[%d].entry = %d (fastpath #%u)\n",
  1108. msix_vec, msix_vec, i);
  1109. msix_vec++;
  1110. }
  1111. req_cnt = BNX2X_NUM_ETH_QUEUES(bp) + CNIC_PRESENT + 1;
  1112. rc = pci_enable_msix(bp->pdev, &bp->msix_table[0], req_cnt);
  1113. /*
  1114. * reconfigure number of tx/rx queues according to available
  1115. * MSI-X vectors
  1116. */
  1117. if (rc >= BNX2X_MIN_MSIX_VEC_CNT) {
  1118. /* how less vectors we will have? */
  1119. int diff = req_cnt - rc;
  1120. BNX2X_DEV_INFO("Trying to use less MSI-X vectors: %d\n", rc);
  1121. rc = pci_enable_msix(bp->pdev, &bp->msix_table[0], rc);
  1122. if (rc) {
  1123. BNX2X_DEV_INFO("MSI-X is not attainable rc %d\n", rc);
  1124. goto no_msix;
  1125. }
  1126. /*
  1127. * decrease number of queues by number of unallocated entries
  1128. */
  1129. bp->num_queues -= diff;
  1130. BNX2X_DEV_INFO("New queue configuration set: %d\n",
  1131. bp->num_queues);
  1132. } else if (rc > 0) {
  1133. /* Get by with single vector */
  1134. rc = pci_enable_msix(bp->pdev, &bp->msix_table[0], 1);
  1135. if (rc) {
  1136. BNX2X_DEV_INFO("Single MSI-X is not attainable rc %d\n",
  1137. rc);
  1138. goto no_msix;
  1139. }
  1140. BNX2X_DEV_INFO("Using single MSI-X vector\n");
  1141. bp->flags |= USING_SINGLE_MSIX_FLAG;
  1142. } else if (rc < 0) {
  1143. BNX2X_DEV_INFO("MSI-X is not attainable rc %d\n", rc);
  1144. goto no_msix;
  1145. }
  1146. bp->flags |= USING_MSIX_FLAG;
  1147. return 0;
  1148. no_msix:
  1149. /* fall to INTx if not enough memory */
  1150. if (rc == -ENOMEM)
  1151. bp->flags |= DISABLE_MSI_FLAG;
  1152. return rc;
  1153. }
  1154. static int bnx2x_req_msix_irqs(struct bnx2x *bp)
  1155. {
  1156. int i, rc, offset = 0;
  1157. rc = request_irq(bp->msix_table[offset++].vector,
  1158. bnx2x_msix_sp_int, 0,
  1159. bp->dev->name, bp->dev);
  1160. if (rc) {
  1161. BNX2X_ERR("request sp irq failed\n");
  1162. return -EBUSY;
  1163. }
  1164. #ifdef BCM_CNIC
  1165. offset++;
  1166. #endif
  1167. for_each_eth_queue(bp, i) {
  1168. struct bnx2x_fastpath *fp = &bp->fp[i];
  1169. snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
  1170. bp->dev->name, i);
  1171. rc = request_irq(bp->msix_table[offset].vector,
  1172. bnx2x_msix_fp_int, 0, fp->name, fp);
  1173. if (rc) {
  1174. BNX2X_ERR("request fp #%d irq (%d) failed rc %d\n", i,
  1175. bp->msix_table[offset].vector, rc);
  1176. bnx2x_free_msix_irqs(bp, offset);
  1177. return -EBUSY;
  1178. }
  1179. offset++;
  1180. }
  1181. i = BNX2X_NUM_ETH_QUEUES(bp);
  1182. offset = 1 + CNIC_PRESENT;
  1183. netdev_info(bp->dev, "using MSI-X IRQs: sp %d fp[%d] %d ... fp[%d] %d\n",
  1184. bp->msix_table[0].vector,
  1185. 0, bp->msix_table[offset].vector,
  1186. i - 1, bp->msix_table[offset + i - 1].vector);
  1187. return 0;
  1188. }
  1189. int bnx2x_enable_msi(struct bnx2x *bp)
  1190. {
  1191. int rc;
  1192. rc = pci_enable_msi(bp->pdev);
  1193. if (rc) {
  1194. BNX2X_DEV_INFO("MSI is not attainable\n");
  1195. return -1;
  1196. }
  1197. bp->flags |= USING_MSI_FLAG;
  1198. return 0;
  1199. }
  1200. static int bnx2x_req_irq(struct bnx2x *bp)
  1201. {
  1202. unsigned long flags;
  1203. unsigned int irq;
  1204. if (bp->flags & (USING_MSI_FLAG | USING_MSIX_FLAG))
  1205. flags = 0;
  1206. else
  1207. flags = IRQF_SHARED;
  1208. if (bp->flags & USING_MSIX_FLAG)
  1209. irq = bp->msix_table[0].vector;
  1210. else
  1211. irq = bp->pdev->irq;
  1212. return request_irq(irq, bnx2x_interrupt, flags, bp->dev->name, bp->dev);
  1213. }
  1214. static int bnx2x_setup_irqs(struct bnx2x *bp)
  1215. {
  1216. int rc = 0;
  1217. if (bp->flags & USING_MSIX_FLAG &&
  1218. !(bp->flags & USING_SINGLE_MSIX_FLAG)) {
  1219. rc = bnx2x_req_msix_irqs(bp);
  1220. if (rc)
  1221. return rc;
  1222. } else {
  1223. bnx2x_ack_int(bp);
  1224. rc = bnx2x_req_irq(bp);
  1225. if (rc) {
  1226. BNX2X_ERR("IRQ request failed rc %d, aborting\n", rc);
  1227. return rc;
  1228. }
  1229. if (bp->flags & USING_MSI_FLAG) {
  1230. bp->dev->irq = bp->pdev->irq;
  1231. netdev_info(bp->dev, "using MSI IRQ %d\n",
  1232. bp->dev->irq);
  1233. }
  1234. if (bp->flags & USING_MSIX_FLAG) {
  1235. bp->dev->irq = bp->msix_table[0].vector;
  1236. netdev_info(bp->dev, "using MSIX IRQ %d\n",
  1237. bp->dev->irq);
  1238. }
  1239. }
  1240. return 0;
  1241. }
  1242. static void bnx2x_napi_enable(struct bnx2x *bp)
  1243. {
  1244. int i;
  1245. for_each_rx_queue(bp, i)
  1246. napi_enable(&bnx2x_fp(bp, i, napi));
  1247. }
  1248. static void bnx2x_napi_disable(struct bnx2x *bp)
  1249. {
  1250. int i;
  1251. for_each_rx_queue(bp, i)
  1252. napi_disable(&bnx2x_fp(bp, i, napi));
  1253. }
  1254. void bnx2x_netif_start(struct bnx2x *bp)
  1255. {
  1256. if (netif_running(bp->dev)) {
  1257. bnx2x_napi_enable(bp);
  1258. bnx2x_int_enable(bp);
  1259. if (bp->state == BNX2X_STATE_OPEN)
  1260. netif_tx_wake_all_queues(bp->dev);
  1261. }
  1262. }
  1263. void bnx2x_netif_stop(struct bnx2x *bp, int disable_hw)
  1264. {
  1265. bnx2x_int_disable_sync(bp, disable_hw);
  1266. bnx2x_napi_disable(bp);
  1267. }
  1268. u16 bnx2x_select_queue(struct net_device *dev, struct sk_buff *skb)
  1269. {
  1270. struct bnx2x *bp = netdev_priv(dev);
  1271. #ifdef BCM_CNIC
  1272. if (!NO_FCOE(bp)) {
  1273. struct ethhdr *hdr = (struct ethhdr *)skb->data;
  1274. u16 ether_type = ntohs(hdr->h_proto);
  1275. /* Skip VLAN tag if present */
  1276. if (ether_type == ETH_P_8021Q) {
  1277. struct vlan_ethhdr *vhdr =
  1278. (struct vlan_ethhdr *)skb->data;
  1279. ether_type = ntohs(vhdr->h_vlan_encapsulated_proto);
  1280. }
  1281. /* If ethertype is FCoE or FIP - use FCoE ring */
  1282. if ((ether_type == ETH_P_FCOE) || (ether_type == ETH_P_FIP))
  1283. return bnx2x_fcoe_tx(bp, txq_index);
  1284. }
  1285. #endif
  1286. /* select a non-FCoE queue */
  1287. return __skb_tx_hash(dev, skb, BNX2X_NUM_ETH_QUEUES(bp));
  1288. }
  1289. void bnx2x_set_num_queues(struct bnx2x *bp)
  1290. {
  1291. /* RSS queues */
  1292. bp->num_queues = bnx2x_calc_num_queues(bp);
  1293. #ifdef BCM_CNIC
  1294. /* override in STORAGE SD modes */
  1295. if (IS_MF_STORAGE_SD(bp) || IS_MF_FCOE_AFEX(bp))
  1296. bp->num_queues = 1;
  1297. #endif
  1298. /* Add special queues */
  1299. bp->num_queues += NON_ETH_CONTEXT_USE;
  1300. }
  1301. /**
  1302. * bnx2x_set_real_num_queues - configure netdev->real_num_[tx,rx]_queues
  1303. *
  1304. * @bp: Driver handle
  1305. *
  1306. * We currently support for at most 16 Tx queues for each CoS thus we will
  1307. * allocate a multiple of 16 for ETH L2 rings according to the value of the
  1308. * bp->max_cos.
  1309. *
  1310. * If there is an FCoE L2 queue the appropriate Tx queue will have the next
  1311. * index after all ETH L2 indices.
  1312. *
  1313. * If the actual number of Tx queues (for each CoS) is less than 16 then there
  1314. * will be the holes at the end of each group of 16 ETh L2 indices (0..15,
  1315. * 16..31,...) with indicies that are not coupled with any real Tx queue.
  1316. *
  1317. * The proper configuration of skb->queue_mapping is handled by
  1318. * bnx2x_select_queue() and __skb_tx_hash().
  1319. *
  1320. * bnx2x_setup_tc() takes care of the proper TC mappings so that __skb_tx_hash()
  1321. * will return a proper Tx index if TC is enabled (netdev->num_tc > 0).
  1322. */
  1323. static int bnx2x_set_real_num_queues(struct bnx2x *bp)
  1324. {
  1325. int rc, tx, rx;
  1326. tx = MAX_TXQS_PER_COS * bp->max_cos;
  1327. rx = BNX2X_NUM_ETH_QUEUES(bp);
  1328. /* account for fcoe queue */
  1329. #ifdef BCM_CNIC
  1330. if (!NO_FCOE(bp)) {
  1331. rx += FCOE_PRESENT;
  1332. tx += FCOE_PRESENT;
  1333. }
  1334. #endif
  1335. rc = netif_set_real_num_tx_queues(bp->dev, tx);
  1336. if (rc) {
  1337. BNX2X_ERR("Failed to set real number of Tx queues: %d\n", rc);
  1338. return rc;
  1339. }
  1340. rc = netif_set_real_num_rx_queues(bp->dev, rx);
  1341. if (rc) {
  1342. BNX2X_ERR("Failed to set real number of Rx queues: %d\n", rc);
  1343. return rc;
  1344. }
  1345. DP(NETIF_MSG_IFUP, "Setting real num queues to (tx, rx) (%d, %d)\n",
  1346. tx, rx);
  1347. return rc;
  1348. }
  1349. static void bnx2x_set_rx_buf_size(struct bnx2x *bp)
  1350. {
  1351. int i;
  1352. for_each_queue(bp, i) {
  1353. struct bnx2x_fastpath *fp = &bp->fp[i];
  1354. u32 mtu;
  1355. /* Always use a mini-jumbo MTU for the FCoE L2 ring */
  1356. if (IS_FCOE_IDX(i))
  1357. /*
  1358. * Although there are no IP frames expected to arrive to
  1359. * this ring we still want to add an
  1360. * IP_HEADER_ALIGNMENT_PADDING to prevent a buffer
  1361. * overrun attack.
  1362. */
  1363. mtu = BNX2X_FCOE_MINI_JUMBO_MTU;
  1364. else
  1365. mtu = bp->dev->mtu;
  1366. fp->rx_buf_size = BNX2X_FW_RX_ALIGN_START +
  1367. IP_HEADER_ALIGNMENT_PADDING +
  1368. ETH_OVREHEAD +
  1369. mtu +
  1370. BNX2X_FW_RX_ALIGN_END;
  1371. /* Note : rx_buf_size doesnt take into account NET_SKB_PAD */
  1372. }
  1373. }
  1374. static int bnx2x_init_rss_pf(struct bnx2x *bp)
  1375. {
  1376. int i;
  1377. u8 ind_table[T_ETH_INDIRECTION_TABLE_SIZE] = {0};
  1378. u8 num_eth_queues = BNX2X_NUM_ETH_QUEUES(bp);
  1379. /* Prepare the initial contents fo the indirection table if RSS is
  1380. * enabled
  1381. */
  1382. for (i = 0; i < sizeof(ind_table); i++)
  1383. ind_table[i] =
  1384. bp->fp->cl_id +
  1385. ethtool_rxfh_indir_default(i, num_eth_queues);
  1386. /*
  1387. * For 57710 and 57711 SEARCHER configuration (rss_keys) is
  1388. * per-port, so if explicit configuration is needed , do it only
  1389. * for a PMF.
  1390. *
  1391. * For 57712 and newer on the other hand it's a per-function
  1392. * configuration.
  1393. */
  1394. return bnx2x_config_rss_eth(bp, ind_table,
  1395. bp->port.pmf || !CHIP_IS_E1x(bp));
  1396. }
  1397. int bnx2x_config_rss_pf(struct bnx2x *bp, struct bnx2x_rss_config_obj *rss_obj,
  1398. u8 *ind_table, bool config_hash)
  1399. {
  1400. struct bnx2x_config_rss_params params = {NULL};
  1401. int i;
  1402. /* Although RSS is meaningless when there is a single HW queue we
  1403. * still need it enabled in order to have HW Rx hash generated.
  1404. *
  1405. * if (!is_eth_multi(bp))
  1406. * bp->multi_mode = ETH_RSS_MODE_DISABLED;
  1407. */
  1408. params.rss_obj = rss_obj;
  1409. __set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
  1410. __set_bit(BNX2X_RSS_MODE_REGULAR, &params.rss_flags);
  1411. /* RSS configuration */
  1412. __set_bit(BNX2X_RSS_IPV4, &params.rss_flags);
  1413. __set_bit(BNX2X_RSS_IPV4_TCP, &params.rss_flags);
  1414. __set_bit(BNX2X_RSS_IPV6, &params.rss_flags);
  1415. __set_bit(BNX2X_RSS_IPV6_TCP, &params.rss_flags);
  1416. /* Hash bits */
  1417. params.rss_result_mask = MULTI_MASK;
  1418. memcpy(params.ind_table, ind_table, sizeof(params.ind_table));
  1419. if (config_hash) {
  1420. /* RSS keys */
  1421. for (i = 0; i < sizeof(params.rss_key) / 4; i++)
  1422. params.rss_key[i] = random32();
  1423. __set_bit(BNX2X_RSS_SET_SRCH, &params.rss_flags);
  1424. }
  1425. return bnx2x_config_rss(bp, &params);
  1426. }
  1427. static int bnx2x_init_hw(struct bnx2x *bp, u32 load_code)
  1428. {
  1429. struct bnx2x_func_state_params func_params = {NULL};
  1430. /* Prepare parameters for function state transitions */
  1431. __set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
  1432. func_params.f_obj = &bp->func_obj;
  1433. func_params.cmd = BNX2X_F_CMD_HW_INIT;
  1434. func_params.params.hw_init.load_phase = load_code;
  1435. return bnx2x_func_state_change(bp, &func_params);
  1436. }
  1437. /*
  1438. * Cleans the object that have internal lists without sending
  1439. * ramrods. Should be run when interrutps are disabled.
  1440. */
  1441. static void bnx2x_squeeze_objects(struct bnx2x *bp)
  1442. {
  1443. int rc;
  1444. unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
  1445. struct bnx2x_mcast_ramrod_params rparam = {NULL};
  1446. struct bnx2x_vlan_mac_obj *mac_obj = &bp->fp->mac_obj;
  1447. /***************** Cleanup MACs' object first *************************/
  1448. /* Wait for completion of requested */
  1449. __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
  1450. /* Perform a dry cleanup */
  1451. __set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
  1452. /* Clean ETH primary MAC */
  1453. __set_bit(BNX2X_ETH_MAC, &vlan_mac_flags);
  1454. rc = mac_obj->delete_all(bp, &bp->fp->mac_obj, &vlan_mac_flags,
  1455. &ramrod_flags);
  1456. if (rc != 0)
  1457. BNX2X_ERR("Failed to clean ETH MACs: %d\n", rc);
  1458. /* Cleanup UC list */
  1459. vlan_mac_flags = 0;
  1460. __set_bit(BNX2X_UC_LIST_MAC, &vlan_mac_flags);
  1461. rc = mac_obj->delete_all(bp, mac_obj, &vlan_mac_flags,
  1462. &ramrod_flags);
  1463. if (rc != 0)
  1464. BNX2X_ERR("Failed to clean UC list MACs: %d\n", rc);
  1465. /***************** Now clean mcast object *****************************/
  1466. rparam.mcast_obj = &bp->mcast_obj;
  1467. __set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
  1468. /* Add a DEL command... */
  1469. rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_DEL);
  1470. if (rc < 0)
  1471. BNX2X_ERR("Failed to add a new DEL command to a multi-cast object: %d\n",
  1472. rc);
  1473. /* ...and wait until all pending commands are cleared */
  1474. rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
  1475. while (rc != 0) {
  1476. if (rc < 0) {
  1477. BNX2X_ERR("Failed to clean multi-cast object: %d\n",
  1478. rc);
  1479. return;
  1480. }
  1481. rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
  1482. }
  1483. }
  1484. #ifndef BNX2X_STOP_ON_ERROR
  1485. #define LOAD_ERROR_EXIT(bp, label) \
  1486. do { \
  1487. (bp)->state = BNX2X_STATE_ERROR; \
  1488. goto label; \
  1489. } while (0)
  1490. #else
  1491. #define LOAD_ERROR_EXIT(bp, label) \
  1492. do { \
  1493. (bp)->state = BNX2X_STATE_ERROR; \
  1494. (bp)->panic = 1; \
  1495. return -EBUSY; \
  1496. } while (0)
  1497. #endif
  1498. bool bnx2x_test_firmware_version(struct bnx2x *bp, bool is_err)
  1499. {
  1500. /* build FW version dword */
  1501. u32 my_fw = (BCM_5710_FW_MAJOR_VERSION) +
  1502. (BCM_5710_FW_MINOR_VERSION << 8) +
  1503. (BCM_5710_FW_REVISION_VERSION << 16) +
  1504. (BCM_5710_FW_ENGINEERING_VERSION << 24);
  1505. /* read loaded FW from chip */
  1506. u32 loaded_fw = REG_RD(bp, XSEM_REG_PRAM);
  1507. DP(NETIF_MSG_IFUP, "loaded fw %x, my fw %x\n", loaded_fw, my_fw);
  1508. if (loaded_fw != my_fw) {
  1509. if (is_err)
  1510. BNX2X_ERR("bnx2x with FW %x was already loaded, which mismatches my %x FW. aborting\n",
  1511. loaded_fw, my_fw);
  1512. return false;
  1513. }
  1514. return true;
  1515. }
  1516. /**
  1517. * bnx2x_bz_fp - zero content of the fastpath structure.
  1518. *
  1519. * @bp: driver handle
  1520. * @index: fastpath index to be zeroed
  1521. *
  1522. * Makes sure the contents of the bp->fp[index].napi is kept
  1523. * intact.
  1524. */
  1525. static void bnx2x_bz_fp(struct bnx2x *bp, int index)
  1526. {
  1527. struct bnx2x_fastpath *fp = &bp->fp[index];
  1528. struct napi_struct orig_napi = fp->napi;
  1529. /* bzero bnx2x_fastpath contents */
  1530. if (bp->stats_init)
  1531. memset(fp, 0, sizeof(*fp));
  1532. else {
  1533. /* Keep Queue statistics */
  1534. struct bnx2x_eth_q_stats *tmp_eth_q_stats;
  1535. struct bnx2x_eth_q_stats_old *tmp_eth_q_stats_old;
  1536. tmp_eth_q_stats = kzalloc(sizeof(struct bnx2x_eth_q_stats),
  1537. GFP_KERNEL);
  1538. if (tmp_eth_q_stats)
  1539. memcpy(tmp_eth_q_stats, &fp->eth_q_stats,
  1540. sizeof(struct bnx2x_eth_q_stats));
  1541. tmp_eth_q_stats_old =
  1542. kzalloc(sizeof(struct bnx2x_eth_q_stats_old),
  1543. GFP_KERNEL);
  1544. if (tmp_eth_q_stats_old)
  1545. memcpy(tmp_eth_q_stats_old, &fp->eth_q_stats_old,
  1546. sizeof(struct bnx2x_eth_q_stats_old));
  1547. memset(fp, 0, sizeof(*fp));
  1548. if (tmp_eth_q_stats) {
  1549. memcpy(&fp->eth_q_stats, tmp_eth_q_stats,
  1550. sizeof(struct bnx2x_eth_q_stats));
  1551. kfree(tmp_eth_q_stats);
  1552. }
  1553. if (tmp_eth_q_stats_old) {
  1554. memcpy(&fp->eth_q_stats_old, tmp_eth_q_stats_old,
  1555. sizeof(struct bnx2x_eth_q_stats_old));
  1556. kfree(tmp_eth_q_stats_old);
  1557. }
  1558. }
  1559. /* Restore the NAPI object as it has been already initialized */
  1560. fp->napi = orig_napi;
  1561. fp->bp = bp;
  1562. fp->index = index;
  1563. if (IS_ETH_FP(fp))
  1564. fp->max_cos = bp->max_cos;
  1565. else
  1566. /* Special queues support only one CoS */
  1567. fp->max_cos = 1;
  1568. /*
  1569. * set the tpa flag for each queue. The tpa flag determines the queue
  1570. * minimal size so it must be set prior to queue memory allocation
  1571. */
  1572. fp->disable_tpa = !(bp->flags & TPA_ENABLE_FLAG ||
  1573. (bp->flags & GRO_ENABLE_FLAG &&
  1574. bnx2x_mtu_allows_gro(bp->dev->mtu)));
  1575. if (bp->flags & TPA_ENABLE_FLAG)
  1576. fp->mode = TPA_MODE_LRO;
  1577. else if (bp->flags & GRO_ENABLE_FLAG)
  1578. fp->mode = TPA_MODE_GRO;
  1579. #ifdef BCM_CNIC
  1580. /* We don't want TPA on an FCoE L2 ring */
  1581. if (IS_FCOE_FP(fp))
  1582. fp->disable_tpa = 1;
  1583. #endif
  1584. }
  1585. /* must be called with rtnl_lock */
  1586. int bnx2x_nic_load(struct bnx2x *bp, int load_mode)
  1587. {
  1588. int port = BP_PORT(bp);
  1589. u32 load_code;
  1590. int i, rc;
  1591. #ifdef BNX2X_STOP_ON_ERROR
  1592. if (unlikely(bp->panic)) {
  1593. BNX2X_ERR("Can't load NIC when there is panic\n");
  1594. return -EPERM;
  1595. }
  1596. #endif
  1597. bp->state = BNX2X_STATE_OPENING_WAIT4_LOAD;
  1598. /* Set the initial link reported state to link down */
  1599. bnx2x_acquire_phy_lock(bp);
  1600. memset(&bp->last_reported_link, 0, sizeof(bp->last_reported_link));
  1601. __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
  1602. &bp->last_reported_link.link_report_flags);
  1603. bnx2x_release_phy_lock(bp);
  1604. /* must be called before memory allocation and HW init */
  1605. bnx2x_ilt_set_info(bp);
  1606. /*
  1607. * Zero fastpath structures preserving invariants like napi, which are
  1608. * allocated only once, fp index, max_cos, bp pointer.
  1609. * Also set fp->disable_tpa.
  1610. */
  1611. DP(NETIF_MSG_IFUP, "num queues: %d", bp->num_queues);
  1612. for_each_queue(bp, i)
  1613. bnx2x_bz_fp(bp, i);
  1614. /* Set the receive queues buffer size */
  1615. bnx2x_set_rx_buf_size(bp);
  1616. if (bnx2x_alloc_mem(bp))
  1617. return -ENOMEM;
  1618. /* As long as bnx2x_alloc_mem() may possibly update
  1619. * bp->num_queues, bnx2x_set_real_num_queues() should always
  1620. * come after it.
  1621. */
  1622. rc = bnx2x_set_real_num_queues(bp);
  1623. if (rc) {
  1624. BNX2X_ERR("Unable to set real_num_queues\n");
  1625. LOAD_ERROR_EXIT(bp, load_error0);
  1626. }
  1627. /* configure multi cos mappings in kernel.
  1628. * this configuration may be overriden by a multi class queue discipline
  1629. * or by a dcbx negotiation result.
  1630. */
  1631. bnx2x_setup_tc(bp->dev, bp->max_cos);
  1632. bnx2x_napi_enable(bp);
  1633. /* set pf load just before approaching the MCP */
  1634. bnx2x_set_pf_load(bp);
  1635. /* Send LOAD_REQUEST command to MCP
  1636. * Returns the type of LOAD command:
  1637. * if it is the first port to be initialized
  1638. * common blocks should be initialized, otherwise - not
  1639. */
  1640. if (!BP_NOMCP(bp)) {
  1641. /* init fw_seq */
  1642. bp->fw_seq =
  1643. (SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_mb_header) &
  1644. DRV_MSG_SEQ_NUMBER_MASK);
  1645. BNX2X_DEV_INFO("fw_seq 0x%08x\n", bp->fw_seq);
  1646. /* Get current FW pulse sequence */
  1647. bp->fw_drv_pulse_wr_seq =
  1648. (SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_pulse_mb) &
  1649. DRV_PULSE_SEQ_MASK);
  1650. BNX2X_DEV_INFO("drv_pulse 0x%x\n", bp->fw_drv_pulse_wr_seq);
  1651. load_code = bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_REQ, 0);
  1652. if (!load_code) {
  1653. BNX2X_ERR("MCP response failure, aborting\n");
  1654. rc = -EBUSY;
  1655. LOAD_ERROR_EXIT(bp, load_error1);
  1656. }
  1657. if (load_code == FW_MSG_CODE_DRV_LOAD_REFUSED) {
  1658. BNX2X_ERR("Driver load refused\n");
  1659. rc = -EBUSY; /* other port in diagnostic mode */
  1660. LOAD_ERROR_EXIT(bp, load_error1);
  1661. }
  1662. if (load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP &&
  1663. load_code != FW_MSG_CODE_DRV_LOAD_COMMON) {
  1664. /* abort nic load if version mismatch */
  1665. if (!bnx2x_test_firmware_version(bp, true)) {
  1666. rc = -EBUSY;
  1667. LOAD_ERROR_EXIT(bp, load_error2);
  1668. }
  1669. }
  1670. } else {
  1671. int path = BP_PATH(bp);
  1672. DP(NETIF_MSG_IFUP, "NO MCP - load counts[%d] %d, %d, %d\n",
  1673. path, load_count[path][0], load_count[path][1],
  1674. load_count[path][2]);
  1675. load_count[path][0]++;
  1676. load_count[path][1 + port]++;
  1677. DP(NETIF_MSG_IFUP, "NO MCP - new load counts[%d] %d, %d, %d\n",
  1678. path, load_count[path][0], load_count[path][1],
  1679. load_count[path][2]);
  1680. if (load_count[path][0] == 1)
  1681. load_code = FW_MSG_CODE_DRV_LOAD_COMMON;
  1682. else if (load_count[path][1 + port] == 1)
  1683. load_code = FW_MSG_CODE_DRV_LOAD_PORT;
  1684. else
  1685. load_code = FW_MSG_CODE_DRV_LOAD_FUNCTION;
  1686. }
  1687. if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
  1688. (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
  1689. (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
  1690. bp->port.pmf = 1;
  1691. /*
  1692. * We need the barrier to ensure the ordering between the
  1693. * writing to bp->port.pmf here and reading it from the
  1694. * bnx2x_periodic_task().
  1695. */
  1696. smp_mb();
  1697. } else
  1698. bp->port.pmf = 0;
  1699. DP(NETIF_MSG_IFUP, "pmf %d\n", bp->port.pmf);
  1700. /* Init Function state controlling object */
  1701. bnx2x__init_func_obj(bp);
  1702. /* Initialize HW */
  1703. rc = bnx2x_init_hw(bp, load_code);
  1704. if (rc) {
  1705. BNX2X_ERR("HW init failed, aborting\n");
  1706. bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
  1707. LOAD_ERROR_EXIT(bp, load_error2);
  1708. }
  1709. /* Connect to IRQs */
  1710. rc = bnx2x_setup_irqs(bp);
  1711. if (rc) {
  1712. BNX2X_ERR("IRQs setup failed\n");
  1713. bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
  1714. LOAD_ERROR_EXIT(bp, load_error2);
  1715. }
  1716. /* Setup NIC internals and enable interrupts */
  1717. bnx2x_nic_init(bp, load_code);
  1718. /* Init per-function objects */
  1719. bnx2x_init_bp_objs(bp);
  1720. if (((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
  1721. (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP)) &&
  1722. (bp->common.shmem2_base)) {
  1723. if (SHMEM2_HAS(bp, dcc_support))
  1724. SHMEM2_WR(bp, dcc_support,
  1725. (SHMEM_DCC_SUPPORT_DISABLE_ENABLE_PF_TLV |
  1726. SHMEM_DCC_SUPPORT_BANDWIDTH_ALLOCATION_TLV));
  1727. if (SHMEM2_HAS(bp, afex_driver_support))
  1728. SHMEM2_WR(bp, afex_driver_support,
  1729. SHMEM_AFEX_SUPPORTED_VERSION_ONE);
  1730. }
  1731. /* Set AFEX default VLAN tag to an invalid value */
  1732. bp->afex_def_vlan_tag = -1;
  1733. bp->state = BNX2X_STATE_OPENING_WAIT4_PORT;
  1734. rc = bnx2x_func_start(bp);
  1735. if (rc) {
  1736. BNX2X_ERR("Function start failed!\n");
  1737. bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
  1738. LOAD_ERROR_EXIT(bp, load_error3);
  1739. }
  1740. /* Send LOAD_DONE command to MCP */
  1741. if (!BP_NOMCP(bp)) {
  1742. load_code = bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
  1743. if (!load_code) {
  1744. BNX2X_ERR("MCP response failure, aborting\n");
  1745. rc = -EBUSY;
  1746. LOAD_ERROR_EXIT(bp, load_error3);
  1747. }
  1748. }
  1749. rc = bnx2x_setup_leading(bp);
  1750. if (rc) {
  1751. BNX2X_ERR("Setup leading failed!\n");
  1752. LOAD_ERROR_EXIT(bp, load_error3);
  1753. }
  1754. #ifdef BCM_CNIC
  1755. /* Enable Timer scan */
  1756. REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 1);
  1757. #endif
  1758. for_each_nondefault_queue(bp, i) {
  1759. rc = bnx2x_setup_queue(bp, &bp->fp[i], 0);
  1760. if (rc) {
  1761. BNX2X_ERR("Queue setup failed\n");
  1762. LOAD_ERROR_EXIT(bp, load_error4);
  1763. }
  1764. }
  1765. rc = bnx2x_init_rss_pf(bp);
  1766. if (rc) {
  1767. BNX2X_ERR("PF RSS init failed\n");
  1768. LOAD_ERROR_EXIT(bp, load_error4);
  1769. }
  1770. /* Now when Clients are configured we are ready to work */
  1771. bp->state = BNX2X_STATE_OPEN;
  1772. /* Configure a ucast MAC */
  1773. rc = bnx2x_set_eth_mac(bp, true);
  1774. if (rc) {
  1775. BNX2X_ERR("Setting Ethernet MAC failed\n");
  1776. LOAD_ERROR_EXIT(bp, load_error4);
  1777. }
  1778. if (bp->pending_max) {
  1779. bnx2x_update_max_mf_config(bp, bp->pending_max);
  1780. bp->pending_max = 0;
  1781. }
  1782. if (bp->port.pmf)
  1783. bnx2x_initial_phy_init(bp, load_mode);
  1784. /* Start fast path */
  1785. /* Initialize Rx filter. */
  1786. netif_addr_lock_bh(bp->dev);
  1787. bnx2x_set_rx_mode(bp->dev);
  1788. netif_addr_unlock_bh(bp->dev);
  1789. /* Start the Tx */
  1790. switch (load_mode) {
  1791. case LOAD_NORMAL:
  1792. /* Tx queue should be only reenabled */
  1793. netif_tx_wake_all_queues(bp->dev);
  1794. break;
  1795. case LOAD_OPEN:
  1796. netif_tx_start_all_queues(bp->dev);
  1797. smp_mb__after_clear_bit();
  1798. break;
  1799. case LOAD_DIAG:
  1800. bp->state = BNX2X_STATE_DIAG;
  1801. break;
  1802. default:
  1803. break;
  1804. }
  1805. if (bp->port.pmf)
  1806. bnx2x_update_drv_flags(bp, 1 << DRV_FLAGS_DCB_CONFIGURED, 0);
  1807. else
  1808. bnx2x__link_status_update(bp);
  1809. /* start the timer */
  1810. mod_timer(&bp->timer, jiffies + bp->current_interval);
  1811. #ifdef BCM_CNIC
  1812. /* re-read iscsi info */
  1813. bnx2x_get_iscsi_info(bp);
  1814. bnx2x_setup_cnic_irq_info(bp);
  1815. if (bp->state == BNX2X_STATE_OPEN)
  1816. bnx2x_cnic_notify(bp, CNIC_CTL_START_CMD);
  1817. #endif
  1818. /* mark driver is loaded in shmem2 */
  1819. if (SHMEM2_HAS(bp, drv_capabilities_flag)) {
  1820. u32 val;
  1821. val = SHMEM2_RD(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)]);
  1822. SHMEM2_WR(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)],
  1823. val | DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
  1824. DRV_FLAGS_CAPABILITIES_LOADED_L2);
  1825. }
  1826. /* Wait for all pending SP commands to complete */
  1827. if (!bnx2x_wait_sp_comp(bp, ~0x0UL)) {
  1828. BNX2X_ERR("Timeout waiting for SP elements to complete\n");
  1829. bnx2x_nic_unload(bp, UNLOAD_CLOSE);
  1830. return -EBUSY;
  1831. }
  1832. bnx2x_dcbx_init(bp);
  1833. return 0;
  1834. #ifndef BNX2X_STOP_ON_ERROR
  1835. load_error4:
  1836. #ifdef BCM_CNIC
  1837. /* Disable Timer scan */
  1838. REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
  1839. #endif
  1840. load_error3:
  1841. bnx2x_int_disable_sync(bp, 1);
  1842. /* Clean queueable objects */
  1843. bnx2x_squeeze_objects(bp);
  1844. /* Free SKBs, SGEs, TPA pool and driver internals */
  1845. bnx2x_free_skbs(bp);
  1846. for_each_rx_queue(bp, i)
  1847. bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
  1848. /* Release IRQs */
  1849. bnx2x_free_irq(bp);
  1850. load_error2:
  1851. if (!BP_NOMCP(bp)) {
  1852. bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
  1853. bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_DONE, 0);
  1854. }
  1855. bp->port.pmf = 0;
  1856. load_error1:
  1857. bnx2x_napi_disable(bp);
  1858. /* clear pf_load status, as it was already set */
  1859. bnx2x_clear_pf_load(bp);
  1860. load_error0:
  1861. bnx2x_free_mem(bp);
  1862. return rc;
  1863. #endif /* ! BNX2X_STOP_ON_ERROR */
  1864. }
  1865. /* must be called with rtnl_lock */
  1866. int bnx2x_nic_unload(struct bnx2x *bp, int unload_mode)
  1867. {
  1868. int i;
  1869. bool global = false;
  1870. /* mark driver is unloaded in shmem2 */
  1871. if (SHMEM2_HAS(bp, drv_capabilities_flag)) {
  1872. u32 val;
  1873. val = SHMEM2_RD(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)]);
  1874. SHMEM2_WR(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)],
  1875. val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
  1876. }
  1877. if ((bp->state == BNX2X_STATE_CLOSED) ||
  1878. (bp->state == BNX2X_STATE_ERROR)) {
  1879. /* We can get here if the driver has been unloaded
  1880. * during parity error recovery and is either waiting for a
  1881. * leader to complete or for other functions to unload and
  1882. * then ifdown has been issued. In this case we want to
  1883. * unload and let other functions to complete a recovery
  1884. * process.
  1885. */
  1886. bp->recovery_state = BNX2X_RECOVERY_DONE;
  1887. bp->is_leader = 0;
  1888. bnx2x_release_leader_lock(bp);
  1889. smp_mb();
  1890. DP(NETIF_MSG_IFDOWN, "Releasing a leadership...\n");
  1891. BNX2X_ERR("Can't unload in closed or error state\n");
  1892. return -EINVAL;
  1893. }
  1894. /*
  1895. * It's important to set the bp->state to the value different from
  1896. * BNX2X_STATE_OPEN and only then stop the Tx. Otherwise bnx2x_tx_int()
  1897. * may restart the Tx from the NAPI context (see bnx2x_tx_int()).
  1898. */
  1899. bp->state = BNX2X_STATE_CLOSING_WAIT4_HALT;
  1900. smp_mb();
  1901. /* Stop Tx */
  1902. bnx2x_tx_disable(bp);
  1903. #ifdef BCM_CNIC
  1904. bnx2x_cnic_notify(bp, CNIC_CTL_STOP_CMD);
  1905. #endif
  1906. bp->rx_mode = BNX2X_RX_MODE_NONE;
  1907. del_timer_sync(&bp->timer);
  1908. /* Set ALWAYS_ALIVE bit in shmem */
  1909. bp->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
  1910. bnx2x_drv_pulse(bp);
  1911. bnx2x_stats_handle(bp, STATS_EVENT_STOP);
  1912. bnx2x_save_statistics(bp);
  1913. /* Cleanup the chip if needed */
  1914. if (unload_mode != UNLOAD_RECOVERY)
  1915. bnx2x_chip_cleanup(bp, unload_mode);
  1916. else {
  1917. /* Send the UNLOAD_REQUEST to the MCP */
  1918. bnx2x_send_unload_req(bp, unload_mode);
  1919. /*
  1920. * Prevent transactions to host from the functions on the
  1921. * engine that doesn't reset global blocks in case of global
  1922. * attention once gloabl blocks are reset and gates are opened
  1923. * (the engine which leader will perform the recovery
  1924. * last).
  1925. */
  1926. if (!CHIP_IS_E1x(bp))
  1927. bnx2x_pf_disable(bp);
  1928. /* Disable HW interrupts, NAPI */
  1929. bnx2x_netif_stop(bp, 1);
  1930. /* Release IRQs */
  1931. bnx2x_free_irq(bp);
  1932. /* Report UNLOAD_DONE to MCP */
  1933. bnx2x_send_unload_done(bp);
  1934. }
  1935. /*
  1936. * At this stage no more interrupts will arrive so we may safly clean
  1937. * the queueable objects here in case they failed to get cleaned so far.
  1938. */
  1939. bnx2x_squeeze_objects(bp);
  1940. /* There should be no more pending SP commands at this stage */
  1941. bp->sp_state = 0;
  1942. bp->port.pmf = 0;
  1943. /* Free SKBs, SGEs, TPA pool and driver internals */
  1944. bnx2x_free_skbs(bp);
  1945. for_each_rx_queue(bp, i)
  1946. bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
  1947. bnx2x_free_mem(bp);
  1948. bp->state = BNX2X_STATE_CLOSED;
  1949. /* Check if there are pending parity attentions. If there are - set
  1950. * RECOVERY_IN_PROGRESS.
  1951. */
  1952. if (bnx2x_chk_parity_attn(bp, &global, false)) {
  1953. bnx2x_set_reset_in_progress(bp);
  1954. /* Set RESET_IS_GLOBAL if needed */
  1955. if (global)
  1956. bnx2x_set_reset_global(bp);
  1957. }
  1958. /* The last driver must disable a "close the gate" if there is no
  1959. * parity attention or "process kill" pending.
  1960. */
  1961. if (!bnx2x_clear_pf_load(bp) && bnx2x_reset_is_done(bp, BP_PATH(bp)))
  1962. bnx2x_disable_close_the_gate(bp);
  1963. return 0;
  1964. }
  1965. int bnx2x_set_power_state(struct bnx2x *bp, pci_power_t state)
  1966. {
  1967. u16 pmcsr;
  1968. /* If there is no power capability, silently succeed */
  1969. if (!bp->pm_cap) {
  1970. BNX2X_DEV_INFO("No power capability. Breaking.\n");
  1971. return 0;
  1972. }
  1973. pci_read_config_word(bp->pdev, bp->pm_cap + PCI_PM_CTRL, &pmcsr);
  1974. switch (state) {
  1975. case PCI_D0:
  1976. pci_write_config_word(bp->pdev, bp->pm_cap + PCI_PM_CTRL,
  1977. ((pmcsr & ~PCI_PM_CTRL_STATE_MASK) |
  1978. PCI_PM_CTRL_PME_STATUS));
  1979. if (pmcsr & PCI_PM_CTRL_STATE_MASK)
  1980. /* delay required during transition out of D3hot */
  1981. msleep(20);
  1982. break;
  1983. case PCI_D3hot:
  1984. /* If there are other clients above don't
  1985. shut down the power */
  1986. if (atomic_read(&bp->pdev->enable_cnt) != 1)
  1987. return 0;
  1988. /* Don't shut down the power for emulation and FPGA */
  1989. if (CHIP_REV_IS_SLOW(bp))
  1990. return 0;
  1991. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  1992. pmcsr |= 3;
  1993. if (bp->wol)
  1994. pmcsr |= PCI_PM_CTRL_PME_ENABLE;
  1995. pci_write_config_word(bp->pdev, bp->pm_cap + PCI_PM_CTRL,
  1996. pmcsr);
  1997. /* No more memory access after this point until
  1998. * device is brought back to D0.
  1999. */
  2000. break;
  2001. default:
  2002. dev_err(&bp->pdev->dev, "Can't support state = %d\n", state);
  2003. return -EINVAL;
  2004. }
  2005. return 0;
  2006. }
  2007. /*
  2008. * net_device service functions
  2009. */
  2010. int bnx2x_poll(struct napi_struct *napi, int budget)
  2011. {
  2012. int work_done = 0;
  2013. u8 cos;
  2014. struct bnx2x_fastpath *fp = container_of(napi, struct bnx2x_fastpath,
  2015. napi);
  2016. struct bnx2x *bp = fp->bp;
  2017. while (1) {
  2018. #ifdef BNX2X_STOP_ON_ERROR
  2019. if (unlikely(bp->panic)) {
  2020. napi_complete(napi);
  2021. return 0;
  2022. }
  2023. #endif
  2024. for_each_cos_in_tx_queue(fp, cos)
  2025. if (bnx2x_tx_queue_has_work(&fp->txdata[cos]))
  2026. bnx2x_tx_int(bp, &fp->txdata[cos]);
  2027. if (bnx2x_has_rx_work(fp)) {
  2028. work_done += bnx2x_rx_int(fp, budget - work_done);
  2029. /* must not complete if we consumed full budget */
  2030. if (work_done >= budget)
  2031. break;
  2032. }
  2033. /* Fall out from the NAPI loop if needed */
  2034. if (!(bnx2x_has_rx_work(fp) || bnx2x_has_tx_work(fp))) {
  2035. #ifdef BCM_CNIC
  2036. /* No need to update SB for FCoE L2 ring as long as
  2037. * it's connected to the default SB and the SB
  2038. * has been updated when NAPI was scheduled.
  2039. */
  2040. if (IS_FCOE_FP(fp)) {
  2041. napi_complete(napi);
  2042. break;
  2043. }
  2044. #endif
  2045. bnx2x_update_fpsb_idx(fp);
  2046. /* bnx2x_has_rx_work() reads the status block,
  2047. * thus we need to ensure that status block indices
  2048. * have been actually read (bnx2x_update_fpsb_idx)
  2049. * prior to this check (bnx2x_has_rx_work) so that
  2050. * we won't write the "newer" value of the status block
  2051. * to IGU (if there was a DMA right after
  2052. * bnx2x_has_rx_work and if there is no rmb, the memory
  2053. * reading (bnx2x_update_fpsb_idx) may be postponed
  2054. * to right before bnx2x_ack_sb). In this case there
  2055. * will never be another interrupt until there is
  2056. * another update of the status block, while there
  2057. * is still unhandled work.
  2058. */
  2059. rmb();
  2060. if (!(bnx2x_has_rx_work(fp) || bnx2x_has_tx_work(fp))) {
  2061. napi_complete(napi);
  2062. /* Re-enable interrupts */
  2063. DP(NETIF_MSG_RX_STATUS,
  2064. "Update index to %d\n", fp->fp_hc_idx);
  2065. bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID,
  2066. le16_to_cpu(fp->fp_hc_idx),
  2067. IGU_INT_ENABLE, 1);
  2068. break;
  2069. }
  2070. }
  2071. }
  2072. return work_done;
  2073. }
  2074. /* we split the first BD into headers and data BDs
  2075. * to ease the pain of our fellow microcode engineers
  2076. * we use one mapping for both BDs
  2077. * So far this has only been observed to happen
  2078. * in Other Operating Systems(TM)
  2079. */
  2080. static noinline u16 bnx2x_tx_split(struct bnx2x *bp,
  2081. struct bnx2x_fp_txdata *txdata,
  2082. struct sw_tx_bd *tx_buf,
  2083. struct eth_tx_start_bd **tx_bd, u16 hlen,
  2084. u16 bd_prod, int nbd)
  2085. {
  2086. struct eth_tx_start_bd *h_tx_bd = *tx_bd;
  2087. struct eth_tx_bd *d_tx_bd;
  2088. dma_addr_t mapping;
  2089. int old_len = le16_to_cpu(h_tx_bd->nbytes);
  2090. /* first fix first BD */
  2091. h_tx_bd->nbd = cpu_to_le16(nbd);
  2092. h_tx_bd->nbytes = cpu_to_le16(hlen);
  2093. DP(NETIF_MSG_TX_QUEUED, "TSO split header size is %d (%x:%x) nbd %d\n",
  2094. h_tx_bd->nbytes, h_tx_bd->addr_hi, h_tx_bd->addr_lo, h_tx_bd->nbd);
  2095. /* now get a new data BD
  2096. * (after the pbd) and fill it */
  2097. bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
  2098. d_tx_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
  2099. mapping = HILO_U64(le32_to_cpu(h_tx_bd->addr_hi),
  2100. le32_to_cpu(h_tx_bd->addr_lo)) + hlen;
  2101. d_tx_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  2102. d_tx_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  2103. d_tx_bd->nbytes = cpu_to_le16(old_len - hlen);
  2104. /* this marks the BD as one that has no individual mapping */
  2105. tx_buf->flags |= BNX2X_TSO_SPLIT_BD;
  2106. DP(NETIF_MSG_TX_QUEUED,
  2107. "TSO split data size is %d (%x:%x)\n",
  2108. d_tx_bd->nbytes, d_tx_bd->addr_hi, d_tx_bd->addr_lo);
  2109. /* update tx_bd */
  2110. *tx_bd = (struct eth_tx_start_bd *)d_tx_bd;
  2111. return bd_prod;
  2112. }
  2113. static inline u16 bnx2x_csum_fix(unsigned char *t_header, u16 csum, s8 fix)
  2114. {
  2115. if (fix > 0)
  2116. csum = (u16) ~csum_fold(csum_sub(csum,
  2117. csum_partial(t_header - fix, fix, 0)));
  2118. else if (fix < 0)
  2119. csum = (u16) ~csum_fold(csum_add(csum,
  2120. csum_partial(t_header, -fix, 0)));
  2121. return swab16(csum);
  2122. }
  2123. static inline u32 bnx2x_xmit_type(struct bnx2x *bp, struct sk_buff *skb)
  2124. {
  2125. u32 rc;
  2126. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2127. rc = XMIT_PLAIN;
  2128. else {
  2129. if (vlan_get_protocol(skb) == htons(ETH_P_IPV6)) {
  2130. rc = XMIT_CSUM_V6;
  2131. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  2132. rc |= XMIT_CSUM_TCP;
  2133. } else {
  2134. rc = XMIT_CSUM_V4;
  2135. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  2136. rc |= XMIT_CSUM_TCP;
  2137. }
  2138. }
  2139. if (skb_is_gso_v6(skb))
  2140. rc |= XMIT_GSO_V6 | XMIT_CSUM_TCP | XMIT_CSUM_V6;
  2141. else if (skb_is_gso(skb))
  2142. rc |= XMIT_GSO_V4 | XMIT_CSUM_V4 | XMIT_CSUM_TCP;
  2143. return rc;
  2144. }
  2145. #if (MAX_SKB_FRAGS >= MAX_FETCH_BD - 3)
  2146. /* check if packet requires linearization (packet is too fragmented)
  2147. no need to check fragmentation if page size > 8K (there will be no
  2148. violation to FW restrictions) */
  2149. static int bnx2x_pkt_req_lin(struct bnx2x *bp, struct sk_buff *skb,
  2150. u32 xmit_type)
  2151. {
  2152. int to_copy = 0;
  2153. int hlen = 0;
  2154. int first_bd_sz = 0;
  2155. /* 3 = 1 (for linear data BD) + 2 (for PBD and last BD) */
  2156. if (skb_shinfo(skb)->nr_frags >= (MAX_FETCH_BD - 3)) {
  2157. if (xmit_type & XMIT_GSO) {
  2158. unsigned short lso_mss = skb_shinfo(skb)->gso_size;
  2159. /* Check if LSO packet needs to be copied:
  2160. 3 = 1 (for headers BD) + 2 (for PBD and last BD) */
  2161. int wnd_size = MAX_FETCH_BD - 3;
  2162. /* Number of windows to check */
  2163. int num_wnds = skb_shinfo(skb)->nr_frags - wnd_size;
  2164. int wnd_idx = 0;
  2165. int frag_idx = 0;
  2166. u32 wnd_sum = 0;
  2167. /* Headers length */
  2168. hlen = (int)(skb_transport_header(skb) - skb->data) +
  2169. tcp_hdrlen(skb);
  2170. /* Amount of data (w/o headers) on linear part of SKB*/
  2171. first_bd_sz = skb_headlen(skb) - hlen;
  2172. wnd_sum = first_bd_sz;
  2173. /* Calculate the first sum - it's special */
  2174. for (frag_idx = 0; frag_idx < wnd_size - 1; frag_idx++)
  2175. wnd_sum +=
  2176. skb_frag_size(&skb_shinfo(skb)->frags[frag_idx]);
  2177. /* If there was data on linear skb data - check it */
  2178. if (first_bd_sz > 0) {
  2179. if (unlikely(wnd_sum < lso_mss)) {
  2180. to_copy = 1;
  2181. goto exit_lbl;
  2182. }
  2183. wnd_sum -= first_bd_sz;
  2184. }
  2185. /* Others are easier: run through the frag list and
  2186. check all windows */
  2187. for (wnd_idx = 0; wnd_idx <= num_wnds; wnd_idx++) {
  2188. wnd_sum +=
  2189. skb_frag_size(&skb_shinfo(skb)->frags[wnd_idx + wnd_size - 1]);
  2190. if (unlikely(wnd_sum < lso_mss)) {
  2191. to_copy = 1;
  2192. break;
  2193. }
  2194. wnd_sum -=
  2195. skb_frag_size(&skb_shinfo(skb)->frags[wnd_idx]);
  2196. }
  2197. } else {
  2198. /* in non-LSO too fragmented packet should always
  2199. be linearized */
  2200. to_copy = 1;
  2201. }
  2202. }
  2203. exit_lbl:
  2204. if (unlikely(to_copy))
  2205. DP(NETIF_MSG_TX_QUEUED,
  2206. "Linearization IS REQUIRED for %s packet. num_frags %d hlen %d first_bd_sz %d\n",
  2207. (xmit_type & XMIT_GSO) ? "LSO" : "non-LSO",
  2208. skb_shinfo(skb)->nr_frags, hlen, first_bd_sz);
  2209. return to_copy;
  2210. }
  2211. #endif
  2212. static inline void bnx2x_set_pbd_gso_e2(struct sk_buff *skb, u32 *parsing_data,
  2213. u32 xmit_type)
  2214. {
  2215. *parsing_data |= (skb_shinfo(skb)->gso_size <<
  2216. ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
  2217. ETH_TX_PARSE_BD_E2_LSO_MSS;
  2218. if ((xmit_type & XMIT_GSO_V6) &&
  2219. (ipv6_hdr(skb)->nexthdr == NEXTHDR_IPV6))
  2220. *parsing_data |= ETH_TX_PARSE_BD_E2_IPV6_WITH_EXT_HDR;
  2221. }
  2222. /**
  2223. * bnx2x_set_pbd_gso - update PBD in GSO case.
  2224. *
  2225. * @skb: packet skb
  2226. * @pbd: parse BD
  2227. * @xmit_type: xmit flags
  2228. */
  2229. static inline void bnx2x_set_pbd_gso(struct sk_buff *skb,
  2230. struct eth_tx_parse_bd_e1x *pbd,
  2231. u32 xmit_type)
  2232. {
  2233. pbd->lso_mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
  2234. pbd->tcp_send_seq = swab32(tcp_hdr(skb)->seq);
  2235. pbd->tcp_flags = pbd_tcp_flags(skb);
  2236. if (xmit_type & XMIT_GSO_V4) {
  2237. pbd->ip_id = swab16(ip_hdr(skb)->id);
  2238. pbd->tcp_pseudo_csum =
  2239. swab16(~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  2240. ip_hdr(skb)->daddr,
  2241. 0, IPPROTO_TCP, 0));
  2242. } else
  2243. pbd->tcp_pseudo_csum =
  2244. swab16(~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2245. &ipv6_hdr(skb)->daddr,
  2246. 0, IPPROTO_TCP, 0));
  2247. pbd->global_data |= ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN;
  2248. }
  2249. /**
  2250. * bnx2x_set_pbd_csum_e2 - update PBD with checksum and return header length
  2251. *
  2252. * @bp: driver handle
  2253. * @skb: packet skb
  2254. * @parsing_data: data to be updated
  2255. * @xmit_type: xmit flags
  2256. *
  2257. * 57712 related
  2258. */
  2259. static inline u8 bnx2x_set_pbd_csum_e2(struct bnx2x *bp, struct sk_buff *skb,
  2260. u32 *parsing_data, u32 xmit_type)
  2261. {
  2262. *parsing_data |=
  2263. ((((u8 *)skb_transport_header(skb) - skb->data) >> 1) <<
  2264. ETH_TX_PARSE_BD_E2_TCP_HDR_START_OFFSET_W_SHIFT) &
  2265. ETH_TX_PARSE_BD_E2_TCP_HDR_START_OFFSET_W;
  2266. if (xmit_type & XMIT_CSUM_TCP) {
  2267. *parsing_data |= ((tcp_hdrlen(skb) / 4) <<
  2268. ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
  2269. ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW;
  2270. return skb_transport_header(skb) + tcp_hdrlen(skb) - skb->data;
  2271. } else
  2272. /* We support checksum offload for TCP and UDP only.
  2273. * No need to pass the UDP header length - it's a constant.
  2274. */
  2275. return skb_transport_header(skb) +
  2276. sizeof(struct udphdr) - skb->data;
  2277. }
  2278. static inline void bnx2x_set_sbd_csum(struct bnx2x *bp, struct sk_buff *skb,
  2279. struct eth_tx_start_bd *tx_start_bd, u32 xmit_type)
  2280. {
  2281. tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
  2282. if (xmit_type & XMIT_CSUM_V4)
  2283. tx_start_bd->bd_flags.as_bitfield |=
  2284. ETH_TX_BD_FLAGS_IP_CSUM;
  2285. else
  2286. tx_start_bd->bd_flags.as_bitfield |=
  2287. ETH_TX_BD_FLAGS_IPV6;
  2288. if (!(xmit_type & XMIT_CSUM_TCP))
  2289. tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IS_UDP;
  2290. }
  2291. /**
  2292. * bnx2x_set_pbd_csum - update PBD with checksum and return header length
  2293. *
  2294. * @bp: driver handle
  2295. * @skb: packet skb
  2296. * @pbd: parse BD to be updated
  2297. * @xmit_type: xmit flags
  2298. */
  2299. static inline u8 bnx2x_set_pbd_csum(struct bnx2x *bp, struct sk_buff *skb,
  2300. struct eth_tx_parse_bd_e1x *pbd,
  2301. u32 xmit_type)
  2302. {
  2303. u8 hlen = (skb_network_header(skb) - skb->data) >> 1;
  2304. /* for now NS flag is not used in Linux */
  2305. pbd->global_data =
  2306. (hlen | ((skb->protocol == cpu_to_be16(ETH_P_8021Q)) <<
  2307. ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
  2308. pbd->ip_hlen_w = (skb_transport_header(skb) -
  2309. skb_network_header(skb)) >> 1;
  2310. hlen += pbd->ip_hlen_w;
  2311. /* We support checksum offload for TCP and UDP only */
  2312. if (xmit_type & XMIT_CSUM_TCP)
  2313. hlen += tcp_hdrlen(skb) / 2;
  2314. else
  2315. hlen += sizeof(struct udphdr) / 2;
  2316. pbd->total_hlen_w = cpu_to_le16(hlen);
  2317. hlen = hlen*2;
  2318. if (xmit_type & XMIT_CSUM_TCP) {
  2319. pbd->tcp_pseudo_csum = swab16(tcp_hdr(skb)->check);
  2320. } else {
  2321. s8 fix = SKB_CS_OFF(skb); /* signed! */
  2322. DP(NETIF_MSG_TX_QUEUED,
  2323. "hlen %d fix %d csum before fix %x\n",
  2324. le16_to_cpu(pbd->total_hlen_w), fix, SKB_CS(skb));
  2325. /* HW bug: fixup the CSUM */
  2326. pbd->tcp_pseudo_csum =
  2327. bnx2x_csum_fix(skb_transport_header(skb),
  2328. SKB_CS(skb), fix);
  2329. DP(NETIF_MSG_TX_QUEUED, "csum after fix %x\n",
  2330. pbd->tcp_pseudo_csum);
  2331. }
  2332. return hlen;
  2333. }
  2334. /* called with netif_tx_lock
  2335. * bnx2x_tx_int() runs without netif_tx_lock unless it needs to call
  2336. * netif_wake_queue()
  2337. */
  2338. netdev_tx_t bnx2x_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2339. {
  2340. struct bnx2x *bp = netdev_priv(dev);
  2341. struct bnx2x_fastpath *fp;
  2342. struct netdev_queue *txq;
  2343. struct bnx2x_fp_txdata *txdata;
  2344. struct sw_tx_bd *tx_buf;
  2345. struct eth_tx_start_bd *tx_start_bd, *first_bd;
  2346. struct eth_tx_bd *tx_data_bd, *total_pkt_bd = NULL;
  2347. struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
  2348. struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
  2349. u32 pbd_e2_parsing_data = 0;
  2350. u16 pkt_prod, bd_prod;
  2351. int nbd, txq_index, fp_index, txdata_index;
  2352. dma_addr_t mapping;
  2353. u32 xmit_type = bnx2x_xmit_type(bp, skb);
  2354. int i;
  2355. u8 hlen = 0;
  2356. __le16 pkt_size = 0;
  2357. struct ethhdr *eth;
  2358. u8 mac_type = UNICAST_ADDRESS;
  2359. #ifdef BNX2X_STOP_ON_ERROR
  2360. if (unlikely(bp->panic))
  2361. return NETDEV_TX_BUSY;
  2362. #endif
  2363. txq_index = skb_get_queue_mapping(skb);
  2364. txq = netdev_get_tx_queue(dev, txq_index);
  2365. BUG_ON(txq_index >= MAX_ETH_TXQ_IDX(bp) + FCOE_PRESENT);
  2366. /* decode the fastpath index and the cos index from the txq */
  2367. fp_index = TXQ_TO_FP(txq_index);
  2368. txdata_index = TXQ_TO_COS(txq_index);
  2369. #ifdef BCM_CNIC
  2370. /*
  2371. * Override the above for the FCoE queue:
  2372. * - FCoE fp entry is right after the ETH entries.
  2373. * - FCoE L2 queue uses bp->txdata[0] only.
  2374. */
  2375. if (unlikely(!NO_FCOE(bp) && (txq_index ==
  2376. bnx2x_fcoe_tx(bp, txq_index)))) {
  2377. fp_index = FCOE_IDX;
  2378. txdata_index = 0;
  2379. }
  2380. #endif
  2381. /* enable this debug print to view the transmission queue being used
  2382. DP(NETIF_MSG_TX_QUEUED, "indices: txq %d, fp %d, txdata %d\n",
  2383. txq_index, fp_index, txdata_index); */
  2384. /* locate the fastpath and the txdata */
  2385. fp = &bp->fp[fp_index];
  2386. txdata = &fp->txdata[txdata_index];
  2387. /* enable this debug print to view the tranmission details
  2388. DP(NETIF_MSG_TX_QUEUED,
  2389. "transmitting packet cid %d fp index %d txdata_index %d tx_data ptr %p fp pointer %p\n",
  2390. txdata->cid, fp_index, txdata_index, txdata, fp); */
  2391. if (unlikely(bnx2x_tx_avail(bp, txdata) <
  2392. (skb_shinfo(skb)->nr_frags + 3))) {
  2393. fp->eth_q_stats.driver_xoff++;
  2394. netif_tx_stop_queue(txq);
  2395. BNX2X_ERR("BUG! Tx ring full when queue awake!\n");
  2396. return NETDEV_TX_BUSY;
  2397. }
  2398. DP(NETIF_MSG_TX_QUEUED,
  2399. "queue[%d]: SKB: summed %x protocol %x protocol(%x,%x) gso type %x xmit_type %x\n",
  2400. txq_index, skb->ip_summed, skb->protocol, ipv6_hdr(skb)->nexthdr,
  2401. ip_hdr(skb)->protocol, skb_shinfo(skb)->gso_type, xmit_type);
  2402. eth = (struct ethhdr *)skb->data;
  2403. /* set flag according to packet type (UNICAST_ADDRESS is default)*/
  2404. if (unlikely(is_multicast_ether_addr(eth->h_dest))) {
  2405. if (is_broadcast_ether_addr(eth->h_dest))
  2406. mac_type = BROADCAST_ADDRESS;
  2407. else
  2408. mac_type = MULTICAST_ADDRESS;
  2409. }
  2410. #if (MAX_SKB_FRAGS >= MAX_FETCH_BD - 3)
  2411. /* First, check if we need to linearize the skb (due to FW
  2412. restrictions). No need to check fragmentation if page size > 8K
  2413. (there will be no violation to FW restrictions) */
  2414. if (bnx2x_pkt_req_lin(bp, skb, xmit_type)) {
  2415. /* Statistics of linearization */
  2416. bp->lin_cnt++;
  2417. if (skb_linearize(skb) != 0) {
  2418. DP(NETIF_MSG_TX_QUEUED,
  2419. "SKB linearization failed - silently dropping this SKB\n");
  2420. dev_kfree_skb_any(skb);
  2421. return NETDEV_TX_OK;
  2422. }
  2423. }
  2424. #endif
  2425. /* Map skb linear data for DMA */
  2426. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  2427. skb_headlen(skb), DMA_TO_DEVICE);
  2428. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  2429. DP(NETIF_MSG_TX_QUEUED,
  2430. "SKB mapping failed - silently dropping this SKB\n");
  2431. dev_kfree_skb_any(skb);
  2432. return NETDEV_TX_OK;
  2433. }
  2434. /*
  2435. Please read carefully. First we use one BD which we mark as start,
  2436. then we have a parsing info BD (used for TSO or xsum),
  2437. and only then we have the rest of the TSO BDs.
  2438. (don't forget to mark the last one as last,
  2439. and to unmap only AFTER you write to the BD ...)
  2440. And above all, all pdb sizes are in words - NOT DWORDS!
  2441. */
  2442. /* get current pkt produced now - advance it just before sending packet
  2443. * since mapping of pages may fail and cause packet to be dropped
  2444. */
  2445. pkt_prod = txdata->tx_pkt_prod;
  2446. bd_prod = TX_BD(txdata->tx_bd_prod);
  2447. /* get a tx_buf and first BD
  2448. * tx_start_bd may be changed during SPLIT,
  2449. * but first_bd will always stay first
  2450. */
  2451. tx_buf = &txdata->tx_buf_ring[TX_BD(pkt_prod)];
  2452. tx_start_bd = &txdata->tx_desc_ring[bd_prod].start_bd;
  2453. first_bd = tx_start_bd;
  2454. tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
  2455. SET_FLAG(tx_start_bd->general_data, ETH_TX_START_BD_ETH_ADDR_TYPE,
  2456. mac_type);
  2457. /* header nbd */
  2458. SET_FLAG(tx_start_bd->general_data, ETH_TX_START_BD_HDR_NBDS, 1);
  2459. /* remember the first BD of the packet */
  2460. tx_buf->first_bd = txdata->tx_bd_prod;
  2461. tx_buf->skb = skb;
  2462. tx_buf->flags = 0;
  2463. DP(NETIF_MSG_TX_QUEUED,
  2464. "sending pkt %u @%p next_idx %u bd %u @%p\n",
  2465. pkt_prod, tx_buf, txdata->tx_pkt_prod, bd_prod, tx_start_bd);
  2466. if (vlan_tx_tag_present(skb)) {
  2467. tx_start_bd->vlan_or_ethertype =
  2468. cpu_to_le16(vlan_tx_tag_get(skb));
  2469. tx_start_bd->bd_flags.as_bitfield |=
  2470. (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
  2471. } else
  2472. tx_start_bd->vlan_or_ethertype = cpu_to_le16(pkt_prod);
  2473. /* turn on parsing and get a BD */
  2474. bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
  2475. if (xmit_type & XMIT_CSUM)
  2476. bnx2x_set_sbd_csum(bp, skb, tx_start_bd, xmit_type);
  2477. if (!CHIP_IS_E1x(bp)) {
  2478. pbd_e2 = &txdata->tx_desc_ring[bd_prod].parse_bd_e2;
  2479. memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
  2480. /* Set PBD in checksum offload case */
  2481. if (xmit_type & XMIT_CSUM)
  2482. hlen = bnx2x_set_pbd_csum_e2(bp, skb,
  2483. &pbd_e2_parsing_data,
  2484. xmit_type);
  2485. if (IS_MF_SI(bp)) {
  2486. /*
  2487. * fill in the MAC addresses in the PBD - for local
  2488. * switching
  2489. */
  2490. bnx2x_set_fw_mac_addr(&pbd_e2->src_mac_addr_hi,
  2491. &pbd_e2->src_mac_addr_mid,
  2492. &pbd_e2->src_mac_addr_lo,
  2493. eth->h_source);
  2494. bnx2x_set_fw_mac_addr(&pbd_e2->dst_mac_addr_hi,
  2495. &pbd_e2->dst_mac_addr_mid,
  2496. &pbd_e2->dst_mac_addr_lo,
  2497. eth->h_dest);
  2498. }
  2499. } else {
  2500. pbd_e1x = &txdata->tx_desc_ring[bd_prod].parse_bd_e1x;
  2501. memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
  2502. /* Set PBD in checksum offload case */
  2503. if (xmit_type & XMIT_CSUM)
  2504. hlen = bnx2x_set_pbd_csum(bp, skb, pbd_e1x, xmit_type);
  2505. }
  2506. /* Setup the data pointer of the first BD of the packet */
  2507. tx_start_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  2508. tx_start_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  2509. nbd = 2; /* start_bd + pbd + frags (updated when pages are mapped) */
  2510. tx_start_bd->nbytes = cpu_to_le16(skb_headlen(skb));
  2511. pkt_size = tx_start_bd->nbytes;
  2512. DP(NETIF_MSG_TX_QUEUED,
  2513. "first bd @%p addr (%x:%x) nbd %d nbytes %d flags %x vlan %x\n",
  2514. tx_start_bd, tx_start_bd->addr_hi, tx_start_bd->addr_lo,
  2515. le16_to_cpu(tx_start_bd->nbd), le16_to_cpu(tx_start_bd->nbytes),
  2516. tx_start_bd->bd_flags.as_bitfield,
  2517. le16_to_cpu(tx_start_bd->vlan_or_ethertype));
  2518. if (xmit_type & XMIT_GSO) {
  2519. DP(NETIF_MSG_TX_QUEUED,
  2520. "TSO packet len %d hlen %d total len %d tso size %d\n",
  2521. skb->len, hlen, skb_headlen(skb),
  2522. skb_shinfo(skb)->gso_size);
  2523. tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
  2524. if (unlikely(skb_headlen(skb) > hlen))
  2525. bd_prod = bnx2x_tx_split(bp, txdata, tx_buf,
  2526. &tx_start_bd, hlen,
  2527. bd_prod, ++nbd);
  2528. if (!CHIP_IS_E1x(bp))
  2529. bnx2x_set_pbd_gso_e2(skb, &pbd_e2_parsing_data,
  2530. xmit_type);
  2531. else
  2532. bnx2x_set_pbd_gso(skb, pbd_e1x, xmit_type);
  2533. }
  2534. /* Set the PBD's parsing_data field if not zero
  2535. * (for the chips newer than 57711).
  2536. */
  2537. if (pbd_e2_parsing_data)
  2538. pbd_e2->parsing_data = cpu_to_le32(pbd_e2_parsing_data);
  2539. tx_data_bd = (struct eth_tx_bd *)tx_start_bd;
  2540. /* Handle fragmented skb */
  2541. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2542. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2543. mapping = skb_frag_dma_map(&bp->pdev->dev, frag, 0,
  2544. skb_frag_size(frag), DMA_TO_DEVICE);
  2545. if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
  2546. unsigned int pkts_compl = 0, bytes_compl = 0;
  2547. DP(NETIF_MSG_TX_QUEUED,
  2548. "Unable to map page - dropping packet...\n");
  2549. /* we need unmap all buffers already mapped
  2550. * for this SKB;
  2551. * first_bd->nbd need to be properly updated
  2552. * before call to bnx2x_free_tx_pkt
  2553. */
  2554. first_bd->nbd = cpu_to_le16(nbd);
  2555. bnx2x_free_tx_pkt(bp, txdata,
  2556. TX_BD(txdata->tx_pkt_prod),
  2557. &pkts_compl, &bytes_compl);
  2558. return NETDEV_TX_OK;
  2559. }
  2560. bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
  2561. tx_data_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
  2562. if (total_pkt_bd == NULL)
  2563. total_pkt_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
  2564. tx_data_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
  2565. tx_data_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
  2566. tx_data_bd->nbytes = cpu_to_le16(skb_frag_size(frag));
  2567. le16_add_cpu(&pkt_size, skb_frag_size(frag));
  2568. nbd++;
  2569. DP(NETIF_MSG_TX_QUEUED,
  2570. "frag %d bd @%p addr (%x:%x) nbytes %d\n",
  2571. i, tx_data_bd, tx_data_bd->addr_hi, tx_data_bd->addr_lo,
  2572. le16_to_cpu(tx_data_bd->nbytes));
  2573. }
  2574. DP(NETIF_MSG_TX_QUEUED, "last bd @%p\n", tx_data_bd);
  2575. /* update with actual num BDs */
  2576. first_bd->nbd = cpu_to_le16(nbd);
  2577. bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
  2578. /* now send a tx doorbell, counting the next BD
  2579. * if the packet contains or ends with it
  2580. */
  2581. if (TX_BD_POFF(bd_prod) < nbd)
  2582. nbd++;
  2583. /* total_pkt_bytes should be set on the first data BD if
  2584. * it's not an LSO packet and there is more than one
  2585. * data BD. In this case pkt_size is limited by an MTU value.
  2586. * However we prefer to set it for an LSO packet (while we don't
  2587. * have to) in order to save some CPU cycles in a none-LSO
  2588. * case, when we much more care about them.
  2589. */
  2590. if (total_pkt_bd != NULL)
  2591. total_pkt_bd->total_pkt_bytes = pkt_size;
  2592. if (pbd_e1x)
  2593. DP(NETIF_MSG_TX_QUEUED,
  2594. "PBD (E1X) @%p ip_data %x ip_hlen %u ip_id %u lso_mss %u tcp_flags %x xsum %x seq %u hlen %u\n",
  2595. pbd_e1x, pbd_e1x->global_data, pbd_e1x->ip_hlen_w,
  2596. pbd_e1x->ip_id, pbd_e1x->lso_mss, pbd_e1x->tcp_flags,
  2597. pbd_e1x->tcp_pseudo_csum, pbd_e1x->tcp_send_seq,
  2598. le16_to_cpu(pbd_e1x->total_hlen_w));
  2599. if (pbd_e2)
  2600. DP(NETIF_MSG_TX_QUEUED,
  2601. "PBD (E2) @%p dst %x %x %x src %x %x %x parsing_data %x\n",
  2602. pbd_e2, pbd_e2->dst_mac_addr_hi, pbd_e2->dst_mac_addr_mid,
  2603. pbd_e2->dst_mac_addr_lo, pbd_e2->src_mac_addr_hi,
  2604. pbd_e2->src_mac_addr_mid, pbd_e2->src_mac_addr_lo,
  2605. pbd_e2->parsing_data);
  2606. DP(NETIF_MSG_TX_QUEUED, "doorbell: nbd %d bd %u\n", nbd, bd_prod);
  2607. netdev_tx_sent_queue(txq, skb->len);
  2608. skb_tx_timestamp(skb);
  2609. txdata->tx_pkt_prod++;
  2610. /*
  2611. * Make sure that the BD data is updated before updating the producer
  2612. * since FW might read the BD right after the producer is updated.
  2613. * This is only applicable for weak-ordered memory model archs such
  2614. * as IA-64. The following barrier is also mandatory since FW will
  2615. * assumes packets must have BDs.
  2616. */
  2617. wmb();
  2618. txdata->tx_db.data.prod += nbd;
  2619. barrier();
  2620. DOORBELL(bp, txdata->cid, txdata->tx_db.raw);
  2621. mmiowb();
  2622. txdata->tx_bd_prod += nbd;
  2623. if (unlikely(bnx2x_tx_avail(bp, txdata) < MAX_SKB_FRAGS + 3)) {
  2624. netif_tx_stop_queue(txq);
  2625. /* paired memory barrier is in bnx2x_tx_int(), we have to keep
  2626. * ordering of set_bit() in netif_tx_stop_queue() and read of
  2627. * fp->bd_tx_cons */
  2628. smp_mb();
  2629. fp->eth_q_stats.driver_xoff++;
  2630. if (bnx2x_tx_avail(bp, txdata) >= MAX_SKB_FRAGS + 3)
  2631. netif_tx_wake_queue(txq);
  2632. }
  2633. txdata->tx_pkt++;
  2634. return NETDEV_TX_OK;
  2635. }
  2636. /**
  2637. * bnx2x_setup_tc - routine to configure net_device for multi tc
  2638. *
  2639. * @netdev: net device to configure
  2640. * @tc: number of traffic classes to enable
  2641. *
  2642. * callback connected to the ndo_setup_tc function pointer
  2643. */
  2644. int bnx2x_setup_tc(struct net_device *dev, u8 num_tc)
  2645. {
  2646. int cos, prio, count, offset;
  2647. struct bnx2x *bp = netdev_priv(dev);
  2648. /* setup tc must be called under rtnl lock */
  2649. ASSERT_RTNL();
  2650. /* no traffic classes requested. aborting */
  2651. if (!num_tc) {
  2652. netdev_reset_tc(dev);
  2653. return 0;
  2654. }
  2655. /* requested to support too many traffic classes */
  2656. if (num_tc > bp->max_cos) {
  2657. BNX2X_ERR("support for too many traffic classes requested: %d. max supported is %d\n",
  2658. num_tc, bp->max_cos);
  2659. return -EINVAL;
  2660. }
  2661. /* declare amount of supported traffic classes */
  2662. if (netdev_set_num_tc(dev, num_tc)) {
  2663. BNX2X_ERR("failed to declare %d traffic classes\n", num_tc);
  2664. return -EINVAL;
  2665. }
  2666. /* configure priority to traffic class mapping */
  2667. for (prio = 0; prio < BNX2X_MAX_PRIORITY; prio++) {
  2668. netdev_set_prio_tc_map(dev, prio, bp->prio_to_cos[prio]);
  2669. DP(BNX2X_MSG_SP | NETIF_MSG_IFUP,
  2670. "mapping priority %d to tc %d\n",
  2671. prio, bp->prio_to_cos[prio]);
  2672. }
  2673. /* Use this configuration to diffrentiate tc0 from other COSes
  2674. This can be used for ets or pfc, and save the effort of setting
  2675. up a multio class queue disc or negotiating DCBX with a switch
  2676. netdev_set_prio_tc_map(dev, 0, 0);
  2677. DP(BNX2X_MSG_SP, "mapping priority %d to tc %d\n", 0, 0);
  2678. for (prio = 1; prio < 16; prio++) {
  2679. netdev_set_prio_tc_map(dev, prio, 1);
  2680. DP(BNX2X_MSG_SP, "mapping priority %d to tc %d\n", prio, 1);
  2681. } */
  2682. /* configure traffic class to transmission queue mapping */
  2683. for (cos = 0; cos < bp->max_cos; cos++) {
  2684. count = BNX2X_NUM_ETH_QUEUES(bp);
  2685. offset = cos * MAX_TXQS_PER_COS;
  2686. netdev_set_tc_queue(dev, cos, count, offset);
  2687. DP(BNX2X_MSG_SP | NETIF_MSG_IFUP,
  2688. "mapping tc %d to offset %d count %d\n",
  2689. cos, offset, count);
  2690. }
  2691. return 0;
  2692. }
  2693. /* called with rtnl_lock */
  2694. int bnx2x_change_mac_addr(struct net_device *dev, void *p)
  2695. {
  2696. struct sockaddr *addr = p;
  2697. struct bnx2x *bp = netdev_priv(dev);
  2698. int rc = 0;
  2699. if (!bnx2x_is_valid_ether_addr(bp, addr->sa_data)) {
  2700. BNX2X_ERR("Requested MAC address is not valid\n");
  2701. return -EINVAL;
  2702. }
  2703. #ifdef BCM_CNIC
  2704. if ((IS_MF_STORAGE_SD(bp) || IS_MF_FCOE_AFEX(bp)) &&
  2705. !is_zero_ether_addr(addr->sa_data)) {
  2706. BNX2X_ERR("Can't configure non-zero address on iSCSI or FCoE functions in MF-SD mode\n");
  2707. return -EINVAL;
  2708. }
  2709. #endif
  2710. if (netif_running(dev)) {
  2711. rc = bnx2x_set_eth_mac(bp, false);
  2712. if (rc)
  2713. return rc;
  2714. }
  2715. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  2716. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  2717. if (netif_running(dev))
  2718. rc = bnx2x_set_eth_mac(bp, true);
  2719. return rc;
  2720. }
  2721. static void bnx2x_free_fp_mem_at(struct bnx2x *bp, int fp_index)
  2722. {
  2723. union host_hc_status_block *sb = &bnx2x_fp(bp, fp_index, status_blk);
  2724. struct bnx2x_fastpath *fp = &bp->fp[fp_index];
  2725. u8 cos;
  2726. /* Common */
  2727. #ifdef BCM_CNIC
  2728. if (IS_FCOE_IDX(fp_index)) {
  2729. memset(sb, 0, sizeof(union host_hc_status_block));
  2730. fp->status_blk_mapping = 0;
  2731. } else {
  2732. #endif
  2733. /* status blocks */
  2734. if (!CHIP_IS_E1x(bp))
  2735. BNX2X_PCI_FREE(sb->e2_sb,
  2736. bnx2x_fp(bp, fp_index,
  2737. status_blk_mapping),
  2738. sizeof(struct host_hc_status_block_e2));
  2739. else
  2740. BNX2X_PCI_FREE(sb->e1x_sb,
  2741. bnx2x_fp(bp, fp_index,
  2742. status_blk_mapping),
  2743. sizeof(struct host_hc_status_block_e1x));
  2744. #ifdef BCM_CNIC
  2745. }
  2746. #endif
  2747. /* Rx */
  2748. if (!skip_rx_queue(bp, fp_index)) {
  2749. bnx2x_free_rx_bds(fp);
  2750. /* fastpath rx rings: rx_buf rx_desc rx_comp */
  2751. BNX2X_FREE(bnx2x_fp(bp, fp_index, rx_buf_ring));
  2752. BNX2X_PCI_FREE(bnx2x_fp(bp, fp_index, rx_desc_ring),
  2753. bnx2x_fp(bp, fp_index, rx_desc_mapping),
  2754. sizeof(struct eth_rx_bd) * NUM_RX_BD);
  2755. BNX2X_PCI_FREE(bnx2x_fp(bp, fp_index, rx_comp_ring),
  2756. bnx2x_fp(bp, fp_index, rx_comp_mapping),
  2757. sizeof(struct eth_fast_path_rx_cqe) *
  2758. NUM_RCQ_BD);
  2759. /* SGE ring */
  2760. BNX2X_FREE(bnx2x_fp(bp, fp_index, rx_page_ring));
  2761. BNX2X_PCI_FREE(bnx2x_fp(bp, fp_index, rx_sge_ring),
  2762. bnx2x_fp(bp, fp_index, rx_sge_mapping),
  2763. BCM_PAGE_SIZE * NUM_RX_SGE_PAGES);
  2764. }
  2765. /* Tx */
  2766. if (!skip_tx_queue(bp, fp_index)) {
  2767. /* fastpath tx rings: tx_buf tx_desc */
  2768. for_each_cos_in_tx_queue(fp, cos) {
  2769. struct bnx2x_fp_txdata *txdata = &fp->txdata[cos];
  2770. DP(NETIF_MSG_IFDOWN,
  2771. "freeing tx memory of fp %d cos %d cid %d\n",
  2772. fp_index, cos, txdata->cid);
  2773. BNX2X_FREE(txdata->tx_buf_ring);
  2774. BNX2X_PCI_FREE(txdata->tx_desc_ring,
  2775. txdata->tx_desc_mapping,
  2776. sizeof(union eth_tx_bd_types) * NUM_TX_BD);
  2777. }
  2778. }
  2779. /* end of fastpath */
  2780. }
  2781. void bnx2x_free_fp_mem(struct bnx2x *bp)
  2782. {
  2783. int i;
  2784. for_each_queue(bp, i)
  2785. bnx2x_free_fp_mem_at(bp, i);
  2786. }
  2787. static void set_sb_shortcuts(struct bnx2x *bp, int index)
  2788. {
  2789. union host_hc_status_block status_blk = bnx2x_fp(bp, index, status_blk);
  2790. if (!CHIP_IS_E1x(bp)) {
  2791. bnx2x_fp(bp, index, sb_index_values) =
  2792. (__le16 *)status_blk.e2_sb->sb.index_values;
  2793. bnx2x_fp(bp, index, sb_running_index) =
  2794. (__le16 *)status_blk.e2_sb->sb.running_index;
  2795. } else {
  2796. bnx2x_fp(bp, index, sb_index_values) =
  2797. (__le16 *)status_blk.e1x_sb->sb.index_values;
  2798. bnx2x_fp(bp, index, sb_running_index) =
  2799. (__le16 *)status_blk.e1x_sb->sb.running_index;
  2800. }
  2801. }
  2802. /* Returns the number of actually allocated BDs */
  2803. static int bnx2x_alloc_rx_bds(struct bnx2x_fastpath *fp,
  2804. int rx_ring_size)
  2805. {
  2806. struct bnx2x *bp = fp->bp;
  2807. u16 ring_prod, cqe_ring_prod;
  2808. int i, failure_cnt = 0;
  2809. fp->rx_comp_cons = 0;
  2810. cqe_ring_prod = ring_prod = 0;
  2811. /* This routine is called only during fo init so
  2812. * fp->eth_q_stats.rx_skb_alloc_failed = 0
  2813. */
  2814. for (i = 0; i < rx_ring_size; i++) {
  2815. if (bnx2x_alloc_rx_data(bp, fp, ring_prod) < 0) {
  2816. failure_cnt++;
  2817. continue;
  2818. }
  2819. ring_prod = NEXT_RX_IDX(ring_prod);
  2820. cqe_ring_prod = NEXT_RCQ_IDX(cqe_ring_prod);
  2821. WARN_ON(ring_prod <= (i - failure_cnt));
  2822. }
  2823. if (failure_cnt)
  2824. BNX2X_ERR("was only able to allocate %d rx skbs on queue[%d]\n",
  2825. i - failure_cnt, fp->index);
  2826. fp->rx_bd_prod = ring_prod;
  2827. /* Limit the CQE producer by the CQE ring size */
  2828. fp->rx_comp_prod = min_t(u16, NUM_RCQ_RINGS*RCQ_DESC_CNT,
  2829. cqe_ring_prod);
  2830. fp->rx_pkt = fp->rx_calls = 0;
  2831. fp->eth_q_stats.rx_skb_alloc_failed += failure_cnt;
  2832. return i - failure_cnt;
  2833. }
  2834. static void bnx2x_set_next_page_rx_cq(struct bnx2x_fastpath *fp)
  2835. {
  2836. int i;
  2837. for (i = 1; i <= NUM_RCQ_RINGS; i++) {
  2838. struct eth_rx_cqe_next_page *nextpg;
  2839. nextpg = (struct eth_rx_cqe_next_page *)
  2840. &fp->rx_comp_ring[RCQ_DESC_CNT * i - 1];
  2841. nextpg->addr_hi =
  2842. cpu_to_le32(U64_HI(fp->rx_comp_mapping +
  2843. BCM_PAGE_SIZE*(i % NUM_RCQ_RINGS)));
  2844. nextpg->addr_lo =
  2845. cpu_to_le32(U64_LO(fp->rx_comp_mapping +
  2846. BCM_PAGE_SIZE*(i % NUM_RCQ_RINGS)));
  2847. }
  2848. }
  2849. static int bnx2x_alloc_fp_mem_at(struct bnx2x *bp, int index)
  2850. {
  2851. union host_hc_status_block *sb;
  2852. struct bnx2x_fastpath *fp = &bp->fp[index];
  2853. int ring_size = 0;
  2854. u8 cos;
  2855. int rx_ring_size = 0;
  2856. #ifdef BCM_CNIC
  2857. if (!bp->rx_ring_size &&
  2858. (IS_MF_STORAGE_SD(bp) || IS_MF_FCOE_AFEX(bp))) {
  2859. rx_ring_size = MIN_RX_SIZE_NONTPA;
  2860. bp->rx_ring_size = rx_ring_size;
  2861. } else
  2862. #endif
  2863. if (!bp->rx_ring_size) {
  2864. u32 cfg = SHMEM_RD(bp,
  2865. dev_info.port_hw_config[BP_PORT(bp)].default_cfg);
  2866. rx_ring_size = MAX_RX_AVAIL/BNX2X_NUM_RX_QUEUES(bp);
  2867. /* Dercease ring size for 1G functions */
  2868. if ((cfg & PORT_HW_CFG_NET_SERDES_IF_MASK) ==
  2869. PORT_HW_CFG_NET_SERDES_IF_SGMII)
  2870. rx_ring_size /= 10;
  2871. /* allocate at least number of buffers required by FW */
  2872. rx_ring_size = max_t(int, bp->disable_tpa ? MIN_RX_SIZE_NONTPA :
  2873. MIN_RX_SIZE_TPA, rx_ring_size);
  2874. bp->rx_ring_size = rx_ring_size;
  2875. } else /* if rx_ring_size specified - use it */
  2876. rx_ring_size = bp->rx_ring_size;
  2877. /* Common */
  2878. sb = &bnx2x_fp(bp, index, status_blk);
  2879. #ifdef BCM_CNIC
  2880. if (!IS_FCOE_IDX(index)) {
  2881. #endif
  2882. /* status blocks */
  2883. if (!CHIP_IS_E1x(bp))
  2884. BNX2X_PCI_ALLOC(sb->e2_sb,
  2885. &bnx2x_fp(bp, index, status_blk_mapping),
  2886. sizeof(struct host_hc_status_block_e2));
  2887. else
  2888. BNX2X_PCI_ALLOC(sb->e1x_sb,
  2889. &bnx2x_fp(bp, index, status_blk_mapping),
  2890. sizeof(struct host_hc_status_block_e1x));
  2891. #ifdef BCM_CNIC
  2892. }
  2893. #endif
  2894. /* FCoE Queue uses Default SB and doesn't ACK the SB, thus no need to
  2895. * set shortcuts for it.
  2896. */
  2897. if (!IS_FCOE_IDX(index))
  2898. set_sb_shortcuts(bp, index);
  2899. /* Tx */
  2900. if (!skip_tx_queue(bp, index)) {
  2901. /* fastpath tx rings: tx_buf tx_desc */
  2902. for_each_cos_in_tx_queue(fp, cos) {
  2903. struct bnx2x_fp_txdata *txdata = &fp->txdata[cos];
  2904. DP(NETIF_MSG_IFUP,
  2905. "allocating tx memory of fp %d cos %d\n",
  2906. index, cos);
  2907. BNX2X_ALLOC(txdata->tx_buf_ring,
  2908. sizeof(struct sw_tx_bd) * NUM_TX_BD);
  2909. BNX2X_PCI_ALLOC(txdata->tx_desc_ring,
  2910. &txdata->tx_desc_mapping,
  2911. sizeof(union eth_tx_bd_types) * NUM_TX_BD);
  2912. }
  2913. }
  2914. /* Rx */
  2915. if (!skip_rx_queue(bp, index)) {
  2916. /* fastpath rx rings: rx_buf rx_desc rx_comp */
  2917. BNX2X_ALLOC(bnx2x_fp(bp, index, rx_buf_ring),
  2918. sizeof(struct sw_rx_bd) * NUM_RX_BD);
  2919. BNX2X_PCI_ALLOC(bnx2x_fp(bp, index, rx_desc_ring),
  2920. &bnx2x_fp(bp, index, rx_desc_mapping),
  2921. sizeof(struct eth_rx_bd) * NUM_RX_BD);
  2922. BNX2X_PCI_ALLOC(bnx2x_fp(bp, index, rx_comp_ring),
  2923. &bnx2x_fp(bp, index, rx_comp_mapping),
  2924. sizeof(struct eth_fast_path_rx_cqe) *
  2925. NUM_RCQ_BD);
  2926. /* SGE ring */
  2927. BNX2X_ALLOC(bnx2x_fp(bp, index, rx_page_ring),
  2928. sizeof(struct sw_rx_page) * NUM_RX_SGE);
  2929. BNX2X_PCI_ALLOC(bnx2x_fp(bp, index, rx_sge_ring),
  2930. &bnx2x_fp(bp, index, rx_sge_mapping),
  2931. BCM_PAGE_SIZE * NUM_RX_SGE_PAGES);
  2932. /* RX BD ring */
  2933. bnx2x_set_next_page_rx_bd(fp);
  2934. /* CQ ring */
  2935. bnx2x_set_next_page_rx_cq(fp);
  2936. /* BDs */
  2937. ring_size = bnx2x_alloc_rx_bds(fp, rx_ring_size);
  2938. if (ring_size < rx_ring_size)
  2939. goto alloc_mem_err;
  2940. }
  2941. return 0;
  2942. /* handles low memory cases */
  2943. alloc_mem_err:
  2944. BNX2X_ERR("Unable to allocate full memory for queue %d (size %d)\n",
  2945. index, ring_size);
  2946. /* FW will drop all packets if queue is not big enough,
  2947. * In these cases we disable the queue
  2948. * Min size is different for OOO, TPA and non-TPA queues
  2949. */
  2950. if (ring_size < (fp->disable_tpa ?
  2951. MIN_RX_SIZE_NONTPA : MIN_RX_SIZE_TPA)) {
  2952. /* release memory allocated for this queue */
  2953. bnx2x_free_fp_mem_at(bp, index);
  2954. return -ENOMEM;
  2955. }
  2956. return 0;
  2957. }
  2958. int bnx2x_alloc_fp_mem(struct bnx2x *bp)
  2959. {
  2960. int i;
  2961. /**
  2962. * 1. Allocate FP for leading - fatal if error
  2963. * 2. {CNIC} Allocate FCoE FP - fatal if error
  2964. * 3. {CNIC} Allocate OOO + FWD - disable OOO if error
  2965. * 4. Allocate RSS - fix number of queues if error
  2966. */
  2967. /* leading */
  2968. if (bnx2x_alloc_fp_mem_at(bp, 0))
  2969. return -ENOMEM;
  2970. #ifdef BCM_CNIC
  2971. if (!NO_FCOE(bp))
  2972. /* FCoE */
  2973. if (bnx2x_alloc_fp_mem_at(bp, FCOE_IDX))
  2974. /* we will fail load process instead of mark
  2975. * NO_FCOE_FLAG
  2976. */
  2977. return -ENOMEM;
  2978. #endif
  2979. /* RSS */
  2980. for_each_nondefault_eth_queue(bp, i)
  2981. if (bnx2x_alloc_fp_mem_at(bp, i))
  2982. break;
  2983. /* handle memory failures */
  2984. if (i != BNX2X_NUM_ETH_QUEUES(bp)) {
  2985. int delta = BNX2X_NUM_ETH_QUEUES(bp) - i;
  2986. WARN_ON(delta < 0);
  2987. #ifdef BCM_CNIC
  2988. /**
  2989. * move non eth FPs next to last eth FP
  2990. * must be done in that order
  2991. * FCOE_IDX < FWD_IDX < OOO_IDX
  2992. */
  2993. /* move FCoE fp even NO_FCOE_FLAG is on */
  2994. bnx2x_move_fp(bp, FCOE_IDX, FCOE_IDX - delta);
  2995. #endif
  2996. bp->num_queues -= delta;
  2997. BNX2X_ERR("Adjusted num of queues from %d to %d\n",
  2998. bp->num_queues + delta, bp->num_queues);
  2999. }
  3000. return 0;
  3001. }
  3002. void bnx2x_free_mem_bp(struct bnx2x *bp)
  3003. {
  3004. kfree(bp->fp);
  3005. kfree(bp->msix_table);
  3006. kfree(bp->ilt);
  3007. }
  3008. int __devinit bnx2x_alloc_mem_bp(struct bnx2x *bp)
  3009. {
  3010. struct bnx2x_fastpath *fp;
  3011. struct msix_entry *tbl;
  3012. struct bnx2x_ilt *ilt;
  3013. int msix_table_size = 0;
  3014. /*
  3015. * The biggest MSI-X table we might need is as a maximum number of fast
  3016. * path IGU SBs plus default SB (for PF).
  3017. */
  3018. msix_table_size = bp->igu_sb_cnt + 1;
  3019. /* fp array: RSS plus CNIC related L2 queues */
  3020. fp = kcalloc(BNX2X_MAX_RSS_COUNT(bp) + NON_ETH_CONTEXT_USE,
  3021. sizeof(*fp), GFP_KERNEL);
  3022. if (!fp)
  3023. goto alloc_err;
  3024. bp->fp = fp;
  3025. /* msix table */
  3026. tbl = kcalloc(msix_table_size, sizeof(*tbl), GFP_KERNEL);
  3027. if (!tbl)
  3028. goto alloc_err;
  3029. bp->msix_table = tbl;
  3030. /* ilt */
  3031. ilt = kzalloc(sizeof(*ilt), GFP_KERNEL);
  3032. if (!ilt)
  3033. goto alloc_err;
  3034. bp->ilt = ilt;
  3035. return 0;
  3036. alloc_err:
  3037. bnx2x_free_mem_bp(bp);
  3038. return -ENOMEM;
  3039. }
  3040. int bnx2x_reload_if_running(struct net_device *dev)
  3041. {
  3042. struct bnx2x *bp = netdev_priv(dev);
  3043. if (unlikely(!netif_running(dev)))
  3044. return 0;
  3045. bnx2x_nic_unload(bp, UNLOAD_NORMAL);
  3046. return bnx2x_nic_load(bp, LOAD_NORMAL);
  3047. }
  3048. int bnx2x_get_cur_phy_idx(struct bnx2x *bp)
  3049. {
  3050. u32 sel_phy_idx = 0;
  3051. if (bp->link_params.num_phys <= 1)
  3052. return INT_PHY;
  3053. if (bp->link_vars.link_up) {
  3054. sel_phy_idx = EXT_PHY1;
  3055. /* In case link is SERDES, check if the EXT_PHY2 is the one */
  3056. if ((bp->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
  3057. (bp->link_params.phy[EXT_PHY2].supported & SUPPORTED_FIBRE))
  3058. sel_phy_idx = EXT_PHY2;
  3059. } else {
  3060. switch (bnx2x_phy_selection(&bp->link_params)) {
  3061. case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
  3062. case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
  3063. case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
  3064. sel_phy_idx = EXT_PHY1;
  3065. break;
  3066. case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
  3067. case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
  3068. sel_phy_idx = EXT_PHY2;
  3069. break;
  3070. }
  3071. }
  3072. return sel_phy_idx;
  3073. }
  3074. int bnx2x_get_link_cfg_idx(struct bnx2x *bp)
  3075. {
  3076. u32 sel_phy_idx = bnx2x_get_cur_phy_idx(bp);
  3077. /*
  3078. * The selected actived PHY is always after swapping (in case PHY
  3079. * swapping is enabled). So when swapping is enabled, we need to reverse
  3080. * the configuration
  3081. */
  3082. if (bp->link_params.multi_phy_config &
  3083. PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
  3084. if (sel_phy_idx == EXT_PHY1)
  3085. sel_phy_idx = EXT_PHY2;
  3086. else if (sel_phy_idx == EXT_PHY2)
  3087. sel_phy_idx = EXT_PHY1;
  3088. }
  3089. return LINK_CONFIG_IDX(sel_phy_idx);
  3090. }
  3091. #if defined(NETDEV_FCOE_WWNN) && defined(BCM_CNIC)
  3092. int bnx2x_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
  3093. {
  3094. struct bnx2x *bp = netdev_priv(dev);
  3095. struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
  3096. switch (type) {
  3097. case NETDEV_FCOE_WWNN:
  3098. *wwn = HILO_U64(cp->fcoe_wwn_node_name_hi,
  3099. cp->fcoe_wwn_node_name_lo);
  3100. break;
  3101. case NETDEV_FCOE_WWPN:
  3102. *wwn = HILO_U64(cp->fcoe_wwn_port_name_hi,
  3103. cp->fcoe_wwn_port_name_lo);
  3104. break;
  3105. default:
  3106. BNX2X_ERR("Wrong WWN type requested - %d\n", type);
  3107. return -EINVAL;
  3108. }
  3109. return 0;
  3110. }
  3111. #endif
  3112. /* called with rtnl_lock */
  3113. int bnx2x_change_mtu(struct net_device *dev, int new_mtu)
  3114. {
  3115. struct bnx2x *bp = netdev_priv(dev);
  3116. if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
  3117. BNX2X_ERR("Can't perform change MTU during parity recovery\n");
  3118. return -EAGAIN;
  3119. }
  3120. if ((new_mtu > ETH_MAX_JUMBO_PACKET_SIZE) ||
  3121. ((new_mtu + ETH_HLEN) < ETH_MIN_PACKET_SIZE)) {
  3122. BNX2X_ERR("Can't support requested MTU size\n");
  3123. return -EINVAL;
  3124. }
  3125. /* This does not race with packet allocation
  3126. * because the actual alloc size is
  3127. * only updated as part of load
  3128. */
  3129. dev->mtu = new_mtu;
  3130. return bnx2x_reload_if_running(dev);
  3131. }
  3132. netdev_features_t bnx2x_fix_features(struct net_device *dev,
  3133. netdev_features_t features)
  3134. {
  3135. struct bnx2x *bp = netdev_priv(dev);
  3136. /* TPA requires Rx CSUM offloading */
  3137. if (!(features & NETIF_F_RXCSUM) || bp->disable_tpa) {
  3138. features &= ~NETIF_F_LRO;
  3139. features &= ~NETIF_F_GRO;
  3140. }
  3141. return features;
  3142. }
  3143. int bnx2x_set_features(struct net_device *dev, netdev_features_t features)
  3144. {
  3145. struct bnx2x *bp = netdev_priv(dev);
  3146. u32 flags = bp->flags;
  3147. bool bnx2x_reload = false;
  3148. if (features & NETIF_F_LRO)
  3149. flags |= TPA_ENABLE_FLAG;
  3150. else
  3151. flags &= ~TPA_ENABLE_FLAG;
  3152. if (features & NETIF_F_GRO)
  3153. flags |= GRO_ENABLE_FLAG;
  3154. else
  3155. flags &= ~GRO_ENABLE_FLAG;
  3156. if (features & NETIF_F_LOOPBACK) {
  3157. if (bp->link_params.loopback_mode != LOOPBACK_BMAC) {
  3158. bp->link_params.loopback_mode = LOOPBACK_BMAC;
  3159. bnx2x_reload = true;
  3160. }
  3161. } else {
  3162. if (bp->link_params.loopback_mode != LOOPBACK_NONE) {
  3163. bp->link_params.loopback_mode = LOOPBACK_NONE;
  3164. bnx2x_reload = true;
  3165. }
  3166. }
  3167. if (flags ^ bp->flags) {
  3168. bp->flags = flags;
  3169. bnx2x_reload = true;
  3170. }
  3171. if (bnx2x_reload) {
  3172. if (bp->recovery_state == BNX2X_RECOVERY_DONE)
  3173. return bnx2x_reload_if_running(dev);
  3174. /* else: bnx2x_nic_load() will be called at end of recovery */
  3175. }
  3176. return 0;
  3177. }
  3178. void bnx2x_tx_timeout(struct net_device *dev)
  3179. {
  3180. struct bnx2x *bp = netdev_priv(dev);
  3181. #ifdef BNX2X_STOP_ON_ERROR
  3182. if (!bp->panic)
  3183. bnx2x_panic();
  3184. #endif
  3185. smp_mb__before_clear_bit();
  3186. set_bit(BNX2X_SP_RTNL_TX_TIMEOUT, &bp->sp_rtnl_state);
  3187. smp_mb__after_clear_bit();
  3188. /* This allows the netif to be shutdown gracefully before resetting */
  3189. schedule_delayed_work(&bp->sp_rtnl_task, 0);
  3190. }
  3191. int bnx2x_suspend(struct pci_dev *pdev, pm_message_t state)
  3192. {
  3193. struct net_device *dev = pci_get_drvdata(pdev);
  3194. struct bnx2x *bp;
  3195. if (!dev) {
  3196. dev_err(&pdev->dev, "BAD net device from bnx2x_init_one\n");
  3197. return -ENODEV;
  3198. }
  3199. bp = netdev_priv(dev);
  3200. rtnl_lock();
  3201. pci_save_state(pdev);
  3202. if (!netif_running(dev)) {
  3203. rtnl_unlock();
  3204. return 0;
  3205. }
  3206. netif_device_detach(dev);
  3207. bnx2x_nic_unload(bp, UNLOAD_CLOSE);
  3208. bnx2x_set_power_state(bp, pci_choose_state(pdev, state));
  3209. rtnl_unlock();
  3210. return 0;
  3211. }
  3212. int bnx2x_resume(struct pci_dev *pdev)
  3213. {
  3214. struct net_device *dev = pci_get_drvdata(pdev);
  3215. struct bnx2x *bp;
  3216. int rc;
  3217. if (!dev) {
  3218. dev_err(&pdev->dev, "BAD net device from bnx2x_init_one\n");
  3219. return -ENODEV;
  3220. }
  3221. bp = netdev_priv(dev);
  3222. if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
  3223. BNX2X_ERR("Handling parity error recovery. Try again later\n");
  3224. return -EAGAIN;
  3225. }
  3226. rtnl_lock();
  3227. pci_restore_state(pdev);
  3228. if (!netif_running(dev)) {
  3229. rtnl_unlock();
  3230. return 0;
  3231. }
  3232. bnx2x_set_power_state(bp, PCI_D0);
  3233. netif_device_attach(dev);
  3234. rc = bnx2x_nic_load(bp, LOAD_OPEN);
  3235. rtnl_unlock();
  3236. return rc;
  3237. }
  3238. void bnx2x_set_ctx_validation(struct bnx2x *bp, struct eth_context *cxt,
  3239. u32 cid)
  3240. {
  3241. /* ustorm cxt validation */
  3242. cxt->ustorm_ag_context.cdu_usage =
  3243. CDU_RSRVD_VALUE_TYPE_A(HW_CID(bp, cid),
  3244. CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
  3245. /* xcontext validation */
  3246. cxt->xstorm_ag_context.cdu_reserved =
  3247. CDU_RSRVD_VALUE_TYPE_A(HW_CID(bp, cid),
  3248. CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
  3249. }
  3250. static void storm_memset_hc_timeout(struct bnx2x *bp, u8 port,
  3251. u8 fw_sb_id, u8 sb_index,
  3252. u8 ticks)
  3253. {
  3254. u32 addr = BAR_CSTRORM_INTMEM +
  3255. CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index);
  3256. REG_WR8(bp, addr, ticks);
  3257. DP(NETIF_MSG_IFUP,
  3258. "port %x fw_sb_id %d sb_index %d ticks %d\n",
  3259. port, fw_sb_id, sb_index, ticks);
  3260. }
  3261. static void storm_memset_hc_disable(struct bnx2x *bp, u8 port,
  3262. u16 fw_sb_id, u8 sb_index,
  3263. u8 disable)
  3264. {
  3265. u32 enable_flag = disable ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
  3266. u32 addr = BAR_CSTRORM_INTMEM +
  3267. CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index);
  3268. u16 flags = REG_RD16(bp, addr);
  3269. /* clear and set */
  3270. flags &= ~HC_INDEX_DATA_HC_ENABLED;
  3271. flags |= enable_flag;
  3272. REG_WR16(bp, addr, flags);
  3273. DP(NETIF_MSG_IFUP,
  3274. "port %x fw_sb_id %d sb_index %d disable %d\n",
  3275. port, fw_sb_id, sb_index, disable);
  3276. }
  3277. void bnx2x_update_coalesce_sb_index(struct bnx2x *bp, u8 fw_sb_id,
  3278. u8 sb_index, u8 disable, u16 usec)
  3279. {
  3280. int port = BP_PORT(bp);
  3281. u8 ticks = usec / BNX2X_BTR;
  3282. storm_memset_hc_timeout(bp, port, fw_sb_id, sb_index, ticks);
  3283. disable = disable ? 1 : (usec ? 0 : 1);
  3284. storm_memset_hc_disable(bp, port, fw_sb_id, sb_index, disable);
  3285. }