bnx2x_cmn.c 93 KB

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