bnad.c 93 KB

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