bnx2x_cmn.c 92 KB

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