bnad.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Linux network driver for QLogic BR-series Converged Network Adapter.
  4. */
  5. /*
  6. * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
  7. * Copyright (c) 2014-2015 QLogic Corporation
  8. * All rights reserved
  9. * www.qlogic.com
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/in.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/ip.h>
  20. #include <linux/prefetch.h>
  21. #include <linux/module.h>
  22. #include "bnad.h"
  23. #include "bna.h"
  24. #include "cna.h"
  25. static DEFINE_MUTEX(bnad_fwimg_mutex);
  26. /*
  27. * Module params
  28. */
  29. static uint bnad_msix_disable;
  30. module_param(bnad_msix_disable, uint, 0444);
  31. MODULE_PARM_DESC(bnad_msix_disable, "Disable MSIX mode");
  32. static uint bnad_ioc_auto_recover = 1;
  33. module_param(bnad_ioc_auto_recover, uint, 0444);
  34. MODULE_PARM_DESC(bnad_ioc_auto_recover, "Enable / Disable auto recovery");
  35. static uint bna_debugfs_enable = 1;
  36. module_param(bna_debugfs_enable, uint, 0644);
  37. MODULE_PARM_DESC(bna_debugfs_enable, "Enables debugfs feature, default=1,"
  38. " Range[false:0|true:1]");
  39. /*
  40. * Global variables
  41. */
  42. static u32 bnad_rxqs_per_cq = 2;
  43. static atomic_t bna_id;
  44. static const u8 bnad_bcast_addr[] __aligned(2) =
  45. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  46. /*
  47. * Local MACROS
  48. */
  49. #define BNAD_GET_MBOX_IRQ(_bnad) \
  50. (((_bnad)->cfg_flags & BNAD_CF_MSIX) ? \
  51. ((_bnad)->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector) : \
  52. ((_bnad)->pcidev->irq))
  53. #define BNAD_FILL_UNMAPQ_MEM_REQ(_res_info, _num, _size) \
  54. do { \
  55. (_res_info)->res_type = BNA_RES_T_MEM; \
  56. (_res_info)->res_u.mem_info.mem_type = BNA_MEM_T_KVA; \
  57. (_res_info)->res_u.mem_info.num = (_num); \
  58. (_res_info)->res_u.mem_info.len = (_size); \
  59. } while (0)
  60. /*
  61. * Reinitialize completions in CQ, once Rx is taken down
  62. */
  63. static void
  64. bnad_cq_cleanup(struct bnad *bnad, struct bna_ccb *ccb)
  65. {
  66. struct bna_cq_entry *cmpl;
  67. int i;
  68. for (i = 0; i < ccb->q_depth; i++) {
  69. cmpl = &((struct bna_cq_entry *)ccb->sw_q)[i];
  70. cmpl->valid = 0;
  71. }
  72. }
  73. /* Tx Datapath functions */
  74. /* Caller should ensure that the entry at unmap_q[index] is valid */
  75. static u32
  76. bnad_tx_buff_unmap(struct bnad *bnad,
  77. struct bnad_tx_unmap *unmap_q,
  78. u32 q_depth, u32 index)
  79. {
  80. struct bnad_tx_unmap *unmap;
  81. struct sk_buff *skb;
  82. int vector, nvecs;
  83. unmap = &unmap_q[index];
  84. nvecs = unmap->nvecs;
  85. skb = unmap->skb;
  86. unmap->skb = NULL;
  87. unmap->nvecs = 0;
  88. dma_unmap_single(&bnad->pcidev->dev,
  89. dma_unmap_addr(&unmap->vectors[0], dma_addr),
  90. skb_headlen(skb), DMA_TO_DEVICE);
  91. dma_unmap_addr_set(&unmap->vectors[0], dma_addr, 0);
  92. nvecs--;
  93. vector = 0;
  94. while (nvecs) {
  95. vector++;
  96. if (vector == BFI_TX_MAX_VECTORS_PER_WI) {
  97. vector = 0;
  98. BNA_QE_INDX_INC(index, q_depth);
  99. unmap = &unmap_q[index];
  100. }
  101. dma_unmap_page(&bnad->pcidev->dev,
  102. dma_unmap_addr(&unmap->vectors[vector], dma_addr),
  103. dma_unmap_len(&unmap->vectors[vector], dma_len),
  104. DMA_TO_DEVICE);
  105. dma_unmap_addr_set(&unmap->vectors[vector], dma_addr, 0);
  106. nvecs--;
  107. }
  108. BNA_QE_INDX_INC(index, q_depth);
  109. return index;
  110. }
  111. /*
  112. * Frees all pending Tx Bufs
  113. * At this point no activity is expected on the Q,
  114. * so DMA unmap & freeing is fine.
  115. */
  116. static void
  117. bnad_txq_cleanup(struct bnad *bnad, struct bna_tcb *tcb)
  118. {
  119. struct bnad_tx_unmap *unmap_q = tcb->unmap_q;
  120. struct sk_buff *skb;
  121. int i;
  122. for (i = 0; i < tcb->q_depth; i++) {
  123. skb = unmap_q[i].skb;
  124. if (!skb)
  125. continue;
  126. bnad_tx_buff_unmap(bnad, unmap_q, tcb->q_depth, i);
  127. dev_kfree_skb_any(skb);
  128. }
  129. }
  130. /*
  131. * bnad_txcmpl_process : Frees the Tx bufs on Tx completion
  132. * Can be called in a) Interrupt context
  133. * b) Sending context
  134. */
  135. static u32
  136. bnad_txcmpl_process(struct bnad *bnad, struct bna_tcb *tcb)
  137. {
  138. u32 sent_packets = 0, sent_bytes = 0;
  139. u32 wis, unmap_wis, hw_cons, cons, q_depth;
  140. struct bnad_tx_unmap *unmap_q = tcb->unmap_q;
  141. struct bnad_tx_unmap *unmap;
  142. struct sk_buff *skb;
  143. /* Just return if TX is stopped */
  144. if (!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
  145. return 0;
  146. hw_cons = *(tcb->hw_consumer_index);
  147. rmb();
  148. cons = tcb->consumer_index;
  149. q_depth = tcb->q_depth;
  150. wis = BNA_Q_INDEX_CHANGE(cons, hw_cons, q_depth);
  151. BUG_ON(!(wis <= BNA_QE_IN_USE_CNT(tcb, tcb->q_depth)));
  152. while (wis) {
  153. unmap = &unmap_q[cons];
  154. skb = unmap->skb;
  155. sent_packets++;
  156. sent_bytes += skb->len;
  157. unmap_wis = BNA_TXQ_WI_NEEDED(unmap->nvecs);
  158. wis -= unmap_wis;
  159. cons = bnad_tx_buff_unmap(bnad, unmap_q, q_depth, cons);
  160. dev_kfree_skb_any(skb);
  161. }
  162. /* Update consumer pointers. */
  163. tcb->consumer_index = hw_cons;
  164. tcb->txq->tx_packets += sent_packets;
  165. tcb->txq->tx_bytes += sent_bytes;
  166. return sent_packets;
  167. }
  168. static u32
  169. bnad_tx_complete(struct bnad *bnad, struct bna_tcb *tcb)
  170. {
  171. struct net_device *netdev = bnad->netdev;
  172. u32 sent = 0;
  173. if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
  174. return 0;
  175. sent = bnad_txcmpl_process(bnad, tcb);
  176. if (sent) {
  177. if (netif_queue_stopped(netdev) &&
  178. netif_carrier_ok(netdev) &&
  179. BNA_QE_FREE_CNT(tcb, tcb->q_depth) >=
  180. BNAD_NETIF_WAKE_THRESHOLD) {
  181. if (test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)) {
  182. netif_wake_queue(netdev);
  183. BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
  184. }
  185. }
  186. }
  187. if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
  188. bna_ib_ack(tcb->i_dbell, sent);
  189. smp_mb__before_atomic();
  190. clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
  191. return sent;
  192. }
  193. /* MSIX Tx Completion Handler */
  194. static irqreturn_t
  195. bnad_msix_tx(int irq, void *data)
  196. {
  197. struct bna_tcb *tcb = (struct bna_tcb *)data;
  198. struct bnad *bnad = tcb->bnad;
  199. bnad_tx_complete(bnad, tcb);
  200. return IRQ_HANDLED;
  201. }
  202. static inline void
  203. bnad_rxq_alloc_uninit(struct bnad *bnad, struct bna_rcb *rcb)
  204. {
  205. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  206. unmap_q->reuse_pi = -1;
  207. unmap_q->alloc_order = -1;
  208. unmap_q->map_size = 0;
  209. unmap_q->type = BNAD_RXBUF_NONE;
  210. }
  211. /* Default is page-based allocation. Multi-buffer support - TBD */
  212. static int
  213. bnad_rxq_alloc_init(struct bnad *bnad, struct bna_rcb *rcb)
  214. {
  215. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  216. int order;
  217. bnad_rxq_alloc_uninit(bnad, rcb);
  218. order = get_order(rcb->rxq->buffer_size);
  219. unmap_q->type = BNAD_RXBUF_PAGE;
  220. if (bna_is_small_rxq(rcb->id)) {
  221. unmap_q->alloc_order = 0;
  222. unmap_q->map_size = rcb->rxq->buffer_size;
  223. } else {
  224. if (rcb->rxq->multi_buffer) {
  225. unmap_q->alloc_order = 0;
  226. unmap_q->map_size = rcb->rxq->buffer_size;
  227. unmap_q->type = BNAD_RXBUF_MULTI_BUFF;
  228. } else {
  229. unmap_q->alloc_order = order;
  230. unmap_q->map_size =
  231. (rcb->rxq->buffer_size > 2048) ?
  232. PAGE_SIZE << order : 2048;
  233. }
  234. }
  235. BUG_ON((PAGE_SIZE << order) % unmap_q->map_size);
  236. return 0;
  237. }
  238. static inline void
  239. bnad_rxq_cleanup_page(struct bnad *bnad, struct bnad_rx_unmap *unmap)
  240. {
  241. if (!unmap->page)
  242. return;
  243. dma_unmap_page(&bnad->pcidev->dev,
  244. dma_unmap_addr(&unmap->vector, dma_addr),
  245. unmap->vector.len, DMA_FROM_DEVICE);
  246. put_page(unmap->page);
  247. unmap->page = NULL;
  248. dma_unmap_addr_set(&unmap->vector, dma_addr, 0);
  249. unmap->vector.len = 0;
  250. }
  251. static inline void
  252. bnad_rxq_cleanup_skb(struct bnad *bnad, struct bnad_rx_unmap *unmap)
  253. {
  254. if (!unmap->skb)
  255. return;
  256. dma_unmap_single(&bnad->pcidev->dev,
  257. dma_unmap_addr(&unmap->vector, dma_addr),
  258. unmap->vector.len, DMA_FROM_DEVICE);
  259. dev_kfree_skb_any(unmap->skb);
  260. unmap->skb = NULL;
  261. dma_unmap_addr_set(&unmap->vector, dma_addr, 0);
  262. unmap->vector.len = 0;
  263. }
  264. static void
  265. bnad_rxq_cleanup(struct bnad *bnad, struct bna_rcb *rcb)
  266. {
  267. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  268. int i;
  269. for (i = 0; i < rcb->q_depth; i++) {
  270. struct bnad_rx_unmap *unmap = &unmap_q->unmap[i];
  271. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  272. bnad_rxq_cleanup_skb(bnad, unmap);
  273. else
  274. bnad_rxq_cleanup_page(bnad, unmap);
  275. }
  276. bnad_rxq_alloc_uninit(bnad, rcb);
  277. }
  278. static u32
  279. bnad_rxq_refill_page(struct bnad *bnad, struct bna_rcb *rcb, u32 nalloc)
  280. {
  281. u32 alloced, prod, q_depth;
  282. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  283. struct bnad_rx_unmap *unmap, *prev;
  284. struct bna_rxq_entry *rxent;
  285. struct page *page;
  286. u32 page_offset, alloc_size;
  287. dma_addr_t dma_addr;
  288. prod = rcb->producer_index;
  289. q_depth = rcb->q_depth;
  290. alloc_size = PAGE_SIZE << unmap_q->alloc_order;
  291. alloced = 0;
  292. while (nalloc--) {
  293. unmap = &unmap_q->unmap[prod];
  294. if (unmap_q->reuse_pi < 0) {
  295. page = alloc_pages(GFP_ATOMIC | __GFP_COMP,
  296. unmap_q->alloc_order);
  297. page_offset = 0;
  298. } else {
  299. prev = &unmap_q->unmap[unmap_q->reuse_pi];
  300. page = prev->page;
  301. page_offset = prev->page_offset + unmap_q->map_size;
  302. get_page(page);
  303. }
  304. if (unlikely(!page)) {
  305. BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
  306. rcb->rxq->rxbuf_alloc_failed++;
  307. goto finishing;
  308. }
  309. dma_addr = dma_map_page(&bnad->pcidev->dev, page, page_offset,
  310. unmap_q->map_size, DMA_FROM_DEVICE);
  311. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  312. put_page(page);
  313. BNAD_UPDATE_CTR(bnad, rxbuf_map_failed);
  314. rcb->rxq->rxbuf_map_failed++;
  315. goto finishing;
  316. }
  317. unmap->page = page;
  318. unmap->page_offset = page_offset;
  319. dma_unmap_addr_set(&unmap->vector, dma_addr, dma_addr);
  320. unmap->vector.len = unmap_q->map_size;
  321. page_offset += unmap_q->map_size;
  322. if (page_offset < alloc_size)
  323. unmap_q->reuse_pi = prod;
  324. else
  325. unmap_q->reuse_pi = -1;
  326. rxent = &((struct bna_rxq_entry *)rcb->sw_q)[prod];
  327. BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
  328. BNA_QE_INDX_INC(prod, q_depth);
  329. alloced++;
  330. }
  331. finishing:
  332. if (likely(alloced)) {
  333. rcb->producer_index = prod;
  334. smp_mb();
  335. if (likely(test_bit(BNAD_RXQ_POST_OK, &rcb->flags)))
  336. bna_rxq_prod_indx_doorbell(rcb);
  337. }
  338. return alloced;
  339. }
  340. static u32
  341. bnad_rxq_refill_skb(struct bnad *bnad, struct bna_rcb *rcb, u32 nalloc)
  342. {
  343. u32 alloced, prod, q_depth, buff_sz;
  344. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  345. struct bnad_rx_unmap *unmap;
  346. struct bna_rxq_entry *rxent;
  347. struct sk_buff *skb;
  348. dma_addr_t dma_addr;
  349. buff_sz = rcb->rxq->buffer_size;
  350. prod = rcb->producer_index;
  351. q_depth = rcb->q_depth;
  352. alloced = 0;
  353. while (nalloc--) {
  354. unmap = &unmap_q->unmap[prod];
  355. skb = netdev_alloc_skb_ip_align(bnad->netdev, buff_sz);
  356. if (unlikely(!skb)) {
  357. BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
  358. rcb->rxq->rxbuf_alloc_failed++;
  359. goto finishing;
  360. }
  361. dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
  362. buff_sz, DMA_FROM_DEVICE);
  363. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  364. dev_kfree_skb_any(skb);
  365. BNAD_UPDATE_CTR(bnad, rxbuf_map_failed);
  366. rcb->rxq->rxbuf_map_failed++;
  367. goto finishing;
  368. }
  369. unmap->skb = skb;
  370. dma_unmap_addr_set(&unmap->vector, dma_addr, dma_addr);
  371. unmap->vector.len = buff_sz;
  372. rxent = &((struct bna_rxq_entry *)rcb->sw_q)[prod];
  373. BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
  374. BNA_QE_INDX_INC(prod, q_depth);
  375. alloced++;
  376. }
  377. finishing:
  378. if (likely(alloced)) {
  379. rcb->producer_index = prod;
  380. smp_mb();
  381. if (likely(test_bit(BNAD_RXQ_POST_OK, &rcb->flags)))
  382. bna_rxq_prod_indx_doorbell(rcb);
  383. }
  384. return alloced;
  385. }
  386. static inline void
  387. bnad_rxq_post(struct bnad *bnad, struct bna_rcb *rcb)
  388. {
  389. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  390. u32 to_alloc;
  391. to_alloc = BNA_QE_FREE_CNT(rcb, rcb->q_depth);
  392. if (!(to_alloc >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT))
  393. return;
  394. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  395. bnad_rxq_refill_skb(bnad, rcb, to_alloc);
  396. else
  397. bnad_rxq_refill_page(bnad, rcb, to_alloc);
  398. }
  399. #define flags_cksum_prot_mask (BNA_CQ_EF_IPV4 | BNA_CQ_EF_L3_CKSUM_OK | \
  400. BNA_CQ_EF_IPV6 | \
  401. BNA_CQ_EF_TCP | BNA_CQ_EF_UDP | \
  402. BNA_CQ_EF_L4_CKSUM_OK)
  403. #define flags_tcp4 (BNA_CQ_EF_IPV4 | BNA_CQ_EF_L3_CKSUM_OK | \
  404. BNA_CQ_EF_TCP | BNA_CQ_EF_L4_CKSUM_OK)
  405. #define flags_tcp6 (BNA_CQ_EF_IPV6 | \
  406. BNA_CQ_EF_TCP | BNA_CQ_EF_L4_CKSUM_OK)
  407. #define flags_udp4 (BNA_CQ_EF_IPV4 | BNA_CQ_EF_L3_CKSUM_OK | \
  408. BNA_CQ_EF_UDP | BNA_CQ_EF_L4_CKSUM_OK)
  409. #define flags_udp6 (BNA_CQ_EF_IPV6 | \
  410. BNA_CQ_EF_UDP | BNA_CQ_EF_L4_CKSUM_OK)
  411. static void
  412. bnad_cq_drop_packet(struct bnad *bnad, struct bna_rcb *rcb,
  413. u32 sop_ci, u32 nvecs)
  414. {
  415. struct bnad_rx_unmap_q *unmap_q;
  416. struct bnad_rx_unmap *unmap;
  417. u32 ci, vec;
  418. unmap_q = rcb->unmap_q;
  419. for (vec = 0, ci = sop_ci; vec < nvecs; vec++) {
  420. unmap = &unmap_q->unmap[ci];
  421. BNA_QE_INDX_INC(ci, rcb->q_depth);
  422. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  423. bnad_rxq_cleanup_skb(bnad, unmap);
  424. else
  425. bnad_rxq_cleanup_page(bnad, unmap);
  426. }
  427. }
  428. static void
  429. bnad_cq_setup_skb_frags(struct bna_ccb *ccb, struct sk_buff *skb, u32 nvecs)
  430. {
  431. struct bna_rcb *rcb;
  432. struct bnad *bnad;
  433. struct bnad_rx_unmap_q *unmap_q;
  434. struct bna_cq_entry *cq, *cmpl;
  435. u32 ci, pi, totlen = 0;
  436. cq = ccb->sw_q;
  437. pi = ccb->producer_index;
  438. cmpl = &cq[pi];
  439. rcb = bna_is_small_rxq(cmpl->rxq_id) ? ccb->rcb[1] : ccb->rcb[0];
  440. unmap_q = rcb->unmap_q;
  441. bnad = rcb->bnad;
  442. ci = rcb->consumer_index;
  443. /* prefetch header */
  444. prefetch(page_address(unmap_q->unmap[ci].page) +
  445. unmap_q->unmap[ci].page_offset);
  446. while (nvecs--) {
  447. struct bnad_rx_unmap *unmap;
  448. u32 len;
  449. unmap = &unmap_q->unmap[ci];
  450. BNA_QE_INDX_INC(ci, rcb->q_depth);
  451. dma_unmap_page(&bnad->pcidev->dev,
  452. dma_unmap_addr(&unmap->vector, dma_addr),
  453. unmap->vector.len, DMA_FROM_DEVICE);
  454. len = ntohs(cmpl->length);
  455. skb->truesize += unmap->vector.len;
  456. totlen += len;
  457. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  458. unmap->page, unmap->page_offset, len);
  459. unmap->page = NULL;
  460. unmap->vector.len = 0;
  461. BNA_QE_INDX_INC(pi, ccb->q_depth);
  462. cmpl = &cq[pi];
  463. }
  464. skb->len += totlen;
  465. skb->data_len += totlen;
  466. }
  467. static inline void
  468. bnad_cq_setup_skb(struct bnad *bnad, struct sk_buff *skb,
  469. struct bnad_rx_unmap *unmap, u32 len)
  470. {
  471. prefetch(skb->data);
  472. dma_unmap_single(&bnad->pcidev->dev,
  473. dma_unmap_addr(&unmap->vector, dma_addr),
  474. unmap->vector.len, DMA_FROM_DEVICE);
  475. skb_put(skb, len);
  476. skb->protocol = eth_type_trans(skb, bnad->netdev);
  477. unmap->skb = NULL;
  478. unmap->vector.len = 0;
  479. }
  480. static u32
  481. bnad_cq_process(struct bnad *bnad, struct bna_ccb *ccb, int budget)
  482. {
  483. struct bna_cq_entry *cq, *cmpl, *next_cmpl;
  484. struct bna_rcb *rcb = NULL;
  485. struct bnad_rx_unmap_q *unmap_q;
  486. struct bnad_rx_unmap *unmap = NULL;
  487. struct sk_buff *skb = NULL;
  488. struct bna_pkt_rate *pkt_rt = &ccb->pkt_rate;
  489. struct bnad_rx_ctrl *rx_ctrl = ccb->ctrl;
  490. u32 packets = 0, len = 0, totlen = 0;
  491. u32 pi, vec, sop_ci = 0, nvecs = 0;
  492. u32 flags, masked_flags;
  493. prefetch(bnad->netdev);
  494. cq = ccb->sw_q;
  495. while (packets < budget) {
  496. cmpl = &cq[ccb->producer_index];
  497. if (!cmpl->valid)
  498. break;
  499. /* The 'valid' field is set by the adapter, only after writing
  500. * the other fields of completion entry. Hence, do not load
  501. * other fields of completion entry *before* the 'valid' is
  502. * loaded. Adding the rmb() here prevents the compiler and/or
  503. * CPU from reordering the reads which would potentially result
  504. * in reading stale values in completion entry.
  505. */
  506. rmb();
  507. BNA_UPDATE_PKT_CNT(pkt_rt, ntohs(cmpl->length));
  508. if (bna_is_small_rxq(cmpl->rxq_id))
  509. rcb = ccb->rcb[1];
  510. else
  511. rcb = ccb->rcb[0];
  512. unmap_q = rcb->unmap_q;
  513. /* start of packet ci */
  514. sop_ci = rcb->consumer_index;
  515. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type)) {
  516. unmap = &unmap_q->unmap[sop_ci];
  517. skb = unmap->skb;
  518. } else {
  519. skb = napi_get_frags(&rx_ctrl->napi);
  520. if (unlikely(!skb))
  521. break;
  522. }
  523. prefetch(skb);
  524. flags = ntohl(cmpl->flags);
  525. len = ntohs(cmpl->length);
  526. totlen = len;
  527. nvecs = 1;
  528. /* Check all the completions for this frame.
  529. * busy-wait doesn't help much, break here.
  530. */
  531. if (BNAD_RXBUF_IS_MULTI_BUFF(unmap_q->type) &&
  532. (flags & BNA_CQ_EF_EOP) == 0) {
  533. pi = ccb->producer_index;
  534. do {
  535. BNA_QE_INDX_INC(pi, ccb->q_depth);
  536. next_cmpl = &cq[pi];
  537. if (!next_cmpl->valid)
  538. break;
  539. /* The 'valid' field is set by the adapter, only
  540. * after writing the other fields of completion
  541. * entry. Hence, do not load other fields of
  542. * completion entry *before* the 'valid' is
  543. * loaded. Adding the rmb() here prevents the
  544. * compiler and/or CPU from reordering the reads
  545. * which would potentially result in reading
  546. * stale values in completion entry.
  547. */
  548. rmb();
  549. len = ntohs(next_cmpl->length);
  550. flags = ntohl(next_cmpl->flags);
  551. nvecs++;
  552. totlen += len;
  553. } while ((flags & BNA_CQ_EF_EOP) == 0);
  554. if (!next_cmpl->valid)
  555. break;
  556. }
  557. packets++;
  558. /* TODO: BNA_CQ_EF_LOCAL ? */
  559. if (unlikely(flags & (BNA_CQ_EF_MAC_ERROR |
  560. BNA_CQ_EF_FCS_ERROR |
  561. BNA_CQ_EF_TOO_LONG))) {
  562. bnad_cq_drop_packet(bnad, rcb, sop_ci, nvecs);
  563. rcb->rxq->rx_packets_with_error++;
  564. goto next;
  565. }
  566. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  567. bnad_cq_setup_skb(bnad, skb, unmap, len);
  568. else
  569. bnad_cq_setup_skb_frags(ccb, skb, nvecs);
  570. rcb->rxq->rx_packets++;
  571. rcb->rxq->rx_bytes += totlen;
  572. ccb->bytes_per_intr += totlen;
  573. masked_flags = flags & flags_cksum_prot_mask;
  574. if (likely
  575. ((bnad->netdev->features & NETIF_F_RXCSUM) &&
  576. ((masked_flags == flags_tcp4) ||
  577. (masked_flags == flags_udp4) ||
  578. (masked_flags == flags_tcp6) ||
  579. (masked_flags == flags_udp6))))
  580. skb->ip_summed = CHECKSUM_UNNECESSARY;
  581. else
  582. skb_checksum_none_assert(skb);
  583. if ((flags & BNA_CQ_EF_VLAN) &&
  584. (bnad->netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  585. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(cmpl->vlan_tag));
  586. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  587. netif_receive_skb(skb);
  588. else
  589. napi_gro_frags(&rx_ctrl->napi);
  590. next:
  591. BNA_QE_INDX_ADD(rcb->consumer_index, nvecs, rcb->q_depth);
  592. for (vec = 0; vec < nvecs; vec++) {
  593. cmpl = &cq[ccb->producer_index];
  594. cmpl->valid = 0;
  595. BNA_QE_INDX_INC(ccb->producer_index, ccb->q_depth);
  596. }
  597. }
  598. napi_gro_flush(&rx_ctrl->napi, false);
  599. if (likely(test_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags)))
  600. bna_ib_ack_disable_irq(ccb->i_dbell, packets);
  601. bnad_rxq_post(bnad, ccb->rcb[0]);
  602. if (ccb->rcb[1])
  603. bnad_rxq_post(bnad, ccb->rcb[1]);
  604. return packets;
  605. }
  606. static void
  607. bnad_netif_rx_schedule_poll(struct bnad *bnad, struct bna_ccb *ccb)
  608. {
  609. struct bnad_rx_ctrl *rx_ctrl = (struct bnad_rx_ctrl *)(ccb->ctrl);
  610. struct napi_struct *napi = &rx_ctrl->napi;
  611. if (likely(napi_schedule_prep(napi))) {
  612. __napi_schedule(napi);
  613. rx_ctrl->rx_schedule++;
  614. }
  615. }
  616. /* MSIX Rx Path Handler */
  617. static irqreturn_t
  618. bnad_msix_rx(int irq, void *data)
  619. {
  620. struct bna_ccb *ccb = (struct bna_ccb *)data;
  621. if (ccb) {
  622. ((struct bnad_rx_ctrl *)ccb->ctrl)->rx_intr_ctr++;
  623. bnad_netif_rx_schedule_poll(ccb->bnad, ccb);
  624. }
  625. return IRQ_HANDLED;
  626. }
  627. /* Interrupt handlers */
  628. /* Mbox Interrupt Handlers */
  629. static irqreturn_t
  630. bnad_msix_mbox_handler(int irq, void *data)
  631. {
  632. u32 intr_status;
  633. unsigned long flags;
  634. struct bnad *bnad = (struct bnad *)data;
  635. spin_lock_irqsave(&bnad->bna_lock, flags);
  636. if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))) {
  637. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  638. return IRQ_HANDLED;
  639. }
  640. bna_intr_status_get(&bnad->bna, intr_status);
  641. if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
  642. bna_mbox_handler(&bnad->bna, intr_status);
  643. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  644. return IRQ_HANDLED;
  645. }
  646. static irqreturn_t
  647. bnad_isr(int irq, void *data)
  648. {
  649. int i, j;
  650. u32 intr_status;
  651. unsigned long flags;
  652. struct bnad *bnad = (struct bnad *)data;
  653. struct bnad_rx_info *rx_info;
  654. struct bnad_rx_ctrl *rx_ctrl;
  655. struct bna_tcb *tcb = NULL;
  656. spin_lock_irqsave(&bnad->bna_lock, flags);
  657. if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))) {
  658. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  659. return IRQ_NONE;
  660. }
  661. bna_intr_status_get(&bnad->bna, intr_status);
  662. if (unlikely(!intr_status)) {
  663. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  664. return IRQ_NONE;
  665. }
  666. if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
  667. bna_mbox_handler(&bnad->bna, intr_status);
  668. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  669. if (!BNA_IS_INTX_DATA_INTR(intr_status))
  670. return IRQ_HANDLED;
  671. /* Process data interrupts */
  672. /* Tx processing */
  673. for (i = 0; i < bnad->num_tx; i++) {
  674. for (j = 0; j < bnad->num_txq_per_tx; j++) {
  675. tcb = bnad->tx_info[i].tcb[j];
  676. if (tcb && test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
  677. bnad_tx_complete(bnad, bnad->tx_info[i].tcb[j]);
  678. }
  679. }
  680. /* Rx processing */
  681. for (i = 0; i < bnad->num_rx; i++) {
  682. rx_info = &bnad->rx_info[i];
  683. if (!rx_info->rx)
  684. continue;
  685. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  686. rx_ctrl = &rx_info->rx_ctrl[j];
  687. if (rx_ctrl->ccb)
  688. bnad_netif_rx_schedule_poll(bnad,
  689. rx_ctrl->ccb);
  690. }
  691. }
  692. return IRQ_HANDLED;
  693. }
  694. /*
  695. * Called in interrupt / callback context
  696. * with bna_lock held, so cfg_flags access is OK
  697. */
  698. static void
  699. bnad_enable_mbox_irq(struct bnad *bnad)
  700. {
  701. clear_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
  702. BNAD_UPDATE_CTR(bnad, mbox_intr_enabled);
  703. }
  704. /*
  705. * Called with bnad->bna_lock held b'cos of
  706. * bnad->cfg_flags access.
  707. */
  708. static void
  709. bnad_disable_mbox_irq(struct bnad *bnad)
  710. {
  711. set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
  712. BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
  713. }
  714. static void
  715. bnad_set_netdev_perm_addr(struct bnad *bnad)
  716. {
  717. struct net_device *netdev = bnad->netdev;
  718. ether_addr_copy(netdev->perm_addr, bnad->perm_addr);
  719. if (is_zero_ether_addr(netdev->dev_addr))
  720. eth_hw_addr_set(netdev, bnad->perm_addr);
  721. }
  722. /* Control Path Handlers */
  723. /* Callbacks */
  724. void
  725. bnad_cb_mbox_intr_enable(struct bnad *bnad)
  726. {
  727. bnad_enable_mbox_irq(bnad);
  728. }
  729. void
  730. bnad_cb_mbox_intr_disable(struct bnad *bnad)
  731. {
  732. bnad_disable_mbox_irq(bnad);
  733. }
  734. void
  735. bnad_cb_ioceth_ready(struct bnad *bnad)
  736. {
  737. bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
  738. complete(&bnad->bnad_completions.ioc_comp);
  739. }
  740. void
  741. bnad_cb_ioceth_failed(struct bnad *bnad)
  742. {
  743. bnad->bnad_completions.ioc_comp_status = BNA_CB_FAIL;
  744. complete(&bnad->bnad_completions.ioc_comp);
  745. }
  746. void
  747. bnad_cb_ioceth_disabled(struct bnad *bnad)
  748. {
  749. bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
  750. complete(&bnad->bnad_completions.ioc_comp);
  751. }
  752. static void
  753. bnad_cb_enet_disabled(void *arg)
  754. {
  755. struct bnad *bnad = (struct bnad *)arg;
  756. netif_carrier_off(bnad->netdev);
  757. complete(&bnad->bnad_completions.enet_comp);
  758. }
  759. void
  760. bnad_cb_ethport_link_status(struct bnad *bnad,
  761. enum bna_link_status link_status)
  762. {
  763. bool link_up = false;
  764. link_up = (link_status == BNA_LINK_UP) || (link_status == BNA_CEE_UP);
  765. if (link_status == BNA_CEE_UP) {
  766. if (!test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
  767. BNAD_UPDATE_CTR(bnad, cee_toggle);
  768. set_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
  769. } else {
  770. if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
  771. BNAD_UPDATE_CTR(bnad, cee_toggle);
  772. clear_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
  773. }
  774. if (link_up) {
  775. if (!netif_carrier_ok(bnad->netdev)) {
  776. uint tx_id, tcb_id;
  777. netdev_info(bnad->netdev, "link up\n");
  778. netif_carrier_on(bnad->netdev);
  779. BNAD_UPDATE_CTR(bnad, link_toggle);
  780. for (tx_id = 0; tx_id < bnad->num_tx; tx_id++) {
  781. for (tcb_id = 0; tcb_id < bnad->num_txq_per_tx;
  782. tcb_id++) {
  783. struct bna_tcb *tcb =
  784. bnad->tx_info[tx_id].tcb[tcb_id];
  785. u32 txq_id;
  786. if (!tcb)
  787. continue;
  788. txq_id = tcb->id;
  789. if (test_bit(BNAD_TXQ_TX_STARTED,
  790. &tcb->flags)) {
  791. /*
  792. * Force an immediate
  793. * Transmit Schedule */
  794. netif_wake_subqueue(
  795. bnad->netdev,
  796. txq_id);
  797. BNAD_UPDATE_CTR(bnad,
  798. netif_queue_wakeup);
  799. } else {
  800. netif_stop_subqueue(
  801. bnad->netdev,
  802. txq_id);
  803. BNAD_UPDATE_CTR(bnad,
  804. netif_queue_stop);
  805. }
  806. }
  807. }
  808. }
  809. } else {
  810. if (netif_carrier_ok(bnad->netdev)) {
  811. netdev_info(bnad->netdev, "link down\n");
  812. netif_carrier_off(bnad->netdev);
  813. BNAD_UPDATE_CTR(bnad, link_toggle);
  814. }
  815. }
  816. }
  817. static void
  818. bnad_cb_tx_disabled(void *arg, struct bna_tx *tx)
  819. {
  820. struct bnad *bnad = (struct bnad *)arg;
  821. complete(&bnad->bnad_completions.tx_comp);
  822. }
  823. static void
  824. bnad_cb_tcb_setup(struct bnad *bnad, struct bna_tcb *tcb)
  825. {
  826. struct bnad_tx_info *tx_info =
  827. (struct bnad_tx_info *)tcb->txq->tx->priv;
  828. tcb->priv = tcb;
  829. tx_info->tcb[tcb->id] = tcb;
  830. }
  831. static void
  832. bnad_cb_tcb_destroy(struct bnad *bnad, struct bna_tcb *tcb)
  833. {
  834. struct bnad_tx_info *tx_info =
  835. (struct bnad_tx_info *)tcb->txq->tx->priv;
  836. tx_info->tcb[tcb->id] = NULL;
  837. tcb->priv = NULL;
  838. }
  839. static void
  840. bnad_cb_ccb_setup(struct bnad *bnad, struct bna_ccb *ccb)
  841. {
  842. struct bnad_rx_info *rx_info =
  843. (struct bnad_rx_info *)ccb->cq->rx->priv;
  844. rx_info->rx_ctrl[ccb->id].ccb = ccb;
  845. ccb->ctrl = &rx_info->rx_ctrl[ccb->id];
  846. }
  847. static void
  848. bnad_cb_ccb_destroy(struct bnad *bnad, struct bna_ccb *ccb)
  849. {
  850. struct bnad_rx_info *rx_info =
  851. (struct bnad_rx_info *)ccb->cq->rx->priv;
  852. rx_info->rx_ctrl[ccb->id].ccb = NULL;
  853. }
  854. static void
  855. bnad_cb_tx_stall(struct bnad *bnad, struct bna_tx *tx)
  856. {
  857. struct bnad_tx_info *tx_info = tx->priv;
  858. struct bna_tcb *tcb;
  859. u32 txq_id;
  860. int i;
  861. for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
  862. tcb = tx_info->tcb[i];
  863. if (!tcb)
  864. continue;
  865. txq_id = tcb->id;
  866. clear_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
  867. netif_stop_subqueue(bnad->netdev, txq_id);
  868. }
  869. }
  870. static void
  871. bnad_cb_tx_resume(struct bnad *bnad, struct bna_tx *tx)
  872. {
  873. struct bnad_tx_info *tx_info = tx->priv;
  874. struct bna_tcb *tcb;
  875. u32 txq_id;
  876. int i;
  877. for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
  878. tcb = tx_info->tcb[i];
  879. if (!tcb)
  880. continue;
  881. txq_id = tcb->id;
  882. BUG_ON(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags));
  883. set_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
  884. BUG_ON(*(tcb->hw_consumer_index) != 0);
  885. if (netif_carrier_ok(bnad->netdev)) {
  886. netif_wake_subqueue(bnad->netdev, txq_id);
  887. BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
  888. }
  889. }
  890. /*
  891. * Workaround for first ioceth enable failure & we
  892. * get a 0 MAC address. We try to get the MAC address
  893. * again here.
  894. */
  895. if (is_zero_ether_addr(bnad->perm_addr)) {
  896. bna_enet_perm_mac_get(&bnad->bna.enet, bnad->perm_addr);
  897. bnad_set_netdev_perm_addr(bnad);
  898. }
  899. }
  900. /*
  901. * Free all TxQs buffers and then notify TX_E_CLEANUP_DONE to Tx fsm.
  902. */
  903. static void
  904. bnad_tx_cleanup(struct work_struct *work)
  905. {
  906. struct bnad_tx_info *tx_info =
  907. container_of(work, struct bnad_tx_info, tx_cleanup_work.work);
  908. struct bnad *bnad = NULL;
  909. struct bna_tcb *tcb;
  910. unsigned long flags;
  911. u32 i, pending = 0;
  912. for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
  913. tcb = tx_info->tcb[i];
  914. if (!tcb)
  915. continue;
  916. bnad = tcb->bnad;
  917. if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
  918. pending++;
  919. continue;
  920. }
  921. bnad_txq_cleanup(bnad, tcb);
  922. smp_mb__before_atomic();
  923. clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
  924. }
  925. if (pending) {
  926. queue_delayed_work(bnad->work_q, &tx_info->tx_cleanup_work,
  927. msecs_to_jiffies(1));
  928. return;
  929. }
  930. spin_lock_irqsave(&bnad->bna_lock, flags);
  931. bna_tx_cleanup_complete(tx_info->tx);
  932. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  933. }
  934. static void
  935. bnad_cb_tx_cleanup(struct bnad *bnad, struct bna_tx *tx)
  936. {
  937. struct bnad_tx_info *tx_info = tx->priv;
  938. struct bna_tcb *tcb;
  939. int i;
  940. for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
  941. tcb = tx_info->tcb[i];
  942. if (!tcb)
  943. continue;
  944. }
  945. queue_delayed_work(bnad->work_q, &tx_info->tx_cleanup_work, 0);
  946. }
  947. static void
  948. bnad_cb_rx_stall(struct bnad *bnad, struct bna_rx *rx)
  949. {
  950. struct bnad_rx_info *rx_info = rx->priv;
  951. struct bna_ccb *ccb;
  952. struct bnad_rx_ctrl *rx_ctrl;
  953. int i;
  954. for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
  955. rx_ctrl = &rx_info->rx_ctrl[i];
  956. ccb = rx_ctrl->ccb;
  957. if (!ccb)
  958. continue;
  959. clear_bit(BNAD_RXQ_POST_OK, &ccb->rcb[0]->flags);
  960. if (ccb->rcb[1])
  961. clear_bit(BNAD_RXQ_POST_OK, &ccb->rcb[1]->flags);
  962. }
  963. }
  964. /*
  965. * Free all RxQs buffers and then notify RX_E_CLEANUP_DONE to Rx fsm.
  966. */
  967. static void
  968. bnad_rx_cleanup(struct work_struct *work)
  969. {
  970. struct bnad_rx_info *rx_info =
  971. container_of(work, struct bnad_rx_info, rx_cleanup_work);
  972. struct bnad_rx_ctrl *rx_ctrl;
  973. struct bnad *bnad = NULL;
  974. unsigned long flags;
  975. u32 i;
  976. for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
  977. rx_ctrl = &rx_info->rx_ctrl[i];
  978. if (!rx_ctrl->ccb)
  979. continue;
  980. bnad = rx_ctrl->ccb->bnad;
  981. /*
  982. * Wait till the poll handler has exited
  983. * and nothing can be scheduled anymore
  984. */
  985. napi_disable(&rx_ctrl->napi);
  986. bnad_cq_cleanup(bnad, rx_ctrl->ccb);
  987. bnad_rxq_cleanup(bnad, rx_ctrl->ccb->rcb[0]);
  988. if (rx_ctrl->ccb->rcb[1])
  989. bnad_rxq_cleanup(bnad, rx_ctrl->ccb->rcb[1]);
  990. }
  991. spin_lock_irqsave(&bnad->bna_lock, flags);
  992. bna_rx_cleanup_complete(rx_info->rx);
  993. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  994. }
  995. static void
  996. bnad_cb_rx_cleanup(struct bnad *bnad, struct bna_rx *rx)
  997. {
  998. struct bnad_rx_info *rx_info = rx->priv;
  999. struct bna_ccb *ccb;
  1000. struct bnad_rx_ctrl *rx_ctrl;
  1001. int i;
  1002. for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
  1003. rx_ctrl = &rx_info->rx_ctrl[i];
  1004. ccb = rx_ctrl->ccb;
  1005. if (!ccb)
  1006. continue;
  1007. clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags);
  1008. if (ccb->rcb[1])
  1009. clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[1]->flags);
  1010. }
  1011. queue_work(bnad->work_q, &rx_info->rx_cleanup_work);
  1012. }
  1013. static void
  1014. bnad_cb_rx_post(struct bnad *bnad, struct bna_rx *rx)
  1015. {
  1016. struct bnad_rx_info *rx_info = rx->priv;
  1017. struct bna_ccb *ccb;
  1018. struct bna_rcb *rcb;
  1019. struct bnad_rx_ctrl *rx_ctrl;
  1020. int i, j;
  1021. for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
  1022. rx_ctrl = &rx_info->rx_ctrl[i];
  1023. ccb = rx_ctrl->ccb;
  1024. if (!ccb)
  1025. continue;
  1026. napi_enable(&rx_ctrl->napi);
  1027. for (j = 0; j < BNAD_MAX_RXQ_PER_RXP; j++) {
  1028. rcb = ccb->rcb[j];
  1029. if (!rcb)
  1030. continue;
  1031. bnad_rxq_alloc_init(bnad, rcb);
  1032. set_bit(BNAD_RXQ_STARTED, &rcb->flags);
  1033. set_bit(BNAD_RXQ_POST_OK, &rcb->flags);
  1034. bnad_rxq_post(bnad, rcb);
  1035. }
  1036. }
  1037. }
  1038. static void
  1039. bnad_cb_rx_disabled(void *arg, struct bna_rx *rx)
  1040. {
  1041. struct bnad *bnad = (struct bnad *)arg;
  1042. complete(&bnad->bnad_completions.rx_comp);
  1043. }
  1044. static void
  1045. bnad_cb_rx_mcast_add(struct bnad *bnad, struct bna_rx *rx)
  1046. {
  1047. bnad->bnad_completions.mcast_comp_status = BNA_CB_SUCCESS;
  1048. complete(&bnad->bnad_completions.mcast_comp);
  1049. }
  1050. void
  1051. bnad_cb_stats_get(struct bnad *bnad, enum bna_cb_status status,
  1052. struct bna_stats *stats)
  1053. {
  1054. if (status == BNA_CB_SUCCESS)
  1055. BNAD_UPDATE_CTR(bnad, hw_stats_updates);
  1056. if (!netif_running(bnad->netdev) ||
  1057. !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
  1058. return;
  1059. mod_timer(&bnad->stats_timer,
  1060. jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
  1061. }
  1062. static void
  1063. bnad_cb_enet_mtu_set(struct bnad *bnad)
  1064. {
  1065. bnad->bnad_completions.mtu_comp_status = BNA_CB_SUCCESS;
  1066. complete(&bnad->bnad_completions.mtu_comp);
  1067. }
  1068. void
  1069. bnad_cb_completion(void *arg, enum bfa_status status)
  1070. {
  1071. struct bnad_iocmd_comp *iocmd_comp =
  1072. (struct bnad_iocmd_comp *)arg;
  1073. iocmd_comp->comp_status = (u32) status;
  1074. complete(&iocmd_comp->comp);
  1075. }
  1076. /* Resource allocation, free functions */
  1077. static void
  1078. bnad_mem_free(struct bnad *bnad,
  1079. struct bna_mem_info *mem_info)
  1080. {
  1081. int i;
  1082. dma_addr_t dma_pa;
  1083. if (mem_info->mdl == NULL)
  1084. return;
  1085. for (i = 0; i < mem_info->num; i++) {
  1086. if (mem_info->mdl[i].kva != NULL) {
  1087. if (mem_info->mem_type == BNA_MEM_T_DMA) {
  1088. BNA_GET_DMA_ADDR(&(mem_info->mdl[i].dma),
  1089. dma_pa);
  1090. dma_free_coherent(&bnad->pcidev->dev,
  1091. mem_info->mdl[i].len,
  1092. mem_info->mdl[i].kva, dma_pa);
  1093. } else
  1094. kfree(mem_info->mdl[i].kva);
  1095. }
  1096. }
  1097. kfree(mem_info->mdl);
  1098. mem_info->mdl = NULL;
  1099. }
  1100. static int
  1101. bnad_mem_alloc(struct bnad *bnad,
  1102. struct bna_mem_info *mem_info)
  1103. {
  1104. int i;
  1105. dma_addr_t dma_pa;
  1106. if ((mem_info->num == 0) || (mem_info->len == 0)) {
  1107. mem_info->mdl = NULL;
  1108. return 0;
  1109. }
  1110. mem_info->mdl = kcalloc(mem_info->num, sizeof(struct bna_mem_descr),
  1111. GFP_KERNEL);
  1112. if (mem_info->mdl == NULL)
  1113. return -ENOMEM;
  1114. if (mem_info->mem_type == BNA_MEM_T_DMA) {
  1115. for (i = 0; i < mem_info->num; i++) {
  1116. mem_info->mdl[i].len = mem_info->len;
  1117. mem_info->mdl[i].kva =
  1118. dma_alloc_coherent(&bnad->pcidev->dev,
  1119. mem_info->len, &dma_pa,
  1120. GFP_KERNEL);
  1121. if (mem_info->mdl[i].kva == NULL)
  1122. goto err_return;
  1123. BNA_SET_DMA_ADDR(dma_pa,
  1124. &(mem_info->mdl[i].dma));
  1125. }
  1126. } else {
  1127. for (i = 0; i < mem_info->num; i++) {
  1128. mem_info->mdl[i].len = mem_info->len;
  1129. mem_info->mdl[i].kva = kzalloc(mem_info->len,
  1130. GFP_KERNEL);
  1131. if (mem_info->mdl[i].kva == NULL)
  1132. goto err_return;
  1133. }
  1134. }
  1135. return 0;
  1136. err_return:
  1137. bnad_mem_free(bnad, mem_info);
  1138. return -ENOMEM;
  1139. }
  1140. /* Free IRQ for Mailbox */
  1141. static void
  1142. bnad_mbox_irq_free(struct bnad *bnad)
  1143. {
  1144. int irq;
  1145. unsigned long flags;
  1146. spin_lock_irqsave(&bnad->bna_lock, flags);
  1147. bnad_disable_mbox_irq(bnad);
  1148. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1149. irq = BNAD_GET_MBOX_IRQ(bnad);
  1150. free_irq(irq, bnad);
  1151. }
  1152. /*
  1153. * Allocates IRQ for Mailbox, but keep it disabled
  1154. * This will be enabled once we get the mbox enable callback
  1155. * from bna
  1156. */
  1157. static int
  1158. bnad_mbox_irq_alloc(struct bnad *bnad)
  1159. {
  1160. int err = 0;
  1161. unsigned long irq_flags, flags;
  1162. u32 irq;
  1163. irq_handler_t irq_handler;
  1164. spin_lock_irqsave(&bnad->bna_lock, flags);
  1165. if (bnad->cfg_flags & BNAD_CF_MSIX) {
  1166. irq_handler = (irq_handler_t)bnad_msix_mbox_handler;
  1167. irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
  1168. irq_flags = 0;
  1169. } else {
  1170. irq_handler = (irq_handler_t)bnad_isr;
  1171. irq = bnad->pcidev->irq;
  1172. irq_flags = IRQF_SHARED;
  1173. }
  1174. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1175. sprintf(bnad->mbox_irq_name, "%s", BNAD_NAME);
  1176. /*
  1177. * Set the Mbox IRQ disable flag, so that the IRQ handler
  1178. * called from request_irq() for SHARED IRQs do not execute
  1179. */
  1180. set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
  1181. BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
  1182. err = request_irq(irq, irq_handler, irq_flags,
  1183. bnad->mbox_irq_name, bnad);
  1184. return err;
  1185. }
  1186. static void
  1187. bnad_txrx_irq_free(struct bnad *bnad, struct bna_intr_info *intr_info)
  1188. {
  1189. kfree(intr_info->idl);
  1190. intr_info->idl = NULL;
  1191. }
  1192. /* Allocates Interrupt Descriptor List for MSIX/INT-X vectors */
  1193. static int
  1194. bnad_txrx_irq_alloc(struct bnad *bnad, enum bnad_intr_source src,
  1195. u32 txrx_id, struct bna_intr_info *intr_info)
  1196. {
  1197. int i, vector_start = 0;
  1198. u32 cfg_flags;
  1199. unsigned long flags;
  1200. spin_lock_irqsave(&bnad->bna_lock, flags);
  1201. cfg_flags = bnad->cfg_flags;
  1202. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1203. if (cfg_flags & BNAD_CF_MSIX) {
  1204. intr_info->intr_type = BNA_INTR_T_MSIX;
  1205. intr_info->idl = kcalloc(intr_info->num,
  1206. sizeof(struct bna_intr_descr),
  1207. GFP_KERNEL);
  1208. if (!intr_info->idl)
  1209. return -ENOMEM;
  1210. switch (src) {
  1211. case BNAD_INTR_TX:
  1212. vector_start = BNAD_MAILBOX_MSIX_VECTORS + txrx_id;
  1213. break;
  1214. case BNAD_INTR_RX:
  1215. vector_start = BNAD_MAILBOX_MSIX_VECTORS +
  1216. (bnad->num_tx * bnad->num_txq_per_tx) +
  1217. txrx_id;
  1218. break;
  1219. default:
  1220. BUG();
  1221. }
  1222. for (i = 0; i < intr_info->num; i++)
  1223. intr_info->idl[i].vector = vector_start + i;
  1224. } else {
  1225. intr_info->intr_type = BNA_INTR_T_INTX;
  1226. intr_info->num = 1;
  1227. intr_info->idl = kcalloc(intr_info->num,
  1228. sizeof(struct bna_intr_descr),
  1229. GFP_KERNEL);
  1230. if (!intr_info->idl)
  1231. return -ENOMEM;
  1232. switch (src) {
  1233. case BNAD_INTR_TX:
  1234. intr_info->idl[0].vector = BNAD_INTX_TX_IB_BITMASK;
  1235. break;
  1236. case BNAD_INTR_RX:
  1237. intr_info->idl[0].vector = BNAD_INTX_RX_IB_BITMASK;
  1238. break;
  1239. }
  1240. }
  1241. return 0;
  1242. }
  1243. /* NOTE: Should be called for MSIX only
  1244. * Unregisters Tx MSIX vector(s) from the kernel
  1245. */
  1246. static void
  1247. bnad_tx_msix_unregister(struct bnad *bnad, struct bnad_tx_info *tx_info,
  1248. int num_txqs)
  1249. {
  1250. int i;
  1251. int vector_num;
  1252. for (i = 0; i < num_txqs; i++) {
  1253. if (tx_info->tcb[i] == NULL)
  1254. continue;
  1255. vector_num = tx_info->tcb[i]->intr_vector;
  1256. free_irq(bnad->msix_table[vector_num].vector, tx_info->tcb[i]);
  1257. }
  1258. }
  1259. /* NOTE: Should be called for MSIX only
  1260. * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
  1261. */
  1262. static int
  1263. bnad_tx_msix_register(struct bnad *bnad, struct bnad_tx_info *tx_info,
  1264. u32 tx_id, int num_txqs)
  1265. {
  1266. int i;
  1267. int err;
  1268. int vector_num;
  1269. for (i = 0; i < num_txqs; i++) {
  1270. vector_num = tx_info->tcb[i]->intr_vector;
  1271. snprintf(tx_info->tcb[i]->name, BNA_Q_NAME_SIZE, "%s TXQ %d",
  1272. bnad->netdev->name,
  1273. tx_id + tx_info->tcb[i]->id);
  1274. err = request_irq(bnad->msix_table[vector_num].vector,
  1275. (irq_handler_t)bnad_msix_tx, 0,
  1276. tx_info->tcb[i]->name,
  1277. tx_info->tcb[i]);
  1278. if (err)
  1279. goto err_return;
  1280. }
  1281. return 0;
  1282. err_return:
  1283. if (i > 0)
  1284. bnad_tx_msix_unregister(bnad, tx_info, (i - 1));
  1285. return -1;
  1286. }
  1287. /* NOTE: Should be called for MSIX only
  1288. * Unregisters Rx MSIX vector(s) from the kernel
  1289. */
  1290. static void
  1291. bnad_rx_msix_unregister(struct bnad *bnad, struct bnad_rx_info *rx_info,
  1292. int num_rxps)
  1293. {
  1294. int i;
  1295. int vector_num;
  1296. for (i = 0; i < num_rxps; i++) {
  1297. if (rx_info->rx_ctrl[i].ccb == NULL)
  1298. continue;
  1299. vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
  1300. free_irq(bnad->msix_table[vector_num].vector,
  1301. rx_info->rx_ctrl[i].ccb);
  1302. }
  1303. }
  1304. /* NOTE: Should be called for MSIX only
  1305. * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
  1306. */
  1307. static int
  1308. bnad_rx_msix_register(struct bnad *bnad, struct bnad_rx_info *rx_info,
  1309. u32 rx_id, int num_rxps)
  1310. {
  1311. int i;
  1312. int err;
  1313. int vector_num;
  1314. for (i = 0; i < num_rxps; i++) {
  1315. vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
  1316. snprintf(rx_info->rx_ctrl[i].ccb->name, BNA_Q_NAME_SIZE,
  1317. "%s CQ %d", bnad->netdev->name,
  1318. rx_id + rx_info->rx_ctrl[i].ccb->id);
  1319. err = request_irq(bnad->msix_table[vector_num].vector,
  1320. (irq_handler_t)bnad_msix_rx, 0,
  1321. rx_info->rx_ctrl[i].ccb->name,
  1322. rx_info->rx_ctrl[i].ccb);
  1323. if (err)
  1324. goto err_return;
  1325. }
  1326. return 0;
  1327. err_return:
  1328. if (i > 0)
  1329. bnad_rx_msix_unregister(bnad, rx_info, (i - 1));
  1330. return -1;
  1331. }
  1332. /* Free Tx object Resources */
  1333. static void
  1334. bnad_tx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
  1335. {
  1336. int i;
  1337. for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
  1338. if (res_info[i].res_type == BNA_RES_T_MEM)
  1339. bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
  1340. else if (res_info[i].res_type == BNA_RES_T_INTR)
  1341. bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
  1342. }
  1343. }
  1344. /* Allocates memory and interrupt resources for Tx object */
  1345. static int
  1346. bnad_tx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
  1347. u32 tx_id)
  1348. {
  1349. int i, err = 0;
  1350. for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
  1351. if (res_info[i].res_type == BNA_RES_T_MEM)
  1352. err = bnad_mem_alloc(bnad,
  1353. &res_info[i].res_u.mem_info);
  1354. else if (res_info[i].res_type == BNA_RES_T_INTR)
  1355. err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_TX, tx_id,
  1356. &res_info[i].res_u.intr_info);
  1357. if (err)
  1358. goto err_return;
  1359. }
  1360. return 0;
  1361. err_return:
  1362. bnad_tx_res_free(bnad, res_info);
  1363. return err;
  1364. }
  1365. /* Free Rx object Resources */
  1366. static void
  1367. bnad_rx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
  1368. {
  1369. int i;
  1370. for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
  1371. if (res_info[i].res_type == BNA_RES_T_MEM)
  1372. bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
  1373. else if (res_info[i].res_type == BNA_RES_T_INTR)
  1374. bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
  1375. }
  1376. }
  1377. /* Allocates memory and interrupt resources for Rx object */
  1378. static int
  1379. bnad_rx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
  1380. uint rx_id)
  1381. {
  1382. int i, err = 0;
  1383. /* All memory needs to be allocated before setup_ccbs */
  1384. for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
  1385. if (res_info[i].res_type == BNA_RES_T_MEM)
  1386. err = bnad_mem_alloc(bnad,
  1387. &res_info[i].res_u.mem_info);
  1388. else if (res_info[i].res_type == BNA_RES_T_INTR)
  1389. err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_RX, rx_id,
  1390. &res_info[i].res_u.intr_info);
  1391. if (err)
  1392. goto err_return;
  1393. }
  1394. return 0;
  1395. err_return:
  1396. bnad_rx_res_free(bnad, res_info);
  1397. return err;
  1398. }
  1399. /* Timer callbacks */
  1400. /* a) IOC timer */
  1401. static void
  1402. bnad_ioc_timeout(struct timer_list *t)
  1403. {
  1404. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.ioc_timer);
  1405. unsigned long flags;
  1406. spin_lock_irqsave(&bnad->bna_lock, flags);
  1407. bfa_nw_ioc_timeout(&bnad->bna.ioceth.ioc);
  1408. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1409. }
  1410. static void
  1411. bnad_ioc_hb_check(struct timer_list *t)
  1412. {
  1413. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.hb_timer);
  1414. unsigned long flags;
  1415. spin_lock_irqsave(&bnad->bna_lock, flags);
  1416. bfa_nw_ioc_hb_check(&bnad->bna.ioceth.ioc);
  1417. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1418. }
  1419. static void
  1420. bnad_iocpf_timeout(struct timer_list *t)
  1421. {
  1422. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.iocpf_timer);
  1423. unsigned long flags;
  1424. spin_lock_irqsave(&bnad->bna_lock, flags);
  1425. bfa_nw_iocpf_timeout(&bnad->bna.ioceth.ioc);
  1426. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1427. }
  1428. static void
  1429. bnad_iocpf_sem_timeout(struct timer_list *t)
  1430. {
  1431. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.sem_timer);
  1432. unsigned long flags;
  1433. spin_lock_irqsave(&bnad->bna_lock, flags);
  1434. bfa_nw_iocpf_sem_timeout(&bnad->bna.ioceth.ioc);
  1435. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1436. }
  1437. /*
  1438. * All timer routines use bnad->bna_lock to protect against
  1439. * the following race, which may occur in case of no locking:
  1440. * Time CPU m CPU n
  1441. * 0 1 = test_bit
  1442. * 1 clear_bit
  1443. * 2 del_timer_sync
  1444. * 3 mod_timer
  1445. */
  1446. /* b) Dynamic Interrupt Moderation Timer */
  1447. static void
  1448. bnad_dim_timeout(struct timer_list *t)
  1449. {
  1450. struct bnad *bnad = from_timer(bnad, t, dim_timer);
  1451. struct bnad_rx_info *rx_info;
  1452. struct bnad_rx_ctrl *rx_ctrl;
  1453. int i, j;
  1454. unsigned long flags;
  1455. if (!netif_carrier_ok(bnad->netdev))
  1456. return;
  1457. spin_lock_irqsave(&bnad->bna_lock, flags);
  1458. for (i = 0; i < bnad->num_rx; i++) {
  1459. rx_info = &bnad->rx_info[i];
  1460. if (!rx_info->rx)
  1461. continue;
  1462. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  1463. rx_ctrl = &rx_info->rx_ctrl[j];
  1464. if (!rx_ctrl->ccb)
  1465. continue;
  1466. bna_rx_dim_update(rx_ctrl->ccb);
  1467. }
  1468. }
  1469. /* Check for BNAD_CF_DIM_ENABLED, does not eliminate a race */
  1470. if (test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags))
  1471. mod_timer(&bnad->dim_timer,
  1472. jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
  1473. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1474. }
  1475. /* c) Statistics Timer */
  1476. static void
  1477. bnad_stats_timeout(struct timer_list *t)
  1478. {
  1479. struct bnad *bnad = from_timer(bnad, t, stats_timer);
  1480. unsigned long flags;
  1481. if (!netif_running(bnad->netdev) ||
  1482. !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
  1483. return;
  1484. spin_lock_irqsave(&bnad->bna_lock, flags);
  1485. bna_hw_stats_get(&bnad->bna);
  1486. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1487. }
  1488. /*
  1489. * Set up timer for DIM
  1490. * Called with bnad->bna_lock held
  1491. */
  1492. void
  1493. bnad_dim_timer_start(struct bnad *bnad)
  1494. {
  1495. if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
  1496. !test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
  1497. timer_setup(&bnad->dim_timer, bnad_dim_timeout, 0);
  1498. set_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
  1499. mod_timer(&bnad->dim_timer,
  1500. jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
  1501. }
  1502. }
  1503. /*
  1504. * Set up timer for statistics
  1505. * Called with mutex_lock(&bnad->conf_mutex) held
  1506. */
  1507. static void
  1508. bnad_stats_timer_start(struct bnad *bnad)
  1509. {
  1510. unsigned long flags;
  1511. spin_lock_irqsave(&bnad->bna_lock, flags);
  1512. if (!test_and_set_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags)) {
  1513. timer_setup(&bnad->stats_timer, bnad_stats_timeout, 0);
  1514. mod_timer(&bnad->stats_timer,
  1515. jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
  1516. }
  1517. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1518. }
  1519. /*
  1520. * Stops the stats timer
  1521. * Called with mutex_lock(&bnad->conf_mutex) held
  1522. */
  1523. static void
  1524. bnad_stats_timer_stop(struct bnad *bnad)
  1525. {
  1526. int to_del = 0;
  1527. unsigned long flags;
  1528. spin_lock_irqsave(&bnad->bna_lock, flags);
  1529. if (test_and_clear_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
  1530. to_del = 1;
  1531. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1532. if (to_del)
  1533. del_timer_sync(&bnad->stats_timer);
  1534. }
  1535. /* Utilities */
  1536. static void
  1537. bnad_netdev_mc_list_get(struct net_device *netdev, u8 *mc_list)
  1538. {
  1539. int i = 1; /* Index 0 has broadcast address */
  1540. struct netdev_hw_addr *mc_addr;
  1541. netdev_for_each_mc_addr(mc_addr, netdev) {
  1542. ether_addr_copy(&mc_list[i * ETH_ALEN], &mc_addr->addr[0]);
  1543. i++;
  1544. }
  1545. }
  1546. static int
  1547. bnad_napi_poll_rx(struct napi_struct *napi, int budget)
  1548. {
  1549. struct bnad_rx_ctrl *rx_ctrl =
  1550. container_of(napi, struct bnad_rx_ctrl, napi);
  1551. struct bnad *bnad = rx_ctrl->bnad;
  1552. int rcvd = 0;
  1553. rx_ctrl->rx_poll_ctr++;
  1554. if (!netif_carrier_ok(bnad->netdev))
  1555. goto poll_exit;
  1556. rcvd = bnad_cq_process(bnad, rx_ctrl->ccb, budget);
  1557. if (rcvd >= budget)
  1558. return rcvd;
  1559. poll_exit:
  1560. napi_complete_done(napi, rcvd);
  1561. rx_ctrl->rx_complete++;
  1562. if (rx_ctrl->ccb)
  1563. bnad_enable_rx_irq_unsafe(rx_ctrl->ccb);
  1564. return rcvd;
  1565. }
  1566. static void
  1567. bnad_napi_add(struct bnad *bnad, u32 rx_id)
  1568. {
  1569. struct bnad_rx_ctrl *rx_ctrl;
  1570. int i;
  1571. /* Initialize & enable NAPI */
  1572. for (i = 0; i < bnad->num_rxp_per_rx; i++) {
  1573. rx_ctrl = &bnad->rx_info[rx_id].rx_ctrl[i];
  1574. netif_napi_add(bnad->netdev, &rx_ctrl->napi,
  1575. bnad_napi_poll_rx);
  1576. }
  1577. }
  1578. static void
  1579. bnad_napi_delete(struct bnad *bnad, u32 rx_id)
  1580. {
  1581. int i;
  1582. /* First disable and then clean up */
  1583. for (i = 0; i < bnad->num_rxp_per_rx; i++)
  1584. netif_napi_del(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
  1585. }
  1586. /* Should be held with conf_lock held */
  1587. void
  1588. bnad_destroy_tx(struct bnad *bnad, u32 tx_id)
  1589. {
  1590. struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
  1591. struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
  1592. unsigned long flags;
  1593. if (!tx_info->tx)
  1594. return;
  1595. init_completion(&bnad->bnad_completions.tx_comp);
  1596. spin_lock_irqsave(&bnad->bna_lock, flags);
  1597. bna_tx_disable(tx_info->tx, BNA_HARD_CLEANUP, bnad_cb_tx_disabled);
  1598. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1599. wait_for_completion(&bnad->bnad_completions.tx_comp);
  1600. if (tx_info->tcb[0]->intr_type == BNA_INTR_T_MSIX)
  1601. bnad_tx_msix_unregister(bnad, tx_info,
  1602. bnad->num_txq_per_tx);
  1603. spin_lock_irqsave(&bnad->bna_lock, flags);
  1604. bna_tx_destroy(tx_info->tx);
  1605. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1606. tx_info->tx = NULL;
  1607. tx_info->tx_id = 0;
  1608. bnad_tx_res_free(bnad, res_info);
  1609. }
  1610. /* Should be held with conf_lock held */
  1611. int
  1612. bnad_setup_tx(struct bnad *bnad, u32 tx_id)
  1613. {
  1614. int err;
  1615. struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
  1616. struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
  1617. struct bna_intr_info *intr_info =
  1618. &res_info[BNA_TX_RES_INTR_T_TXCMPL].res_u.intr_info;
  1619. struct bna_tx_config *tx_config = &bnad->tx_config[tx_id];
  1620. static const struct bna_tx_event_cbfn tx_cbfn = {
  1621. .tcb_setup_cbfn = bnad_cb_tcb_setup,
  1622. .tcb_destroy_cbfn = bnad_cb_tcb_destroy,
  1623. .tx_stall_cbfn = bnad_cb_tx_stall,
  1624. .tx_resume_cbfn = bnad_cb_tx_resume,
  1625. .tx_cleanup_cbfn = bnad_cb_tx_cleanup,
  1626. };
  1627. struct bna_tx *tx;
  1628. unsigned long flags;
  1629. tx_info->tx_id = tx_id;
  1630. /* Initialize the Tx object configuration */
  1631. tx_config->num_txq = bnad->num_txq_per_tx;
  1632. tx_config->txq_depth = bnad->txq_depth;
  1633. tx_config->tx_type = BNA_TX_T_REGULAR;
  1634. tx_config->coalescing_timeo = bnad->tx_coalescing_timeo;
  1635. /* Get BNA's resource requirement for one tx object */
  1636. spin_lock_irqsave(&bnad->bna_lock, flags);
  1637. bna_tx_res_req(bnad->num_txq_per_tx,
  1638. bnad->txq_depth, res_info);
  1639. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1640. /* Fill Unmap Q memory requirements */
  1641. BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_TX_RES_MEM_T_UNMAPQ],
  1642. bnad->num_txq_per_tx, (sizeof(struct bnad_tx_unmap) *
  1643. bnad->txq_depth));
  1644. /* Allocate resources */
  1645. err = bnad_tx_res_alloc(bnad, res_info, tx_id);
  1646. if (err)
  1647. return err;
  1648. /* Ask BNA to create one Tx object, supplying required resources */
  1649. spin_lock_irqsave(&bnad->bna_lock, flags);
  1650. tx = bna_tx_create(&bnad->bna, bnad, tx_config, &tx_cbfn, res_info,
  1651. tx_info);
  1652. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1653. if (!tx) {
  1654. err = -ENOMEM;
  1655. goto err_return;
  1656. }
  1657. tx_info->tx = tx;
  1658. INIT_DELAYED_WORK(&tx_info->tx_cleanup_work, bnad_tx_cleanup);
  1659. /* Register ISR for the Tx object */
  1660. if (intr_info->intr_type == BNA_INTR_T_MSIX) {
  1661. err = bnad_tx_msix_register(bnad, tx_info,
  1662. tx_id, bnad->num_txq_per_tx);
  1663. if (err)
  1664. goto cleanup_tx;
  1665. }
  1666. spin_lock_irqsave(&bnad->bna_lock, flags);
  1667. bna_tx_enable(tx);
  1668. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1669. return 0;
  1670. cleanup_tx:
  1671. spin_lock_irqsave(&bnad->bna_lock, flags);
  1672. bna_tx_destroy(tx_info->tx);
  1673. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1674. tx_info->tx = NULL;
  1675. tx_info->tx_id = 0;
  1676. err_return:
  1677. bnad_tx_res_free(bnad, res_info);
  1678. return err;
  1679. }
  1680. /* Setup the rx config for bna_rx_create */
  1681. /* bnad decides the configuration */
  1682. static void
  1683. bnad_init_rx_config(struct bnad *bnad, struct bna_rx_config *rx_config)
  1684. {
  1685. memset(rx_config, 0, sizeof(*rx_config));
  1686. rx_config->rx_type = BNA_RX_T_REGULAR;
  1687. rx_config->num_paths = bnad->num_rxp_per_rx;
  1688. rx_config->coalescing_timeo = bnad->rx_coalescing_timeo;
  1689. if (bnad->num_rxp_per_rx > 1) {
  1690. rx_config->rss_status = BNA_STATUS_T_ENABLED;
  1691. rx_config->rss_config.hash_type =
  1692. (BFI_ENET_RSS_IPV6 |
  1693. BFI_ENET_RSS_IPV6_TCP |
  1694. BFI_ENET_RSS_IPV4 |
  1695. BFI_ENET_RSS_IPV4_TCP);
  1696. rx_config->rss_config.hash_mask =
  1697. bnad->num_rxp_per_rx - 1;
  1698. netdev_rss_key_fill(rx_config->rss_config.toeplitz_hash_key,
  1699. sizeof(rx_config->rss_config.toeplitz_hash_key));
  1700. } else {
  1701. rx_config->rss_status = BNA_STATUS_T_DISABLED;
  1702. memset(&rx_config->rss_config, 0,
  1703. sizeof(rx_config->rss_config));
  1704. }
  1705. rx_config->frame_size = BNAD_FRAME_SIZE(bnad->netdev->mtu);
  1706. rx_config->q0_multi_buf = BNA_STATUS_T_DISABLED;
  1707. /* BNA_RXP_SINGLE - one data-buffer queue
  1708. * BNA_RXP_SLR - one small-buffer and one large-buffer queues
  1709. * BNA_RXP_HDS - one header-buffer and one data-buffer queues
  1710. */
  1711. /* TODO: configurable param for queue type */
  1712. rx_config->rxp_type = BNA_RXP_SLR;
  1713. if (BNAD_PCI_DEV_IS_CAT2(bnad) &&
  1714. rx_config->frame_size > 4096) {
  1715. /* though size_routing_enable is set in SLR,
  1716. * small packets may get routed to same rxq.
  1717. * set buf_size to 2048 instead of PAGE_SIZE.
  1718. */
  1719. rx_config->q0_buf_size = 2048;
  1720. /* this should be in multiples of 2 */
  1721. rx_config->q0_num_vecs = 4;
  1722. rx_config->q0_depth = bnad->rxq_depth * rx_config->q0_num_vecs;
  1723. rx_config->q0_multi_buf = BNA_STATUS_T_ENABLED;
  1724. } else {
  1725. rx_config->q0_buf_size = rx_config->frame_size;
  1726. rx_config->q0_num_vecs = 1;
  1727. rx_config->q0_depth = bnad->rxq_depth;
  1728. }
  1729. /* initialize for q1 for BNA_RXP_SLR/BNA_RXP_HDS */
  1730. if (rx_config->rxp_type == BNA_RXP_SLR) {
  1731. rx_config->q1_depth = bnad->rxq_depth;
  1732. rx_config->q1_buf_size = BFI_SMALL_RXBUF_SIZE;
  1733. }
  1734. rx_config->vlan_strip_status =
  1735. (bnad->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) ?
  1736. BNA_STATUS_T_ENABLED : BNA_STATUS_T_DISABLED;
  1737. }
  1738. static void
  1739. bnad_rx_ctrl_init(struct bnad *bnad, u32 rx_id)
  1740. {
  1741. struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
  1742. int i;
  1743. for (i = 0; i < bnad->num_rxp_per_rx; i++)
  1744. rx_info->rx_ctrl[i].bnad = bnad;
  1745. }
  1746. /* Called with mutex_lock(&bnad->conf_mutex) held */
  1747. static u32
  1748. bnad_reinit_rx(struct bnad *bnad)
  1749. {
  1750. struct net_device *netdev = bnad->netdev;
  1751. u32 err = 0, current_err = 0;
  1752. u32 rx_id = 0, count = 0;
  1753. unsigned long flags;
  1754. /* destroy and create new rx objects */
  1755. for (rx_id = 0; rx_id < bnad->num_rx; rx_id++) {
  1756. if (!bnad->rx_info[rx_id].rx)
  1757. continue;
  1758. bnad_destroy_rx(bnad, rx_id);
  1759. }
  1760. spin_lock_irqsave(&bnad->bna_lock, flags);
  1761. bna_enet_mtu_set(&bnad->bna.enet,
  1762. BNAD_FRAME_SIZE(bnad->netdev->mtu), NULL);
  1763. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1764. for (rx_id = 0; rx_id < bnad->num_rx; rx_id++) {
  1765. count++;
  1766. current_err = bnad_setup_rx(bnad, rx_id);
  1767. if (current_err && !err) {
  1768. err = current_err;
  1769. netdev_err(netdev, "RXQ:%u setup failed\n", rx_id);
  1770. }
  1771. }
  1772. /* restore rx configuration */
  1773. if (bnad->rx_info[0].rx && !err) {
  1774. bnad_restore_vlans(bnad, 0);
  1775. bnad_enable_default_bcast(bnad);
  1776. spin_lock_irqsave(&bnad->bna_lock, flags);
  1777. bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
  1778. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1779. bnad_set_rx_mode(netdev);
  1780. }
  1781. return count;
  1782. }
  1783. /* Called with bnad_conf_lock() held */
  1784. void
  1785. bnad_destroy_rx(struct bnad *bnad, u32 rx_id)
  1786. {
  1787. struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
  1788. struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
  1789. struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
  1790. unsigned long flags;
  1791. int to_del = 0;
  1792. if (!rx_info->rx)
  1793. return;
  1794. if (0 == rx_id) {
  1795. spin_lock_irqsave(&bnad->bna_lock, flags);
  1796. if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
  1797. test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
  1798. clear_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
  1799. to_del = 1;
  1800. }
  1801. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1802. if (to_del)
  1803. del_timer_sync(&bnad->dim_timer);
  1804. }
  1805. init_completion(&bnad->bnad_completions.rx_comp);
  1806. spin_lock_irqsave(&bnad->bna_lock, flags);
  1807. bna_rx_disable(rx_info->rx, BNA_HARD_CLEANUP, bnad_cb_rx_disabled);
  1808. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1809. wait_for_completion(&bnad->bnad_completions.rx_comp);
  1810. if (rx_info->rx_ctrl[0].ccb->intr_type == BNA_INTR_T_MSIX)
  1811. bnad_rx_msix_unregister(bnad, rx_info, rx_config->num_paths);
  1812. bnad_napi_delete(bnad, rx_id);
  1813. spin_lock_irqsave(&bnad->bna_lock, flags);
  1814. bna_rx_destroy(rx_info->rx);
  1815. rx_info->rx = NULL;
  1816. rx_info->rx_id = 0;
  1817. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1818. bnad_rx_res_free(bnad, res_info);
  1819. }
  1820. /* Called with mutex_lock(&bnad->conf_mutex) held */
  1821. int
  1822. bnad_setup_rx(struct bnad *bnad, u32 rx_id)
  1823. {
  1824. int err;
  1825. struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
  1826. struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
  1827. struct bna_intr_info *intr_info =
  1828. &res_info[BNA_RX_RES_T_INTR].res_u.intr_info;
  1829. struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
  1830. static const struct bna_rx_event_cbfn rx_cbfn = {
  1831. .rcb_setup_cbfn = NULL,
  1832. .rcb_destroy_cbfn = NULL,
  1833. .ccb_setup_cbfn = bnad_cb_ccb_setup,
  1834. .ccb_destroy_cbfn = bnad_cb_ccb_destroy,
  1835. .rx_stall_cbfn = bnad_cb_rx_stall,
  1836. .rx_cleanup_cbfn = bnad_cb_rx_cleanup,
  1837. .rx_post_cbfn = bnad_cb_rx_post,
  1838. };
  1839. struct bna_rx *rx;
  1840. unsigned long flags;
  1841. rx_info->rx_id = rx_id;
  1842. /* Initialize the Rx object configuration */
  1843. bnad_init_rx_config(bnad, rx_config);
  1844. /* Get BNA's resource requirement for one Rx object */
  1845. spin_lock_irqsave(&bnad->bna_lock, flags);
  1846. bna_rx_res_req(rx_config, res_info);
  1847. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1848. /* Fill Unmap Q memory requirements */
  1849. BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_RX_RES_MEM_T_UNMAPDQ],
  1850. rx_config->num_paths,
  1851. (rx_config->q0_depth *
  1852. sizeof(struct bnad_rx_unmap)) +
  1853. sizeof(struct bnad_rx_unmap_q));
  1854. if (rx_config->rxp_type != BNA_RXP_SINGLE) {
  1855. BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_RX_RES_MEM_T_UNMAPHQ],
  1856. rx_config->num_paths,
  1857. (rx_config->q1_depth *
  1858. sizeof(struct bnad_rx_unmap) +
  1859. sizeof(struct bnad_rx_unmap_q)));
  1860. }
  1861. /* Allocate resource */
  1862. err = bnad_rx_res_alloc(bnad, res_info, rx_id);
  1863. if (err)
  1864. return err;
  1865. bnad_rx_ctrl_init(bnad, rx_id);
  1866. /* Ask BNA to create one Rx object, supplying required resources */
  1867. spin_lock_irqsave(&bnad->bna_lock, flags);
  1868. rx = bna_rx_create(&bnad->bna, bnad, rx_config, &rx_cbfn, res_info,
  1869. rx_info);
  1870. if (!rx) {
  1871. err = -ENOMEM;
  1872. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1873. goto err_return;
  1874. }
  1875. rx_info->rx = rx;
  1876. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1877. INIT_WORK(&rx_info->rx_cleanup_work, bnad_rx_cleanup);
  1878. /*
  1879. * Init NAPI, so that state is set to NAPI_STATE_SCHED,
  1880. * so that IRQ handler cannot schedule NAPI at this point.
  1881. */
  1882. bnad_napi_add(bnad, rx_id);
  1883. /* Register ISR for the Rx object */
  1884. if (intr_info->intr_type == BNA_INTR_T_MSIX) {
  1885. err = bnad_rx_msix_register(bnad, rx_info, rx_id,
  1886. rx_config->num_paths);
  1887. if (err)
  1888. goto err_return;
  1889. }
  1890. spin_lock_irqsave(&bnad->bna_lock, flags);
  1891. if (0 == rx_id) {
  1892. /* Set up Dynamic Interrupt Moderation Vector */
  1893. if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED)
  1894. bna_rx_dim_reconfig(&bnad->bna, bna_napi_dim_vector);
  1895. /* Enable VLAN filtering only on the default Rx */
  1896. bna_rx_vlanfilter_enable(rx);
  1897. /* Start the DIM timer */
  1898. bnad_dim_timer_start(bnad);
  1899. }
  1900. bna_rx_enable(rx);
  1901. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1902. return 0;
  1903. err_return:
  1904. bnad_destroy_rx(bnad, rx_id);
  1905. return err;
  1906. }
  1907. /* Called with conf_lock & bnad->bna_lock held */
  1908. void
  1909. bnad_tx_coalescing_timeo_set(struct bnad *bnad)
  1910. {
  1911. struct bnad_tx_info *tx_info;
  1912. tx_info = &bnad->tx_info[0];
  1913. if (!tx_info->tx)
  1914. return;
  1915. bna_tx_coalescing_timeo_set(tx_info->tx, bnad->tx_coalescing_timeo);
  1916. }
  1917. /* Called with conf_lock & bnad->bna_lock held */
  1918. void
  1919. bnad_rx_coalescing_timeo_set(struct bnad *bnad)
  1920. {
  1921. struct bnad_rx_info *rx_info;
  1922. int i;
  1923. for (i = 0; i < bnad->num_rx; i++) {
  1924. rx_info = &bnad->rx_info[i];
  1925. if (!rx_info->rx)
  1926. continue;
  1927. bna_rx_coalescing_timeo_set(rx_info->rx,
  1928. bnad->rx_coalescing_timeo);
  1929. }
  1930. }
  1931. /*
  1932. * Called with bnad->bna_lock held
  1933. */
  1934. int
  1935. bnad_mac_addr_set_locked(struct bnad *bnad, const u8 *mac_addr)
  1936. {
  1937. int ret;
  1938. if (!is_valid_ether_addr(mac_addr))
  1939. return -EADDRNOTAVAIL;
  1940. /* If datapath is down, pretend everything went through */
  1941. if (!bnad->rx_info[0].rx)
  1942. return 0;
  1943. ret = bna_rx_ucast_set(bnad->rx_info[0].rx, mac_addr);
  1944. if (ret != BNA_CB_SUCCESS)
  1945. return -EADDRNOTAVAIL;
  1946. return 0;
  1947. }
  1948. /* Should be called with conf_lock held */
  1949. int
  1950. bnad_enable_default_bcast(struct bnad *bnad)
  1951. {
  1952. struct bnad_rx_info *rx_info = &bnad->rx_info[0];
  1953. int ret;
  1954. unsigned long flags;
  1955. init_completion(&bnad->bnad_completions.mcast_comp);
  1956. spin_lock_irqsave(&bnad->bna_lock, flags);
  1957. ret = bna_rx_mcast_add(rx_info->rx, bnad_bcast_addr,
  1958. bnad_cb_rx_mcast_add);
  1959. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1960. if (ret == BNA_CB_SUCCESS)
  1961. wait_for_completion(&bnad->bnad_completions.mcast_comp);
  1962. else
  1963. return -ENODEV;
  1964. if (bnad->bnad_completions.mcast_comp_status != BNA_CB_SUCCESS)
  1965. return -ENODEV;
  1966. return 0;
  1967. }
  1968. /* Called with mutex_lock(&bnad->conf_mutex) held */
  1969. void
  1970. bnad_restore_vlans(struct bnad *bnad, u32 rx_id)
  1971. {
  1972. u16 vid;
  1973. unsigned long flags;
  1974. for_each_set_bit(vid, bnad->active_vlans, VLAN_N_VID) {
  1975. spin_lock_irqsave(&bnad->bna_lock, flags);
  1976. bna_rx_vlan_add(bnad->rx_info[rx_id].rx, vid);
  1977. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1978. }
  1979. }
  1980. /* Statistics utilities */
  1981. void
  1982. bnad_netdev_qstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
  1983. {
  1984. int i, j;
  1985. for (i = 0; i < bnad->num_rx; i++) {
  1986. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  1987. if (bnad->rx_info[i].rx_ctrl[j].ccb) {
  1988. stats->rx_packets += bnad->rx_info[i].
  1989. rx_ctrl[j].ccb->rcb[0]->rxq->rx_packets;
  1990. stats->rx_bytes += bnad->rx_info[i].
  1991. rx_ctrl[j].ccb->rcb[0]->rxq->rx_bytes;
  1992. if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
  1993. bnad->rx_info[i].rx_ctrl[j].ccb->
  1994. rcb[1]->rxq) {
  1995. stats->rx_packets +=
  1996. bnad->rx_info[i].rx_ctrl[j].
  1997. ccb->rcb[1]->rxq->rx_packets;
  1998. stats->rx_bytes +=
  1999. bnad->rx_info[i].rx_ctrl[j].
  2000. ccb->rcb[1]->rxq->rx_bytes;
  2001. }
  2002. }
  2003. }
  2004. }
  2005. for (i = 0; i < bnad->num_tx; i++) {
  2006. for (j = 0; j < bnad->num_txq_per_tx; j++) {
  2007. if (bnad->tx_info[i].tcb[j]) {
  2008. stats->tx_packets +=
  2009. bnad->tx_info[i].tcb[j]->txq->tx_packets;
  2010. stats->tx_bytes +=
  2011. bnad->tx_info[i].tcb[j]->txq->tx_bytes;
  2012. }
  2013. }
  2014. }
  2015. }
  2016. /*
  2017. * Must be called with the bna_lock held.
  2018. */
  2019. void
  2020. bnad_netdev_hwstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
  2021. {
  2022. struct bfi_enet_stats_mac *mac_stats;
  2023. u32 bmap;
  2024. int i;
  2025. mac_stats = &bnad->stats.bna_stats->hw_stats.mac_stats;
  2026. stats->rx_errors =
  2027. mac_stats->rx_fcs_error + mac_stats->rx_alignment_error +
  2028. mac_stats->rx_frame_length_error + mac_stats->rx_code_error +
  2029. mac_stats->rx_undersize;
  2030. stats->tx_errors = mac_stats->tx_fcs_error +
  2031. mac_stats->tx_undersize;
  2032. stats->rx_dropped = mac_stats->rx_drop;
  2033. stats->tx_dropped = mac_stats->tx_drop;
  2034. stats->multicast = mac_stats->rx_multicast;
  2035. stats->collisions = mac_stats->tx_total_collision;
  2036. stats->rx_length_errors = mac_stats->rx_frame_length_error;
  2037. /* receive ring buffer overflow ?? */
  2038. stats->rx_crc_errors = mac_stats->rx_fcs_error;
  2039. stats->rx_frame_errors = mac_stats->rx_alignment_error;
  2040. /* recv'r fifo overrun */
  2041. bmap = bna_rx_rid_mask(&bnad->bna);
  2042. for (i = 0; bmap; i++) {
  2043. if (bmap & 1) {
  2044. stats->rx_fifo_errors +=
  2045. bnad->stats.bna_stats->
  2046. hw_stats.rxf_stats[i].frame_drops;
  2047. break;
  2048. }
  2049. bmap >>= 1;
  2050. }
  2051. }
  2052. static void
  2053. bnad_mbox_irq_sync(struct bnad *bnad)
  2054. {
  2055. u32 irq;
  2056. unsigned long flags;
  2057. spin_lock_irqsave(&bnad->bna_lock, flags);
  2058. if (bnad->cfg_flags & BNAD_CF_MSIX)
  2059. irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
  2060. else
  2061. irq = bnad->pcidev->irq;
  2062. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2063. synchronize_irq(irq);
  2064. }
  2065. /* Utility used by bnad_start_xmit, for doing TSO */
  2066. static int
  2067. bnad_tso_prepare(struct bnad *bnad, struct sk_buff *skb)
  2068. {
  2069. int err;
  2070. err = skb_cow_head(skb, 0);
  2071. if (err < 0) {
  2072. BNAD_UPDATE_CTR(bnad, tso_err);
  2073. return err;
  2074. }
  2075. /*
  2076. * For TSO, the TCP checksum field is seeded with pseudo-header sum
  2077. * excluding the length field.
  2078. */
  2079. if (vlan_get_protocol(skb) == htons(ETH_P_IP)) {
  2080. struct iphdr *iph = ip_hdr(skb);
  2081. /* Do we really need these? */
  2082. iph->tot_len = 0;
  2083. iph->check = 0;
  2084. tcp_hdr(skb)->check =
  2085. ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
  2086. IPPROTO_TCP, 0);
  2087. BNAD_UPDATE_CTR(bnad, tso4);
  2088. } else {
  2089. tcp_v6_gso_csum_prep(skb);
  2090. BNAD_UPDATE_CTR(bnad, tso6);
  2091. }
  2092. return 0;
  2093. }
  2094. /*
  2095. * Initialize Q numbers depending on Rx Paths
  2096. * Called with bnad->bna_lock held, because of cfg_flags
  2097. * access.
  2098. */
  2099. static void
  2100. bnad_q_num_init(struct bnad *bnad)
  2101. {
  2102. int rxps;
  2103. rxps = min((uint)num_online_cpus(),
  2104. (uint)(BNAD_MAX_RX * BNAD_MAX_RXP_PER_RX));
  2105. if (!(bnad->cfg_flags & BNAD_CF_MSIX))
  2106. rxps = 1; /* INTx */
  2107. bnad->num_rx = 1;
  2108. bnad->num_tx = 1;
  2109. bnad->num_rxp_per_rx = rxps;
  2110. bnad->num_txq_per_tx = BNAD_TXQ_NUM;
  2111. }
  2112. /*
  2113. * Adjusts the Q numbers, given a number of msix vectors
  2114. * Give preference to RSS as opposed to Tx priority Queues,
  2115. * in such a case, just use 1 Tx Q
  2116. * Called with bnad->bna_lock held b'cos of cfg_flags access
  2117. */
  2118. static void
  2119. bnad_q_num_adjust(struct bnad *bnad, int msix_vectors, int temp)
  2120. {
  2121. bnad->num_txq_per_tx = 1;
  2122. if ((msix_vectors >= (bnad->num_tx * bnad->num_txq_per_tx) +
  2123. bnad_rxqs_per_cq + BNAD_MAILBOX_MSIX_VECTORS) &&
  2124. (bnad->cfg_flags & BNAD_CF_MSIX)) {
  2125. bnad->num_rxp_per_rx = msix_vectors -
  2126. (bnad->num_tx * bnad->num_txq_per_tx) -
  2127. BNAD_MAILBOX_MSIX_VECTORS;
  2128. } else
  2129. bnad->num_rxp_per_rx = 1;
  2130. }
  2131. /* Enable / disable ioceth */
  2132. static int
  2133. bnad_ioceth_disable(struct bnad *bnad)
  2134. {
  2135. unsigned long flags;
  2136. int err = 0;
  2137. spin_lock_irqsave(&bnad->bna_lock, flags);
  2138. init_completion(&bnad->bnad_completions.ioc_comp);
  2139. bna_ioceth_disable(&bnad->bna.ioceth, BNA_HARD_CLEANUP);
  2140. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2141. wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
  2142. msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
  2143. err = bnad->bnad_completions.ioc_comp_status;
  2144. return err;
  2145. }
  2146. static int
  2147. bnad_ioceth_enable(struct bnad *bnad)
  2148. {
  2149. int err = 0;
  2150. unsigned long flags;
  2151. spin_lock_irqsave(&bnad->bna_lock, flags);
  2152. init_completion(&bnad->bnad_completions.ioc_comp);
  2153. bnad->bnad_completions.ioc_comp_status = BNA_CB_WAITING;
  2154. bna_ioceth_enable(&bnad->bna.ioceth);
  2155. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2156. wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
  2157. msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
  2158. err = bnad->bnad_completions.ioc_comp_status;
  2159. return err;
  2160. }
  2161. /* Free BNA resources */
  2162. static void
  2163. bnad_res_free(struct bnad *bnad, struct bna_res_info *res_info,
  2164. u32 res_val_max)
  2165. {
  2166. int i;
  2167. for (i = 0; i < res_val_max; i++)
  2168. bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
  2169. }
  2170. /* Allocates memory and interrupt resources for BNA */
  2171. static int
  2172. bnad_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
  2173. u32 res_val_max)
  2174. {
  2175. int i, err;
  2176. for (i = 0; i < res_val_max; i++) {
  2177. err = bnad_mem_alloc(bnad, &res_info[i].res_u.mem_info);
  2178. if (err)
  2179. goto err_return;
  2180. }
  2181. return 0;
  2182. err_return:
  2183. bnad_res_free(bnad, res_info, res_val_max);
  2184. return err;
  2185. }
  2186. /* Interrupt enable / disable */
  2187. static void
  2188. bnad_enable_msix(struct bnad *bnad)
  2189. {
  2190. int i, ret;
  2191. unsigned long flags;
  2192. spin_lock_irqsave(&bnad->bna_lock, flags);
  2193. if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
  2194. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2195. return;
  2196. }
  2197. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2198. if (bnad->msix_table)
  2199. return;
  2200. bnad->msix_table =
  2201. kcalloc(bnad->msix_num, sizeof(struct msix_entry), GFP_KERNEL);
  2202. if (!bnad->msix_table)
  2203. goto intx_mode;
  2204. for (i = 0; i < bnad->msix_num; i++)
  2205. bnad->msix_table[i].entry = i;
  2206. ret = pci_enable_msix_range(bnad->pcidev, bnad->msix_table,
  2207. 1, bnad->msix_num);
  2208. if (ret < 0) {
  2209. goto intx_mode;
  2210. } else if (ret < bnad->msix_num) {
  2211. dev_warn(&bnad->pcidev->dev,
  2212. "%d MSI-X vectors allocated < %d requested\n",
  2213. ret, bnad->msix_num);
  2214. spin_lock_irqsave(&bnad->bna_lock, flags);
  2215. /* ret = #of vectors that we got */
  2216. bnad_q_num_adjust(bnad, (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2,
  2217. (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2);
  2218. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2219. bnad->msix_num = BNAD_NUM_TXQ + BNAD_NUM_RXP +
  2220. BNAD_MAILBOX_MSIX_VECTORS;
  2221. if (bnad->msix_num > ret) {
  2222. pci_disable_msix(bnad->pcidev);
  2223. goto intx_mode;
  2224. }
  2225. }
  2226. pci_intx(bnad->pcidev, 0);
  2227. return;
  2228. intx_mode:
  2229. dev_warn(&bnad->pcidev->dev,
  2230. "MSI-X enable failed - operating in INTx mode\n");
  2231. kfree(bnad->msix_table);
  2232. bnad->msix_table = NULL;
  2233. bnad->msix_num = 0;
  2234. spin_lock_irqsave(&bnad->bna_lock, flags);
  2235. bnad->cfg_flags &= ~BNAD_CF_MSIX;
  2236. bnad_q_num_init(bnad);
  2237. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2238. }
  2239. static void
  2240. bnad_disable_msix(struct bnad *bnad)
  2241. {
  2242. u32 cfg_flags;
  2243. unsigned long flags;
  2244. spin_lock_irqsave(&bnad->bna_lock, flags);
  2245. cfg_flags = bnad->cfg_flags;
  2246. if (bnad->cfg_flags & BNAD_CF_MSIX)
  2247. bnad->cfg_flags &= ~BNAD_CF_MSIX;
  2248. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2249. if (cfg_flags & BNAD_CF_MSIX) {
  2250. pci_disable_msix(bnad->pcidev);
  2251. kfree(bnad->msix_table);
  2252. bnad->msix_table = NULL;
  2253. }
  2254. }
  2255. /* Netdev entry points */
  2256. static int
  2257. bnad_open(struct net_device *netdev)
  2258. {
  2259. int err;
  2260. struct bnad *bnad = netdev_priv(netdev);
  2261. struct bna_pause_config pause_config;
  2262. unsigned long flags;
  2263. mutex_lock(&bnad->conf_mutex);
  2264. /* Tx */
  2265. err = bnad_setup_tx(bnad, 0);
  2266. if (err)
  2267. goto err_return;
  2268. /* Rx */
  2269. err = bnad_setup_rx(bnad, 0);
  2270. if (err)
  2271. goto cleanup_tx;
  2272. /* Port */
  2273. pause_config.tx_pause = 0;
  2274. pause_config.rx_pause = 0;
  2275. spin_lock_irqsave(&bnad->bna_lock, flags);
  2276. bna_enet_mtu_set(&bnad->bna.enet,
  2277. BNAD_FRAME_SIZE(bnad->netdev->mtu), NULL);
  2278. bna_enet_pause_config(&bnad->bna.enet, &pause_config);
  2279. bna_enet_enable(&bnad->bna.enet);
  2280. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2281. /* Enable broadcast */
  2282. bnad_enable_default_bcast(bnad);
  2283. /* Restore VLANs, if any */
  2284. bnad_restore_vlans(bnad, 0);
  2285. /* Set the UCAST address */
  2286. spin_lock_irqsave(&bnad->bna_lock, flags);
  2287. bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
  2288. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2289. /* Start the stats timer */
  2290. bnad_stats_timer_start(bnad);
  2291. mutex_unlock(&bnad->conf_mutex);
  2292. return 0;
  2293. cleanup_tx:
  2294. bnad_destroy_tx(bnad, 0);
  2295. err_return:
  2296. mutex_unlock(&bnad->conf_mutex);
  2297. return err;
  2298. }
  2299. static int
  2300. bnad_stop(struct net_device *netdev)
  2301. {
  2302. struct bnad *bnad = netdev_priv(netdev);
  2303. unsigned long flags;
  2304. mutex_lock(&bnad->conf_mutex);
  2305. /* Stop the stats timer */
  2306. bnad_stats_timer_stop(bnad);
  2307. init_completion(&bnad->bnad_completions.enet_comp);
  2308. spin_lock_irqsave(&bnad->bna_lock, flags);
  2309. bna_enet_disable(&bnad->bna.enet, BNA_HARD_CLEANUP,
  2310. bnad_cb_enet_disabled);
  2311. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2312. wait_for_completion(&bnad->bnad_completions.enet_comp);
  2313. bnad_destroy_tx(bnad, 0);
  2314. bnad_destroy_rx(bnad, 0);
  2315. /* Synchronize mailbox IRQ */
  2316. bnad_mbox_irq_sync(bnad);
  2317. mutex_unlock(&bnad->conf_mutex);
  2318. return 0;
  2319. }
  2320. /* TX */
  2321. /* Returns 0 for success */
  2322. static int
  2323. bnad_txq_wi_prepare(struct bnad *bnad, struct bna_tcb *tcb,
  2324. struct sk_buff *skb, struct bna_txq_entry *txqent)
  2325. {
  2326. u16 flags = 0;
  2327. u32 gso_size;
  2328. u16 vlan_tag = 0;
  2329. if (skb_vlan_tag_present(skb)) {
  2330. vlan_tag = (u16)skb_vlan_tag_get(skb);
  2331. flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
  2332. }
  2333. if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags)) {
  2334. vlan_tag = ((tcb->priority & 0x7) << VLAN_PRIO_SHIFT)
  2335. | (vlan_tag & 0x1fff);
  2336. flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
  2337. }
  2338. txqent->hdr.wi.vlan_tag = htons(vlan_tag);
  2339. if (skb_is_gso(skb)) {
  2340. gso_size = skb_shinfo(skb)->gso_size;
  2341. if (unlikely(gso_size > bnad->netdev->mtu)) {
  2342. BNAD_UPDATE_CTR(bnad, tx_skb_mss_too_long);
  2343. return -EINVAL;
  2344. }
  2345. if (unlikely((gso_size + skb_tcp_all_headers(skb)) >= skb->len)) {
  2346. txqent->hdr.wi.opcode = htons(BNA_TXQ_WI_SEND);
  2347. txqent->hdr.wi.lso_mss = 0;
  2348. BNAD_UPDATE_CTR(bnad, tx_skb_tso_too_short);
  2349. } else {
  2350. txqent->hdr.wi.opcode = htons(BNA_TXQ_WI_SEND_LSO);
  2351. txqent->hdr.wi.lso_mss = htons(gso_size);
  2352. }
  2353. if (bnad_tso_prepare(bnad, skb)) {
  2354. BNAD_UPDATE_CTR(bnad, tx_skb_tso_prepare);
  2355. return -EINVAL;
  2356. }
  2357. flags |= (BNA_TXQ_WI_CF_IP_CKSUM | BNA_TXQ_WI_CF_TCP_CKSUM);
  2358. txqent->hdr.wi.l4_hdr_size_n_offset =
  2359. htons(BNA_TXQ_WI_L4_HDR_N_OFFSET(
  2360. tcp_hdrlen(skb) >> 2, skb_transport_offset(skb)));
  2361. } else {
  2362. txqent->hdr.wi.opcode = htons(BNA_TXQ_WI_SEND);
  2363. txqent->hdr.wi.lso_mss = 0;
  2364. if (unlikely(skb->len > (bnad->netdev->mtu + VLAN_ETH_HLEN))) {
  2365. BNAD_UPDATE_CTR(bnad, tx_skb_non_tso_too_long);
  2366. return -EINVAL;
  2367. }
  2368. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2369. __be16 net_proto = vlan_get_protocol(skb);
  2370. u8 proto = 0;
  2371. if (net_proto == htons(ETH_P_IP))
  2372. proto = ip_hdr(skb)->protocol;
  2373. #ifdef NETIF_F_IPV6_CSUM
  2374. else if (net_proto == htons(ETH_P_IPV6)) {
  2375. /* nexthdr may not be TCP immediately. */
  2376. proto = ipv6_hdr(skb)->nexthdr;
  2377. }
  2378. #endif
  2379. if (proto == IPPROTO_TCP) {
  2380. flags |= BNA_TXQ_WI_CF_TCP_CKSUM;
  2381. txqent->hdr.wi.l4_hdr_size_n_offset =
  2382. htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
  2383. (0, skb_transport_offset(skb)));
  2384. BNAD_UPDATE_CTR(bnad, tcpcsum_offload);
  2385. if (unlikely(skb_headlen(skb) <
  2386. skb_tcp_all_headers(skb))) {
  2387. BNAD_UPDATE_CTR(bnad, tx_skb_tcp_hdr);
  2388. return -EINVAL;
  2389. }
  2390. } else if (proto == IPPROTO_UDP) {
  2391. flags |= BNA_TXQ_WI_CF_UDP_CKSUM;
  2392. txqent->hdr.wi.l4_hdr_size_n_offset =
  2393. htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
  2394. (0, skb_transport_offset(skb)));
  2395. BNAD_UPDATE_CTR(bnad, udpcsum_offload);
  2396. if (unlikely(skb_headlen(skb) <
  2397. skb_transport_offset(skb) +
  2398. sizeof(struct udphdr))) {
  2399. BNAD_UPDATE_CTR(bnad, tx_skb_udp_hdr);
  2400. return -EINVAL;
  2401. }
  2402. } else {
  2403. BNAD_UPDATE_CTR(bnad, tx_skb_csum_err);
  2404. return -EINVAL;
  2405. }
  2406. } else
  2407. txqent->hdr.wi.l4_hdr_size_n_offset = 0;
  2408. }
  2409. txqent->hdr.wi.flags = htons(flags);
  2410. txqent->hdr.wi.frame_length = htonl(skb->len);
  2411. return 0;
  2412. }
  2413. /*
  2414. * bnad_start_xmit : Netdev entry point for Transmit
  2415. * Called under lock held by net_device
  2416. */
  2417. static netdev_tx_t
  2418. bnad_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  2419. {
  2420. struct bnad *bnad = netdev_priv(netdev);
  2421. u32 txq_id = 0;
  2422. struct bna_tcb *tcb = NULL;
  2423. struct bnad_tx_unmap *unmap_q, *unmap, *head_unmap;
  2424. u32 prod, q_depth, vect_id;
  2425. u32 wis, vectors, len;
  2426. int i;
  2427. dma_addr_t dma_addr;
  2428. struct bna_txq_entry *txqent;
  2429. len = skb_headlen(skb);
  2430. /* Sanity checks for the skb */
  2431. if (unlikely(skb->len <= ETH_HLEN)) {
  2432. dev_kfree_skb_any(skb);
  2433. BNAD_UPDATE_CTR(bnad, tx_skb_too_short);
  2434. return NETDEV_TX_OK;
  2435. }
  2436. if (unlikely(len > BFI_TX_MAX_DATA_PER_VECTOR)) {
  2437. dev_kfree_skb_any(skb);
  2438. BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
  2439. return NETDEV_TX_OK;
  2440. }
  2441. if (unlikely(len == 0)) {
  2442. dev_kfree_skb_any(skb);
  2443. BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
  2444. return NETDEV_TX_OK;
  2445. }
  2446. tcb = bnad->tx_info[0].tcb[txq_id];
  2447. /*
  2448. * Takes care of the Tx that is scheduled between clearing the flag
  2449. * and the netif_tx_stop_all_queues() call.
  2450. */
  2451. if (unlikely(!tcb || !test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))) {
  2452. dev_kfree_skb_any(skb);
  2453. BNAD_UPDATE_CTR(bnad, tx_skb_stopping);
  2454. return NETDEV_TX_OK;
  2455. }
  2456. q_depth = tcb->q_depth;
  2457. prod = tcb->producer_index;
  2458. unmap_q = tcb->unmap_q;
  2459. vectors = 1 + skb_shinfo(skb)->nr_frags;
  2460. wis = BNA_TXQ_WI_NEEDED(vectors); /* 4 vectors per work item */
  2461. if (unlikely(vectors > BFI_TX_MAX_VECTORS_PER_PKT)) {
  2462. dev_kfree_skb_any(skb);
  2463. BNAD_UPDATE_CTR(bnad, tx_skb_max_vectors);
  2464. return NETDEV_TX_OK;
  2465. }
  2466. /* Check for available TxQ resources */
  2467. if (unlikely(wis > BNA_QE_FREE_CNT(tcb, q_depth))) {
  2468. if ((*tcb->hw_consumer_index != tcb->consumer_index) &&
  2469. !test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
  2470. u32 sent;
  2471. sent = bnad_txcmpl_process(bnad, tcb);
  2472. if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
  2473. bna_ib_ack(tcb->i_dbell, sent);
  2474. smp_mb__before_atomic();
  2475. clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
  2476. } else {
  2477. netif_stop_queue(netdev);
  2478. BNAD_UPDATE_CTR(bnad, netif_queue_stop);
  2479. }
  2480. smp_mb();
  2481. /*
  2482. * Check again to deal with race condition between
  2483. * netif_stop_queue here, and netif_wake_queue in
  2484. * interrupt handler which is not inside netif tx lock.
  2485. */
  2486. if (likely(wis > BNA_QE_FREE_CNT(tcb, q_depth))) {
  2487. BNAD_UPDATE_CTR(bnad, netif_queue_stop);
  2488. return NETDEV_TX_BUSY;
  2489. } else {
  2490. netif_wake_queue(netdev);
  2491. BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
  2492. }
  2493. }
  2494. txqent = &((struct bna_txq_entry *)tcb->sw_q)[prod];
  2495. head_unmap = &unmap_q[prod];
  2496. /* Program the opcode, flags, frame_len, num_vectors in WI */
  2497. if (bnad_txq_wi_prepare(bnad, tcb, skb, txqent)) {
  2498. dev_kfree_skb_any(skb);
  2499. return NETDEV_TX_OK;
  2500. }
  2501. txqent->hdr.wi.reserved = 0;
  2502. txqent->hdr.wi.num_vectors = vectors;
  2503. head_unmap->skb = skb;
  2504. head_unmap->nvecs = 0;
  2505. /* Program the vectors */
  2506. unmap = head_unmap;
  2507. dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
  2508. len, DMA_TO_DEVICE);
  2509. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  2510. dev_kfree_skb_any(skb);
  2511. BNAD_UPDATE_CTR(bnad, tx_skb_map_failed);
  2512. return NETDEV_TX_OK;
  2513. }
  2514. BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[0].host_addr);
  2515. txqent->vector[0].length = htons(len);
  2516. dma_unmap_addr_set(&unmap->vectors[0], dma_addr, dma_addr);
  2517. head_unmap->nvecs++;
  2518. for (i = 0, vect_id = 0; i < vectors - 1; i++) {
  2519. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2520. u32 size = skb_frag_size(frag);
  2521. if (unlikely(size == 0)) {
  2522. /* Undo the changes starting at tcb->producer_index */
  2523. bnad_tx_buff_unmap(bnad, unmap_q, q_depth,
  2524. tcb->producer_index);
  2525. dev_kfree_skb_any(skb);
  2526. BNAD_UPDATE_CTR(bnad, tx_skb_frag_zero);
  2527. return NETDEV_TX_OK;
  2528. }
  2529. len += size;
  2530. vect_id++;
  2531. if (vect_id == BFI_TX_MAX_VECTORS_PER_WI) {
  2532. vect_id = 0;
  2533. BNA_QE_INDX_INC(prod, q_depth);
  2534. txqent = &((struct bna_txq_entry *)tcb->sw_q)[prod];
  2535. txqent->hdr.wi_ext.opcode = htons(BNA_TXQ_WI_EXTENSION);
  2536. unmap = &unmap_q[prod];
  2537. }
  2538. dma_addr = skb_frag_dma_map(&bnad->pcidev->dev, frag,
  2539. 0, size, DMA_TO_DEVICE);
  2540. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  2541. /* Undo the changes starting at tcb->producer_index */
  2542. bnad_tx_buff_unmap(bnad, unmap_q, q_depth,
  2543. tcb->producer_index);
  2544. dev_kfree_skb_any(skb);
  2545. BNAD_UPDATE_CTR(bnad, tx_skb_map_failed);
  2546. return NETDEV_TX_OK;
  2547. }
  2548. dma_unmap_len_set(&unmap->vectors[vect_id], dma_len, size);
  2549. BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[vect_id].host_addr);
  2550. txqent->vector[vect_id].length = htons(size);
  2551. dma_unmap_addr_set(&unmap->vectors[vect_id], dma_addr,
  2552. dma_addr);
  2553. head_unmap->nvecs++;
  2554. }
  2555. if (unlikely(len != skb->len)) {
  2556. /* Undo the changes starting at tcb->producer_index */
  2557. bnad_tx_buff_unmap(bnad, unmap_q, q_depth, tcb->producer_index);
  2558. dev_kfree_skb_any(skb);
  2559. BNAD_UPDATE_CTR(bnad, tx_skb_len_mismatch);
  2560. return NETDEV_TX_OK;
  2561. }
  2562. BNA_QE_INDX_INC(prod, q_depth);
  2563. tcb->producer_index = prod;
  2564. wmb();
  2565. if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
  2566. return NETDEV_TX_OK;
  2567. skb_tx_timestamp(skb);
  2568. bna_txq_prod_indx_doorbell(tcb);
  2569. return NETDEV_TX_OK;
  2570. }
  2571. /*
  2572. * Used spin_lock to synchronize reading of stats structures, which
  2573. * is written by BNA under the same lock.
  2574. */
  2575. static void
  2576. bnad_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
  2577. {
  2578. struct bnad *bnad = netdev_priv(netdev);
  2579. unsigned long flags;
  2580. spin_lock_irqsave(&bnad->bna_lock, flags);
  2581. bnad_netdev_qstats_fill(bnad, stats);
  2582. bnad_netdev_hwstats_fill(bnad, stats);
  2583. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2584. }
  2585. static void
  2586. bnad_set_rx_ucast_fltr(struct bnad *bnad)
  2587. {
  2588. struct net_device *netdev = bnad->netdev;
  2589. int uc_count = netdev_uc_count(netdev);
  2590. enum bna_cb_status ret;
  2591. u8 *mac_list;
  2592. struct netdev_hw_addr *ha;
  2593. int entry;
  2594. if (netdev_uc_empty(bnad->netdev)) {
  2595. bna_rx_ucast_listset(bnad->rx_info[0].rx, 0, NULL);
  2596. return;
  2597. }
  2598. if (uc_count > bna_attr(&bnad->bna)->num_ucmac)
  2599. goto mode_default;
  2600. mac_list = kcalloc(ETH_ALEN, uc_count, GFP_ATOMIC);
  2601. if (mac_list == NULL)
  2602. goto mode_default;
  2603. entry = 0;
  2604. netdev_for_each_uc_addr(ha, netdev) {
  2605. ether_addr_copy(&mac_list[entry * ETH_ALEN], &ha->addr[0]);
  2606. entry++;
  2607. }
  2608. ret = bna_rx_ucast_listset(bnad->rx_info[0].rx, entry, mac_list);
  2609. kfree(mac_list);
  2610. if (ret != BNA_CB_SUCCESS)
  2611. goto mode_default;
  2612. return;
  2613. /* ucast packets not in UCAM are routed to default function */
  2614. mode_default:
  2615. bnad->cfg_flags |= BNAD_CF_DEFAULT;
  2616. bna_rx_ucast_listset(bnad->rx_info[0].rx, 0, NULL);
  2617. }
  2618. static void
  2619. bnad_set_rx_mcast_fltr(struct bnad *bnad)
  2620. {
  2621. struct net_device *netdev = bnad->netdev;
  2622. int mc_count = netdev_mc_count(netdev);
  2623. enum bna_cb_status ret;
  2624. u8 *mac_list;
  2625. if (netdev->flags & IFF_ALLMULTI)
  2626. goto mode_allmulti;
  2627. if (netdev_mc_empty(netdev))
  2628. return;
  2629. if (mc_count > bna_attr(&bnad->bna)->num_mcmac)
  2630. goto mode_allmulti;
  2631. mac_list = kcalloc(mc_count + 1, ETH_ALEN, GFP_ATOMIC);
  2632. if (mac_list == NULL)
  2633. goto mode_allmulti;
  2634. ether_addr_copy(&mac_list[0], &bnad_bcast_addr[0]);
  2635. /* copy rest of the MCAST addresses */
  2636. bnad_netdev_mc_list_get(netdev, mac_list);
  2637. ret = bna_rx_mcast_listset(bnad->rx_info[0].rx, mc_count + 1, mac_list);
  2638. kfree(mac_list);
  2639. if (ret != BNA_CB_SUCCESS)
  2640. goto mode_allmulti;
  2641. return;
  2642. mode_allmulti:
  2643. bnad->cfg_flags |= BNAD_CF_ALLMULTI;
  2644. bna_rx_mcast_delall(bnad->rx_info[0].rx);
  2645. }
  2646. void
  2647. bnad_set_rx_mode(struct net_device *netdev)
  2648. {
  2649. struct bnad *bnad = netdev_priv(netdev);
  2650. enum bna_rxmode new_mode, mode_mask;
  2651. unsigned long flags;
  2652. spin_lock_irqsave(&bnad->bna_lock, flags);
  2653. if (bnad->rx_info[0].rx == NULL) {
  2654. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2655. return;
  2656. }
  2657. /* clear bnad flags to update it with new settings */
  2658. bnad->cfg_flags &= ~(BNAD_CF_PROMISC | BNAD_CF_DEFAULT |
  2659. BNAD_CF_ALLMULTI);
  2660. new_mode = 0;
  2661. if (netdev->flags & IFF_PROMISC) {
  2662. new_mode |= BNAD_RXMODE_PROMISC_DEFAULT;
  2663. bnad->cfg_flags |= BNAD_CF_PROMISC;
  2664. } else {
  2665. bnad_set_rx_mcast_fltr(bnad);
  2666. if (bnad->cfg_flags & BNAD_CF_ALLMULTI)
  2667. new_mode |= BNA_RXMODE_ALLMULTI;
  2668. bnad_set_rx_ucast_fltr(bnad);
  2669. if (bnad->cfg_flags & BNAD_CF_DEFAULT)
  2670. new_mode |= BNA_RXMODE_DEFAULT;
  2671. }
  2672. mode_mask = BNA_RXMODE_PROMISC | BNA_RXMODE_DEFAULT |
  2673. BNA_RXMODE_ALLMULTI;
  2674. bna_rx_mode_set(bnad->rx_info[0].rx, new_mode, mode_mask);
  2675. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2676. }
  2677. /*
  2678. * bna_lock is used to sync writes to netdev->addr
  2679. * conf_lock cannot be used since this call may be made
  2680. * in a non-blocking context.
  2681. */
  2682. static int
  2683. bnad_set_mac_address(struct net_device *netdev, void *addr)
  2684. {
  2685. int err;
  2686. struct bnad *bnad = netdev_priv(netdev);
  2687. struct sockaddr *sa = (struct sockaddr *)addr;
  2688. unsigned long flags;
  2689. spin_lock_irqsave(&bnad->bna_lock, flags);
  2690. err = bnad_mac_addr_set_locked(bnad, sa->sa_data);
  2691. if (!err)
  2692. eth_hw_addr_set(netdev, sa->sa_data);
  2693. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2694. return err;
  2695. }
  2696. static int
  2697. bnad_mtu_set(struct bnad *bnad, int frame_size)
  2698. {
  2699. unsigned long flags;
  2700. init_completion(&bnad->bnad_completions.mtu_comp);
  2701. spin_lock_irqsave(&bnad->bna_lock, flags);
  2702. bna_enet_mtu_set(&bnad->bna.enet, frame_size, bnad_cb_enet_mtu_set);
  2703. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2704. wait_for_completion(&bnad->bnad_completions.mtu_comp);
  2705. return bnad->bnad_completions.mtu_comp_status;
  2706. }
  2707. static int
  2708. bnad_change_mtu(struct net_device *netdev, int new_mtu)
  2709. {
  2710. int err, mtu;
  2711. struct bnad *bnad = netdev_priv(netdev);
  2712. u32 frame, new_frame;
  2713. mutex_lock(&bnad->conf_mutex);
  2714. mtu = netdev->mtu;
  2715. WRITE_ONCE(netdev->mtu, new_mtu);
  2716. frame = BNAD_FRAME_SIZE(mtu);
  2717. new_frame = BNAD_FRAME_SIZE(new_mtu);
  2718. /* check if multi-buffer needs to be enabled */
  2719. if (BNAD_PCI_DEV_IS_CAT2(bnad) &&
  2720. netif_running(bnad->netdev)) {
  2721. /* only when transition is over 4K */
  2722. if ((frame <= 4096 && new_frame > 4096) ||
  2723. (frame > 4096 && new_frame <= 4096))
  2724. bnad_reinit_rx(bnad);
  2725. }
  2726. err = bnad_mtu_set(bnad, new_frame);
  2727. if (err)
  2728. err = -EBUSY;
  2729. mutex_unlock(&bnad->conf_mutex);
  2730. return err;
  2731. }
  2732. static int
  2733. bnad_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
  2734. {
  2735. struct bnad *bnad = netdev_priv(netdev);
  2736. unsigned long flags;
  2737. if (!bnad->rx_info[0].rx)
  2738. return 0;
  2739. mutex_lock(&bnad->conf_mutex);
  2740. spin_lock_irqsave(&bnad->bna_lock, flags);
  2741. bna_rx_vlan_add(bnad->rx_info[0].rx, vid);
  2742. set_bit(vid, bnad->active_vlans);
  2743. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2744. mutex_unlock(&bnad->conf_mutex);
  2745. return 0;
  2746. }
  2747. static int
  2748. bnad_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
  2749. {
  2750. struct bnad *bnad = netdev_priv(netdev);
  2751. unsigned long flags;
  2752. if (!bnad->rx_info[0].rx)
  2753. return 0;
  2754. mutex_lock(&bnad->conf_mutex);
  2755. spin_lock_irqsave(&bnad->bna_lock, flags);
  2756. clear_bit(vid, bnad->active_vlans);
  2757. bna_rx_vlan_del(bnad->rx_info[0].rx, vid);
  2758. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2759. mutex_unlock(&bnad->conf_mutex);
  2760. return 0;
  2761. }
  2762. static int bnad_set_features(struct net_device *dev, netdev_features_t features)
  2763. {
  2764. struct bnad *bnad = netdev_priv(dev);
  2765. netdev_features_t changed = features ^ dev->features;
  2766. if ((changed & NETIF_F_HW_VLAN_CTAG_RX) && netif_running(dev)) {
  2767. unsigned long flags;
  2768. spin_lock_irqsave(&bnad->bna_lock, flags);
  2769. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2770. bna_rx_vlan_strip_enable(bnad->rx_info[0].rx);
  2771. else
  2772. bna_rx_vlan_strip_disable(bnad->rx_info[0].rx);
  2773. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2774. }
  2775. return 0;
  2776. }
  2777. #ifdef CONFIG_NET_POLL_CONTROLLER
  2778. static void
  2779. bnad_netpoll(struct net_device *netdev)
  2780. {
  2781. struct bnad *bnad = netdev_priv(netdev);
  2782. struct bnad_rx_info *rx_info;
  2783. struct bnad_rx_ctrl *rx_ctrl;
  2784. u32 curr_mask;
  2785. int i, j;
  2786. if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
  2787. bna_intx_disable(&bnad->bna, curr_mask);
  2788. bnad_isr(bnad->pcidev->irq, netdev);
  2789. bna_intx_enable(&bnad->bna, curr_mask);
  2790. } else {
  2791. /*
  2792. * Tx processing may happen in sending context, so no need
  2793. * to explicitly process completions here
  2794. */
  2795. /* Rx processing */
  2796. for (i = 0; i < bnad->num_rx; i++) {
  2797. rx_info = &bnad->rx_info[i];
  2798. if (!rx_info->rx)
  2799. continue;
  2800. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  2801. rx_ctrl = &rx_info->rx_ctrl[j];
  2802. if (rx_ctrl->ccb)
  2803. bnad_netif_rx_schedule_poll(bnad,
  2804. rx_ctrl->ccb);
  2805. }
  2806. }
  2807. }
  2808. }
  2809. #endif
  2810. static const struct net_device_ops bnad_netdev_ops = {
  2811. .ndo_open = bnad_open,
  2812. .ndo_stop = bnad_stop,
  2813. .ndo_start_xmit = bnad_start_xmit,
  2814. .ndo_get_stats64 = bnad_get_stats64,
  2815. .ndo_set_rx_mode = bnad_set_rx_mode,
  2816. .ndo_validate_addr = eth_validate_addr,
  2817. .ndo_set_mac_address = bnad_set_mac_address,
  2818. .ndo_change_mtu = bnad_change_mtu,
  2819. .ndo_vlan_rx_add_vid = bnad_vlan_rx_add_vid,
  2820. .ndo_vlan_rx_kill_vid = bnad_vlan_rx_kill_vid,
  2821. .ndo_set_features = bnad_set_features,
  2822. #ifdef CONFIG_NET_POLL_CONTROLLER
  2823. .ndo_poll_controller = bnad_netpoll
  2824. #endif
  2825. };
  2826. static void
  2827. bnad_netdev_init(struct bnad *bnad)
  2828. {
  2829. struct net_device *netdev = bnad->netdev;
  2830. netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
  2831. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2832. NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_TX |
  2833. NETIF_F_HW_VLAN_CTAG_RX;
  2834. netdev->vlan_features = NETIF_F_SG | NETIF_F_HIGHDMA |
  2835. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2836. NETIF_F_TSO | NETIF_F_TSO6;
  2837. netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER |
  2838. NETIF_F_HIGHDMA;
  2839. netdev->mem_start = bnad->mmio_start;
  2840. netdev->mem_end = bnad->mmio_start + bnad->mmio_len - 1;
  2841. /* MTU range: 46 - 9000 */
  2842. netdev->min_mtu = ETH_ZLEN - ETH_HLEN;
  2843. netdev->max_mtu = BNAD_JUMBO_MTU;
  2844. netdev->netdev_ops = &bnad_netdev_ops;
  2845. bnad_set_ethtool_ops(netdev);
  2846. }
  2847. /*
  2848. * 1. Initialize the bnad structure
  2849. * 2. Setup netdev pointer in pci_dev
  2850. * 3. Initialize no. of TxQ & CQs & MSIX vectors
  2851. * 4. Initialize work queue.
  2852. */
  2853. static int
  2854. bnad_init(struct bnad *bnad,
  2855. struct pci_dev *pdev, struct net_device *netdev)
  2856. {
  2857. unsigned long flags;
  2858. SET_NETDEV_DEV(netdev, &pdev->dev);
  2859. pci_set_drvdata(pdev, netdev);
  2860. bnad->netdev = netdev;
  2861. bnad->pcidev = pdev;
  2862. bnad->mmio_start = pci_resource_start(pdev, 0);
  2863. bnad->mmio_len = pci_resource_len(pdev, 0);
  2864. bnad->bar0 = ioremap(bnad->mmio_start, bnad->mmio_len);
  2865. if (!bnad->bar0) {
  2866. dev_err(&pdev->dev, "ioremap for bar0 failed\n");
  2867. return -ENOMEM;
  2868. }
  2869. dev_info(&pdev->dev, "bar0 mapped to %p, len %llu\n", bnad->bar0,
  2870. (unsigned long long) bnad->mmio_len);
  2871. spin_lock_irqsave(&bnad->bna_lock, flags);
  2872. if (!bnad_msix_disable)
  2873. bnad->cfg_flags = BNAD_CF_MSIX;
  2874. bnad->cfg_flags |= BNAD_CF_DIM_ENABLED;
  2875. bnad_q_num_init(bnad);
  2876. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2877. bnad->msix_num = (bnad->num_tx * bnad->num_txq_per_tx) +
  2878. (bnad->num_rx * bnad->num_rxp_per_rx) +
  2879. BNAD_MAILBOX_MSIX_VECTORS;
  2880. bnad->txq_depth = BNAD_TXQ_DEPTH;
  2881. bnad->rxq_depth = BNAD_RXQ_DEPTH;
  2882. bnad->tx_coalescing_timeo = BFI_TX_COALESCING_TIMEO;
  2883. bnad->rx_coalescing_timeo = BFI_RX_COALESCING_TIMEO;
  2884. sprintf(bnad->wq_name, "%s_wq_%d", BNAD_NAME, bnad->id);
  2885. bnad->work_q = create_singlethread_workqueue(bnad->wq_name);
  2886. if (!bnad->work_q) {
  2887. iounmap(bnad->bar0);
  2888. return -ENOMEM;
  2889. }
  2890. return 0;
  2891. }
  2892. /*
  2893. * Must be called after bnad_pci_uninit()
  2894. * so that iounmap() and pci_set_drvdata(NULL)
  2895. * happens only after PCI uninitialization.
  2896. */
  2897. static void
  2898. bnad_uninit(struct bnad *bnad)
  2899. {
  2900. if (bnad->work_q) {
  2901. destroy_workqueue(bnad->work_q);
  2902. bnad->work_q = NULL;
  2903. }
  2904. if (bnad->bar0)
  2905. iounmap(bnad->bar0);
  2906. }
  2907. /*
  2908. * Initialize locks
  2909. a) Per ioceth mutes used for serializing configuration
  2910. changes from OS interface
  2911. b) spin lock used to protect bna state machine
  2912. */
  2913. static void
  2914. bnad_lock_init(struct bnad *bnad)
  2915. {
  2916. spin_lock_init(&bnad->bna_lock);
  2917. mutex_init(&bnad->conf_mutex);
  2918. }
  2919. static void
  2920. bnad_lock_uninit(struct bnad *bnad)
  2921. {
  2922. mutex_destroy(&bnad->conf_mutex);
  2923. }
  2924. /* PCI Initialization */
  2925. static int
  2926. bnad_pci_init(struct bnad *bnad, struct pci_dev *pdev)
  2927. {
  2928. int err;
  2929. err = pci_enable_device(pdev);
  2930. if (err)
  2931. return err;
  2932. err = pci_request_regions(pdev, BNAD_NAME);
  2933. if (err)
  2934. goto disable_device;
  2935. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2936. if (err)
  2937. goto release_regions;
  2938. pci_set_master(pdev);
  2939. return 0;
  2940. release_regions:
  2941. pci_release_regions(pdev);
  2942. disable_device:
  2943. pci_disable_device(pdev);
  2944. return err;
  2945. }
  2946. static void
  2947. bnad_pci_uninit(struct pci_dev *pdev)
  2948. {
  2949. pci_release_regions(pdev);
  2950. pci_disable_device(pdev);
  2951. }
  2952. static int
  2953. bnad_pci_probe(struct pci_dev *pdev,
  2954. const struct pci_device_id *pcidev_id)
  2955. {
  2956. int err;
  2957. struct bnad *bnad;
  2958. struct bna *bna;
  2959. struct net_device *netdev;
  2960. struct bfa_pcidev pcidev_info;
  2961. unsigned long flags;
  2962. mutex_lock(&bnad_fwimg_mutex);
  2963. if (!cna_get_firmware_buf(pdev)) {
  2964. mutex_unlock(&bnad_fwimg_mutex);
  2965. dev_err(&pdev->dev, "failed to load firmware image!\n");
  2966. return -ENODEV;
  2967. }
  2968. mutex_unlock(&bnad_fwimg_mutex);
  2969. /*
  2970. * Allocates sizeof(struct net_device + struct bnad)
  2971. * bnad = netdev->priv
  2972. */
  2973. netdev = alloc_etherdev(sizeof(struct bnad));
  2974. if (!netdev) {
  2975. err = -ENOMEM;
  2976. return err;
  2977. }
  2978. bnad = netdev_priv(netdev);
  2979. bnad_lock_init(bnad);
  2980. bnad->id = atomic_inc_return(&bna_id) - 1;
  2981. mutex_lock(&bnad->conf_mutex);
  2982. /* PCI initialization */
  2983. err = bnad_pci_init(bnad, pdev);
  2984. if (err)
  2985. goto unlock_mutex;
  2986. /*
  2987. * Initialize bnad structure
  2988. * Setup relation between pci_dev & netdev
  2989. */
  2990. err = bnad_init(bnad, pdev, netdev);
  2991. if (err)
  2992. goto pci_uninit;
  2993. /* Initialize netdev structure, set up ethtool ops */
  2994. bnad_netdev_init(bnad);
  2995. /* Set link to down state */
  2996. netif_carrier_off(netdev);
  2997. /* Setup the debugfs node for this bfad */
  2998. if (bna_debugfs_enable)
  2999. bnad_debugfs_init(bnad);
  3000. /* Get resource requirement form bna */
  3001. spin_lock_irqsave(&bnad->bna_lock, flags);
  3002. bna_res_req(&bnad->res_info[0]);
  3003. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3004. /* Allocate resources from bna */
  3005. err = bnad_res_alloc(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
  3006. if (err)
  3007. goto drv_uninit;
  3008. bna = &bnad->bna;
  3009. /* Setup pcidev_info for bna_init() */
  3010. pcidev_info.pci_slot = PCI_SLOT(bnad->pcidev->devfn);
  3011. pcidev_info.pci_func = PCI_FUNC(bnad->pcidev->devfn);
  3012. pcidev_info.device_id = bnad->pcidev->device;
  3013. pcidev_info.pci_bar_kva = bnad->bar0;
  3014. spin_lock_irqsave(&bnad->bna_lock, flags);
  3015. bna_init(bna, bnad, &pcidev_info, &bnad->res_info[0]);
  3016. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3017. bnad->stats.bna_stats = &bna->stats;
  3018. bnad_enable_msix(bnad);
  3019. err = bnad_mbox_irq_alloc(bnad);
  3020. if (err)
  3021. goto res_free;
  3022. /* Set up timers */
  3023. timer_setup(&bnad->bna.ioceth.ioc.ioc_timer, bnad_ioc_timeout, 0);
  3024. timer_setup(&bnad->bna.ioceth.ioc.hb_timer, bnad_ioc_hb_check, 0);
  3025. timer_setup(&bnad->bna.ioceth.ioc.iocpf_timer, bnad_iocpf_timeout, 0);
  3026. timer_setup(&bnad->bna.ioceth.ioc.sem_timer, bnad_iocpf_sem_timeout,
  3027. 0);
  3028. /*
  3029. * Start the chip
  3030. * If the call back comes with error, we bail out.
  3031. * This is a catastrophic error.
  3032. */
  3033. err = bnad_ioceth_enable(bnad);
  3034. if (err) {
  3035. dev_err(&pdev->dev, "initialization failed err=%d\n", err);
  3036. goto probe_success;
  3037. }
  3038. spin_lock_irqsave(&bnad->bna_lock, flags);
  3039. if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
  3040. bna_num_rxp_set(bna, BNAD_NUM_RXP + 1)) {
  3041. bnad_q_num_adjust(bnad, bna_attr(bna)->num_txq - 1,
  3042. bna_attr(bna)->num_rxp - 1);
  3043. if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
  3044. bna_num_rxp_set(bna, BNAD_NUM_RXP + 1))
  3045. err = -EIO;
  3046. }
  3047. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3048. if (err)
  3049. goto disable_ioceth;
  3050. spin_lock_irqsave(&bnad->bna_lock, flags);
  3051. bna_mod_res_req(&bnad->bna, &bnad->mod_res_info[0]);
  3052. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3053. err = bnad_res_alloc(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
  3054. if (err) {
  3055. err = -EIO;
  3056. goto disable_ioceth;
  3057. }
  3058. spin_lock_irqsave(&bnad->bna_lock, flags);
  3059. bna_mod_init(&bnad->bna, &bnad->mod_res_info[0]);
  3060. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3061. /* Get the burnt-in mac */
  3062. spin_lock_irqsave(&bnad->bna_lock, flags);
  3063. bna_enet_perm_mac_get(&bna->enet, bnad->perm_addr);
  3064. bnad_set_netdev_perm_addr(bnad);
  3065. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3066. mutex_unlock(&bnad->conf_mutex);
  3067. /* Finally, reguister with net_device layer */
  3068. err = register_netdev(netdev);
  3069. if (err) {
  3070. dev_err(&pdev->dev, "registering net device failed\n");
  3071. goto probe_uninit;
  3072. }
  3073. set_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags);
  3074. return 0;
  3075. probe_success:
  3076. mutex_unlock(&bnad->conf_mutex);
  3077. return 0;
  3078. probe_uninit:
  3079. mutex_lock(&bnad->conf_mutex);
  3080. bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
  3081. disable_ioceth:
  3082. bnad_ioceth_disable(bnad);
  3083. del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
  3084. del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
  3085. del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
  3086. spin_lock_irqsave(&bnad->bna_lock, flags);
  3087. bna_uninit(bna);
  3088. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3089. bnad_mbox_irq_free(bnad);
  3090. bnad_disable_msix(bnad);
  3091. res_free:
  3092. bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
  3093. drv_uninit:
  3094. /* Remove the debugfs node for this bnad */
  3095. kfree(bnad->regdata);
  3096. bnad_debugfs_uninit(bnad);
  3097. bnad_uninit(bnad);
  3098. pci_uninit:
  3099. bnad_pci_uninit(pdev);
  3100. unlock_mutex:
  3101. mutex_unlock(&bnad->conf_mutex);
  3102. bnad_lock_uninit(bnad);
  3103. free_netdev(netdev);
  3104. return err;
  3105. }
  3106. static void
  3107. bnad_pci_remove(struct pci_dev *pdev)
  3108. {
  3109. struct net_device *netdev = pci_get_drvdata(pdev);
  3110. struct bnad *bnad;
  3111. struct bna *bna;
  3112. unsigned long flags;
  3113. if (!netdev)
  3114. return;
  3115. bnad = netdev_priv(netdev);
  3116. bna = &bnad->bna;
  3117. if (test_and_clear_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags))
  3118. unregister_netdev(netdev);
  3119. mutex_lock(&bnad->conf_mutex);
  3120. bnad_ioceth_disable(bnad);
  3121. del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
  3122. del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
  3123. del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
  3124. spin_lock_irqsave(&bnad->bna_lock, flags);
  3125. bna_uninit(bna);
  3126. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3127. bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
  3128. bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
  3129. bnad_mbox_irq_free(bnad);
  3130. bnad_disable_msix(bnad);
  3131. bnad_pci_uninit(pdev);
  3132. mutex_unlock(&bnad->conf_mutex);
  3133. bnad_lock_uninit(bnad);
  3134. /* Remove the debugfs node for this bnad */
  3135. kfree(bnad->regdata);
  3136. bnad_debugfs_uninit(bnad);
  3137. bnad_uninit(bnad);
  3138. free_netdev(netdev);
  3139. }
  3140. static const struct pci_device_id bnad_pci_id_table[] = {
  3141. {
  3142. PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
  3143. PCI_DEVICE_ID_BROCADE_CT),
  3144. .class = PCI_CLASS_NETWORK_ETHERNET << 8,
  3145. .class_mask = 0xffff00
  3146. },
  3147. {
  3148. PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
  3149. BFA_PCI_DEVICE_ID_CT2),
  3150. .class = PCI_CLASS_NETWORK_ETHERNET << 8,
  3151. .class_mask = 0xffff00
  3152. },
  3153. {0, },
  3154. };
  3155. MODULE_DEVICE_TABLE(pci, bnad_pci_id_table);
  3156. static struct pci_driver bnad_pci_driver = {
  3157. .name = BNAD_NAME,
  3158. .id_table = bnad_pci_id_table,
  3159. .probe = bnad_pci_probe,
  3160. .remove = bnad_pci_remove,
  3161. };
  3162. static int __init
  3163. bnad_module_init(void)
  3164. {
  3165. int err;
  3166. bfa_nw_ioc_auto_recover(bnad_ioc_auto_recover);
  3167. err = pci_register_driver(&bnad_pci_driver);
  3168. if (err < 0) {
  3169. pr_err("bna: PCI driver registration failed err=%d\n", err);
  3170. return err;
  3171. }
  3172. return 0;
  3173. }
  3174. static void __exit
  3175. bnad_module_exit(void)
  3176. {
  3177. pci_unregister_driver(&bnad_pci_driver);
  3178. release_firmware(bfi_fw);
  3179. }
  3180. module_init(bnad_module_init);
  3181. module_exit(bnad_module_exit);
  3182. MODULE_AUTHOR("Brocade");
  3183. MODULE_LICENSE("GPL");
  3184. MODULE_DESCRIPTION("QLogic BR-series 10G PCIe Ethernet driver");
  3185. MODULE_FIRMWARE(CNA_FW_FILE_CT);
  3186. MODULE_FIRMWARE(CNA_FW_FILE_CT2);