bnad.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888
  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, 0644);
  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(struct timer_list *t)
  1411. {
  1412. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.ioc_timer);
  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(struct timer_list *t)
  1420. {
  1421. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.hb_timer);
  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(struct timer_list *t)
  1429. {
  1430. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.iocpf_timer);
  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(struct timer_list *t)
  1438. {
  1439. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.sem_timer);
  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(struct timer_list *t)
  1457. {
  1458. struct bnad *bnad = from_timer(bnad, t, dim_timer);
  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(struct timer_list *t)
  1486. {
  1487. struct bnad *bnad = from_timer(bnad, t, stats_timer);
  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. timer_setup(&bnad->dim_timer, bnad_dim_timeout, 0);
  1506. set_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
  1507. mod_timer(&bnad->dim_timer,
  1508. jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
  1509. }
  1510. }
  1511. /*
  1512. * Set up timer for statistics
  1513. * Called with mutex_lock(&bnad->conf_mutex) held
  1514. */
  1515. static void
  1516. bnad_stats_timer_start(struct bnad *bnad)
  1517. {
  1518. unsigned long flags;
  1519. spin_lock_irqsave(&bnad->bna_lock, flags);
  1520. if (!test_and_set_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags)) {
  1521. timer_setup(&bnad->stats_timer, bnad_stats_timeout, 0);
  1522. mod_timer(&bnad->stats_timer,
  1523. jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
  1524. }
  1525. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1526. }
  1527. /*
  1528. * Stops the stats timer
  1529. * Called with mutex_lock(&bnad->conf_mutex) held
  1530. */
  1531. static void
  1532. bnad_stats_timer_stop(struct bnad *bnad)
  1533. {
  1534. int to_del = 0;
  1535. unsigned long flags;
  1536. spin_lock_irqsave(&bnad->bna_lock, flags);
  1537. if (test_and_clear_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
  1538. to_del = 1;
  1539. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1540. if (to_del)
  1541. del_timer_sync(&bnad->stats_timer);
  1542. }
  1543. /* Utilities */
  1544. static void
  1545. bnad_netdev_mc_list_get(struct net_device *netdev, u8 *mc_list)
  1546. {
  1547. int i = 1; /* Index 0 has broadcast address */
  1548. struct netdev_hw_addr *mc_addr;
  1549. netdev_for_each_mc_addr(mc_addr, netdev) {
  1550. ether_addr_copy(&mc_list[i * ETH_ALEN], &mc_addr->addr[0]);
  1551. i++;
  1552. }
  1553. }
  1554. static int
  1555. bnad_napi_poll_rx(struct napi_struct *napi, int budget)
  1556. {
  1557. struct bnad_rx_ctrl *rx_ctrl =
  1558. container_of(napi, struct bnad_rx_ctrl, napi);
  1559. struct bnad *bnad = rx_ctrl->bnad;
  1560. int rcvd = 0;
  1561. rx_ctrl->rx_poll_ctr++;
  1562. if (!netif_carrier_ok(bnad->netdev))
  1563. goto poll_exit;
  1564. rcvd = bnad_cq_process(bnad, rx_ctrl->ccb, budget);
  1565. if (rcvd >= budget)
  1566. return rcvd;
  1567. poll_exit:
  1568. napi_complete_done(napi, rcvd);
  1569. rx_ctrl->rx_complete++;
  1570. if (rx_ctrl->ccb)
  1571. bnad_enable_rx_irq_unsafe(rx_ctrl->ccb);
  1572. return rcvd;
  1573. }
  1574. #define BNAD_NAPI_POLL_QUOTA 64
  1575. static void
  1576. bnad_napi_add(struct bnad *bnad, u32 rx_id)
  1577. {
  1578. struct bnad_rx_ctrl *rx_ctrl;
  1579. int i;
  1580. /* Initialize & enable NAPI */
  1581. for (i = 0; i < bnad->num_rxp_per_rx; i++) {
  1582. rx_ctrl = &bnad->rx_info[rx_id].rx_ctrl[i];
  1583. netif_napi_add(bnad->netdev, &rx_ctrl->napi,
  1584. bnad_napi_poll_rx, BNAD_NAPI_POLL_QUOTA);
  1585. }
  1586. }
  1587. static void
  1588. bnad_napi_delete(struct bnad *bnad, u32 rx_id)
  1589. {
  1590. int i;
  1591. /* First disable and then clean up */
  1592. for (i = 0; i < bnad->num_rxp_per_rx; i++)
  1593. netif_napi_del(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
  1594. }
  1595. /* Should be held with conf_lock held */
  1596. void
  1597. bnad_destroy_tx(struct bnad *bnad, u32 tx_id)
  1598. {
  1599. struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
  1600. struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
  1601. unsigned long flags;
  1602. if (!tx_info->tx)
  1603. return;
  1604. init_completion(&bnad->bnad_completions.tx_comp);
  1605. spin_lock_irqsave(&bnad->bna_lock, flags);
  1606. bna_tx_disable(tx_info->tx, BNA_HARD_CLEANUP, bnad_cb_tx_disabled);
  1607. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1608. wait_for_completion(&bnad->bnad_completions.tx_comp);
  1609. if (tx_info->tcb[0]->intr_type == BNA_INTR_T_MSIX)
  1610. bnad_tx_msix_unregister(bnad, tx_info,
  1611. bnad->num_txq_per_tx);
  1612. spin_lock_irqsave(&bnad->bna_lock, flags);
  1613. bna_tx_destroy(tx_info->tx);
  1614. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1615. tx_info->tx = NULL;
  1616. tx_info->tx_id = 0;
  1617. bnad_tx_res_free(bnad, res_info);
  1618. }
  1619. /* Should be held with conf_lock held */
  1620. int
  1621. bnad_setup_tx(struct bnad *bnad, u32 tx_id)
  1622. {
  1623. int err;
  1624. struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
  1625. struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
  1626. struct bna_intr_info *intr_info =
  1627. &res_info[BNA_TX_RES_INTR_T_TXCMPL].res_u.intr_info;
  1628. struct bna_tx_config *tx_config = &bnad->tx_config[tx_id];
  1629. static const struct bna_tx_event_cbfn tx_cbfn = {
  1630. .tcb_setup_cbfn = bnad_cb_tcb_setup,
  1631. .tcb_destroy_cbfn = bnad_cb_tcb_destroy,
  1632. .tx_stall_cbfn = bnad_cb_tx_stall,
  1633. .tx_resume_cbfn = bnad_cb_tx_resume,
  1634. .tx_cleanup_cbfn = bnad_cb_tx_cleanup,
  1635. };
  1636. struct bna_tx *tx;
  1637. unsigned long flags;
  1638. tx_info->tx_id = tx_id;
  1639. /* Initialize the Tx object configuration */
  1640. tx_config->num_txq = bnad->num_txq_per_tx;
  1641. tx_config->txq_depth = bnad->txq_depth;
  1642. tx_config->tx_type = BNA_TX_T_REGULAR;
  1643. tx_config->coalescing_timeo = bnad->tx_coalescing_timeo;
  1644. /* Get BNA's resource requirement for one tx object */
  1645. spin_lock_irqsave(&bnad->bna_lock, flags);
  1646. bna_tx_res_req(bnad->num_txq_per_tx,
  1647. bnad->txq_depth, res_info);
  1648. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1649. /* Fill Unmap Q memory requirements */
  1650. BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_TX_RES_MEM_T_UNMAPQ],
  1651. bnad->num_txq_per_tx, (sizeof(struct bnad_tx_unmap) *
  1652. bnad->txq_depth));
  1653. /* Allocate resources */
  1654. err = bnad_tx_res_alloc(bnad, res_info, tx_id);
  1655. if (err)
  1656. return err;
  1657. /* Ask BNA to create one Tx object, supplying required resources */
  1658. spin_lock_irqsave(&bnad->bna_lock, flags);
  1659. tx = bna_tx_create(&bnad->bna, bnad, tx_config, &tx_cbfn, res_info,
  1660. tx_info);
  1661. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1662. if (!tx) {
  1663. err = -ENOMEM;
  1664. goto err_return;
  1665. }
  1666. tx_info->tx = tx;
  1667. INIT_DELAYED_WORK(&tx_info->tx_cleanup_work,
  1668. (work_func_t)bnad_tx_cleanup);
  1669. /* Register ISR for the Tx object */
  1670. if (intr_info->intr_type == BNA_INTR_T_MSIX) {
  1671. err = bnad_tx_msix_register(bnad, tx_info,
  1672. tx_id, bnad->num_txq_per_tx);
  1673. if (err)
  1674. goto cleanup_tx;
  1675. }
  1676. spin_lock_irqsave(&bnad->bna_lock, flags);
  1677. bna_tx_enable(tx);
  1678. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1679. return 0;
  1680. cleanup_tx:
  1681. spin_lock_irqsave(&bnad->bna_lock, flags);
  1682. bna_tx_destroy(tx_info->tx);
  1683. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1684. tx_info->tx = NULL;
  1685. tx_info->tx_id = 0;
  1686. err_return:
  1687. bnad_tx_res_free(bnad, res_info);
  1688. return err;
  1689. }
  1690. /* Setup the rx config for bna_rx_create */
  1691. /* bnad decides the configuration */
  1692. static void
  1693. bnad_init_rx_config(struct bnad *bnad, struct bna_rx_config *rx_config)
  1694. {
  1695. memset(rx_config, 0, sizeof(*rx_config));
  1696. rx_config->rx_type = BNA_RX_T_REGULAR;
  1697. rx_config->num_paths = bnad->num_rxp_per_rx;
  1698. rx_config->coalescing_timeo = bnad->rx_coalescing_timeo;
  1699. if (bnad->num_rxp_per_rx > 1) {
  1700. rx_config->rss_status = BNA_STATUS_T_ENABLED;
  1701. rx_config->rss_config.hash_type =
  1702. (BFI_ENET_RSS_IPV6 |
  1703. BFI_ENET_RSS_IPV6_TCP |
  1704. BFI_ENET_RSS_IPV4 |
  1705. BFI_ENET_RSS_IPV4_TCP);
  1706. rx_config->rss_config.hash_mask =
  1707. bnad->num_rxp_per_rx - 1;
  1708. netdev_rss_key_fill(rx_config->rss_config.toeplitz_hash_key,
  1709. sizeof(rx_config->rss_config.toeplitz_hash_key));
  1710. } else {
  1711. rx_config->rss_status = BNA_STATUS_T_DISABLED;
  1712. memset(&rx_config->rss_config, 0,
  1713. sizeof(rx_config->rss_config));
  1714. }
  1715. rx_config->frame_size = BNAD_FRAME_SIZE(bnad->netdev->mtu);
  1716. rx_config->q0_multi_buf = BNA_STATUS_T_DISABLED;
  1717. /* BNA_RXP_SINGLE - one data-buffer queue
  1718. * BNA_RXP_SLR - one small-buffer and one large-buffer queues
  1719. * BNA_RXP_HDS - one header-buffer and one data-buffer queues
  1720. */
  1721. /* TODO: configurable param for queue type */
  1722. rx_config->rxp_type = BNA_RXP_SLR;
  1723. if (BNAD_PCI_DEV_IS_CAT2(bnad) &&
  1724. rx_config->frame_size > 4096) {
  1725. /* though size_routing_enable is set in SLR,
  1726. * small packets may get routed to same rxq.
  1727. * set buf_size to 2048 instead of PAGE_SIZE.
  1728. */
  1729. rx_config->q0_buf_size = 2048;
  1730. /* this should be in multiples of 2 */
  1731. rx_config->q0_num_vecs = 4;
  1732. rx_config->q0_depth = bnad->rxq_depth * rx_config->q0_num_vecs;
  1733. rx_config->q0_multi_buf = BNA_STATUS_T_ENABLED;
  1734. } else {
  1735. rx_config->q0_buf_size = rx_config->frame_size;
  1736. rx_config->q0_num_vecs = 1;
  1737. rx_config->q0_depth = bnad->rxq_depth;
  1738. }
  1739. /* initialize for q1 for BNA_RXP_SLR/BNA_RXP_HDS */
  1740. if (rx_config->rxp_type == BNA_RXP_SLR) {
  1741. rx_config->q1_depth = bnad->rxq_depth;
  1742. rx_config->q1_buf_size = BFI_SMALL_RXBUF_SIZE;
  1743. }
  1744. rx_config->vlan_strip_status =
  1745. (bnad->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) ?
  1746. BNA_STATUS_T_ENABLED : BNA_STATUS_T_DISABLED;
  1747. }
  1748. static void
  1749. bnad_rx_ctrl_init(struct bnad *bnad, u32 rx_id)
  1750. {
  1751. struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
  1752. int i;
  1753. for (i = 0; i < bnad->num_rxp_per_rx; i++)
  1754. rx_info->rx_ctrl[i].bnad = bnad;
  1755. }
  1756. /* Called with mutex_lock(&bnad->conf_mutex) held */
  1757. static u32
  1758. bnad_reinit_rx(struct bnad *bnad)
  1759. {
  1760. struct net_device *netdev = bnad->netdev;
  1761. u32 err = 0, current_err = 0;
  1762. u32 rx_id = 0, count = 0;
  1763. unsigned long flags;
  1764. /* destroy and create new rx objects */
  1765. for (rx_id = 0; rx_id < bnad->num_rx; rx_id++) {
  1766. if (!bnad->rx_info[rx_id].rx)
  1767. continue;
  1768. bnad_destroy_rx(bnad, rx_id);
  1769. }
  1770. spin_lock_irqsave(&bnad->bna_lock, flags);
  1771. bna_enet_mtu_set(&bnad->bna.enet,
  1772. BNAD_FRAME_SIZE(bnad->netdev->mtu), NULL);
  1773. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1774. for (rx_id = 0; rx_id < bnad->num_rx; rx_id++) {
  1775. count++;
  1776. current_err = bnad_setup_rx(bnad, rx_id);
  1777. if (current_err && !err) {
  1778. err = current_err;
  1779. netdev_err(netdev, "RXQ:%u setup failed\n", rx_id);
  1780. }
  1781. }
  1782. /* restore rx configuration */
  1783. if (bnad->rx_info[0].rx && !err) {
  1784. bnad_restore_vlans(bnad, 0);
  1785. bnad_enable_default_bcast(bnad);
  1786. spin_lock_irqsave(&bnad->bna_lock, flags);
  1787. bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
  1788. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1789. bnad_set_rx_mode(netdev);
  1790. }
  1791. return count;
  1792. }
  1793. /* Called with bnad_conf_lock() held */
  1794. void
  1795. bnad_destroy_rx(struct bnad *bnad, u32 rx_id)
  1796. {
  1797. struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
  1798. struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
  1799. struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
  1800. unsigned long flags;
  1801. int to_del = 0;
  1802. if (!rx_info->rx)
  1803. return;
  1804. if (0 == rx_id) {
  1805. spin_lock_irqsave(&bnad->bna_lock, flags);
  1806. if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
  1807. test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
  1808. clear_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
  1809. to_del = 1;
  1810. }
  1811. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1812. if (to_del)
  1813. del_timer_sync(&bnad->dim_timer);
  1814. }
  1815. init_completion(&bnad->bnad_completions.rx_comp);
  1816. spin_lock_irqsave(&bnad->bna_lock, flags);
  1817. bna_rx_disable(rx_info->rx, BNA_HARD_CLEANUP, bnad_cb_rx_disabled);
  1818. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1819. wait_for_completion(&bnad->bnad_completions.rx_comp);
  1820. if (rx_info->rx_ctrl[0].ccb->intr_type == BNA_INTR_T_MSIX)
  1821. bnad_rx_msix_unregister(bnad, rx_info, rx_config->num_paths);
  1822. bnad_napi_delete(bnad, rx_id);
  1823. spin_lock_irqsave(&bnad->bna_lock, flags);
  1824. bna_rx_destroy(rx_info->rx);
  1825. rx_info->rx = NULL;
  1826. rx_info->rx_id = 0;
  1827. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1828. bnad_rx_res_free(bnad, res_info);
  1829. }
  1830. /* Called with mutex_lock(&bnad->conf_mutex) held */
  1831. int
  1832. bnad_setup_rx(struct bnad *bnad, u32 rx_id)
  1833. {
  1834. int err;
  1835. struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
  1836. struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
  1837. struct bna_intr_info *intr_info =
  1838. &res_info[BNA_RX_RES_T_INTR].res_u.intr_info;
  1839. struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
  1840. static const struct bna_rx_event_cbfn rx_cbfn = {
  1841. .rcb_setup_cbfn = NULL,
  1842. .rcb_destroy_cbfn = NULL,
  1843. .ccb_setup_cbfn = bnad_cb_ccb_setup,
  1844. .ccb_destroy_cbfn = bnad_cb_ccb_destroy,
  1845. .rx_stall_cbfn = bnad_cb_rx_stall,
  1846. .rx_cleanup_cbfn = bnad_cb_rx_cleanup,
  1847. .rx_post_cbfn = bnad_cb_rx_post,
  1848. };
  1849. struct bna_rx *rx;
  1850. unsigned long flags;
  1851. rx_info->rx_id = rx_id;
  1852. /* Initialize the Rx object configuration */
  1853. bnad_init_rx_config(bnad, rx_config);
  1854. /* Get BNA's resource requirement for one Rx object */
  1855. spin_lock_irqsave(&bnad->bna_lock, flags);
  1856. bna_rx_res_req(rx_config, res_info);
  1857. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1858. /* Fill Unmap Q memory requirements */
  1859. BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_RX_RES_MEM_T_UNMAPDQ],
  1860. rx_config->num_paths,
  1861. (rx_config->q0_depth *
  1862. sizeof(struct bnad_rx_unmap)) +
  1863. sizeof(struct bnad_rx_unmap_q));
  1864. if (rx_config->rxp_type != BNA_RXP_SINGLE) {
  1865. BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_RX_RES_MEM_T_UNMAPHQ],
  1866. rx_config->num_paths,
  1867. (rx_config->q1_depth *
  1868. sizeof(struct bnad_rx_unmap) +
  1869. sizeof(struct bnad_rx_unmap_q)));
  1870. }
  1871. /* Allocate resource */
  1872. err = bnad_rx_res_alloc(bnad, res_info, rx_id);
  1873. if (err)
  1874. return err;
  1875. bnad_rx_ctrl_init(bnad, rx_id);
  1876. /* Ask BNA to create one Rx object, supplying required resources */
  1877. spin_lock_irqsave(&bnad->bna_lock, flags);
  1878. rx = bna_rx_create(&bnad->bna, bnad, rx_config, &rx_cbfn, res_info,
  1879. rx_info);
  1880. if (!rx) {
  1881. err = -ENOMEM;
  1882. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1883. goto err_return;
  1884. }
  1885. rx_info->rx = rx;
  1886. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1887. INIT_WORK(&rx_info->rx_cleanup_work,
  1888. (work_func_t)(bnad_rx_cleanup));
  1889. /*
  1890. * Init NAPI, so that state is set to NAPI_STATE_SCHED,
  1891. * so that IRQ handler cannot schedule NAPI at this point.
  1892. */
  1893. bnad_napi_add(bnad, rx_id);
  1894. /* Register ISR for the Rx object */
  1895. if (intr_info->intr_type == BNA_INTR_T_MSIX) {
  1896. err = bnad_rx_msix_register(bnad, rx_info, rx_id,
  1897. rx_config->num_paths);
  1898. if (err)
  1899. goto err_return;
  1900. }
  1901. spin_lock_irqsave(&bnad->bna_lock, flags);
  1902. if (0 == rx_id) {
  1903. /* Set up Dynamic Interrupt Moderation Vector */
  1904. if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED)
  1905. bna_rx_dim_reconfig(&bnad->bna, bna_napi_dim_vector);
  1906. /* Enable VLAN filtering only on the default Rx */
  1907. bna_rx_vlanfilter_enable(rx);
  1908. /* Start the DIM timer */
  1909. bnad_dim_timer_start(bnad);
  1910. }
  1911. bna_rx_enable(rx);
  1912. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1913. return 0;
  1914. err_return:
  1915. bnad_destroy_rx(bnad, rx_id);
  1916. return err;
  1917. }
  1918. /* Called with conf_lock & bnad->bna_lock held */
  1919. void
  1920. bnad_tx_coalescing_timeo_set(struct bnad *bnad)
  1921. {
  1922. struct bnad_tx_info *tx_info;
  1923. tx_info = &bnad->tx_info[0];
  1924. if (!tx_info->tx)
  1925. return;
  1926. bna_tx_coalescing_timeo_set(tx_info->tx, bnad->tx_coalescing_timeo);
  1927. }
  1928. /* Called with conf_lock & bnad->bna_lock held */
  1929. void
  1930. bnad_rx_coalescing_timeo_set(struct bnad *bnad)
  1931. {
  1932. struct bnad_rx_info *rx_info;
  1933. int i;
  1934. for (i = 0; i < bnad->num_rx; i++) {
  1935. rx_info = &bnad->rx_info[i];
  1936. if (!rx_info->rx)
  1937. continue;
  1938. bna_rx_coalescing_timeo_set(rx_info->rx,
  1939. bnad->rx_coalescing_timeo);
  1940. }
  1941. }
  1942. /*
  1943. * Called with bnad->bna_lock held
  1944. */
  1945. int
  1946. bnad_mac_addr_set_locked(struct bnad *bnad, const u8 *mac_addr)
  1947. {
  1948. int ret;
  1949. if (!is_valid_ether_addr(mac_addr))
  1950. return -EADDRNOTAVAIL;
  1951. /* If datapath is down, pretend everything went through */
  1952. if (!bnad->rx_info[0].rx)
  1953. return 0;
  1954. ret = bna_rx_ucast_set(bnad->rx_info[0].rx, mac_addr);
  1955. if (ret != BNA_CB_SUCCESS)
  1956. return -EADDRNOTAVAIL;
  1957. return 0;
  1958. }
  1959. /* Should be called with conf_lock held */
  1960. int
  1961. bnad_enable_default_bcast(struct bnad *bnad)
  1962. {
  1963. struct bnad_rx_info *rx_info = &bnad->rx_info[0];
  1964. int ret;
  1965. unsigned long flags;
  1966. init_completion(&bnad->bnad_completions.mcast_comp);
  1967. spin_lock_irqsave(&bnad->bna_lock, flags);
  1968. ret = bna_rx_mcast_add(rx_info->rx, bnad_bcast_addr,
  1969. bnad_cb_rx_mcast_add);
  1970. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1971. if (ret == BNA_CB_SUCCESS)
  1972. wait_for_completion(&bnad->bnad_completions.mcast_comp);
  1973. else
  1974. return -ENODEV;
  1975. if (bnad->bnad_completions.mcast_comp_status != BNA_CB_SUCCESS)
  1976. return -ENODEV;
  1977. return 0;
  1978. }
  1979. /* Called with mutex_lock(&bnad->conf_mutex) held */
  1980. void
  1981. bnad_restore_vlans(struct bnad *bnad, u32 rx_id)
  1982. {
  1983. u16 vid;
  1984. unsigned long flags;
  1985. for_each_set_bit(vid, bnad->active_vlans, VLAN_N_VID) {
  1986. spin_lock_irqsave(&bnad->bna_lock, flags);
  1987. bna_rx_vlan_add(bnad->rx_info[rx_id].rx, vid);
  1988. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1989. }
  1990. }
  1991. /* Statistics utilities */
  1992. void
  1993. bnad_netdev_qstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
  1994. {
  1995. int i, j;
  1996. for (i = 0; i < bnad->num_rx; i++) {
  1997. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  1998. if (bnad->rx_info[i].rx_ctrl[j].ccb) {
  1999. stats->rx_packets += bnad->rx_info[i].
  2000. rx_ctrl[j].ccb->rcb[0]->rxq->rx_packets;
  2001. stats->rx_bytes += bnad->rx_info[i].
  2002. rx_ctrl[j].ccb->rcb[0]->rxq->rx_bytes;
  2003. if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
  2004. bnad->rx_info[i].rx_ctrl[j].ccb->
  2005. rcb[1]->rxq) {
  2006. stats->rx_packets +=
  2007. bnad->rx_info[i].rx_ctrl[j].
  2008. ccb->rcb[1]->rxq->rx_packets;
  2009. stats->rx_bytes +=
  2010. bnad->rx_info[i].rx_ctrl[j].
  2011. ccb->rcb[1]->rxq->rx_bytes;
  2012. }
  2013. }
  2014. }
  2015. }
  2016. for (i = 0; i < bnad->num_tx; i++) {
  2017. for (j = 0; j < bnad->num_txq_per_tx; j++) {
  2018. if (bnad->tx_info[i].tcb[j]) {
  2019. stats->tx_packets +=
  2020. bnad->tx_info[i].tcb[j]->txq->tx_packets;
  2021. stats->tx_bytes +=
  2022. bnad->tx_info[i].tcb[j]->txq->tx_bytes;
  2023. }
  2024. }
  2025. }
  2026. }
  2027. /*
  2028. * Must be called with the bna_lock held.
  2029. */
  2030. void
  2031. bnad_netdev_hwstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
  2032. {
  2033. struct bfi_enet_stats_mac *mac_stats;
  2034. u32 bmap;
  2035. int i;
  2036. mac_stats = &bnad->stats.bna_stats->hw_stats.mac_stats;
  2037. stats->rx_errors =
  2038. mac_stats->rx_fcs_error + mac_stats->rx_alignment_error +
  2039. mac_stats->rx_frame_length_error + mac_stats->rx_code_error +
  2040. mac_stats->rx_undersize;
  2041. stats->tx_errors = mac_stats->tx_fcs_error +
  2042. mac_stats->tx_undersize;
  2043. stats->rx_dropped = mac_stats->rx_drop;
  2044. stats->tx_dropped = mac_stats->tx_drop;
  2045. stats->multicast = mac_stats->rx_multicast;
  2046. stats->collisions = mac_stats->tx_total_collision;
  2047. stats->rx_length_errors = mac_stats->rx_frame_length_error;
  2048. /* receive ring buffer overflow ?? */
  2049. stats->rx_crc_errors = mac_stats->rx_fcs_error;
  2050. stats->rx_frame_errors = mac_stats->rx_alignment_error;
  2051. /* recv'r fifo overrun */
  2052. bmap = bna_rx_rid_mask(&bnad->bna);
  2053. for (i = 0; bmap; i++) {
  2054. if (bmap & 1) {
  2055. stats->rx_fifo_errors +=
  2056. bnad->stats.bna_stats->
  2057. hw_stats.rxf_stats[i].frame_drops;
  2058. break;
  2059. }
  2060. bmap >>= 1;
  2061. }
  2062. }
  2063. static void
  2064. bnad_mbox_irq_sync(struct bnad *bnad)
  2065. {
  2066. u32 irq;
  2067. unsigned long flags;
  2068. spin_lock_irqsave(&bnad->bna_lock, flags);
  2069. if (bnad->cfg_flags & BNAD_CF_MSIX)
  2070. irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
  2071. else
  2072. irq = bnad->pcidev->irq;
  2073. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2074. synchronize_irq(irq);
  2075. }
  2076. /* Utility used by bnad_start_xmit, for doing TSO */
  2077. static int
  2078. bnad_tso_prepare(struct bnad *bnad, struct sk_buff *skb)
  2079. {
  2080. int err;
  2081. err = skb_cow_head(skb, 0);
  2082. if (err < 0) {
  2083. BNAD_UPDATE_CTR(bnad, tso_err);
  2084. return err;
  2085. }
  2086. /*
  2087. * For TSO, the TCP checksum field is seeded with pseudo-header sum
  2088. * excluding the length field.
  2089. */
  2090. if (vlan_get_protocol(skb) == htons(ETH_P_IP)) {
  2091. struct iphdr *iph = ip_hdr(skb);
  2092. /* Do we really need these? */
  2093. iph->tot_len = 0;
  2094. iph->check = 0;
  2095. tcp_hdr(skb)->check =
  2096. ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
  2097. IPPROTO_TCP, 0);
  2098. BNAD_UPDATE_CTR(bnad, tso4);
  2099. } else {
  2100. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  2101. ipv6h->payload_len = 0;
  2102. tcp_hdr(skb)->check =
  2103. ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, 0,
  2104. IPPROTO_TCP, 0);
  2105. BNAD_UPDATE_CTR(bnad, tso6);
  2106. }
  2107. return 0;
  2108. }
  2109. /*
  2110. * Initialize Q numbers depending on Rx Paths
  2111. * Called with bnad->bna_lock held, because of cfg_flags
  2112. * access.
  2113. */
  2114. static void
  2115. bnad_q_num_init(struct bnad *bnad)
  2116. {
  2117. int rxps;
  2118. rxps = min((uint)num_online_cpus(),
  2119. (uint)(BNAD_MAX_RX * BNAD_MAX_RXP_PER_RX));
  2120. if (!(bnad->cfg_flags & BNAD_CF_MSIX))
  2121. rxps = 1; /* INTx */
  2122. bnad->num_rx = 1;
  2123. bnad->num_tx = 1;
  2124. bnad->num_rxp_per_rx = rxps;
  2125. bnad->num_txq_per_tx = BNAD_TXQ_NUM;
  2126. }
  2127. /*
  2128. * Adjusts the Q numbers, given a number of msix vectors
  2129. * Give preference to RSS as opposed to Tx priority Queues,
  2130. * in such a case, just use 1 Tx Q
  2131. * Called with bnad->bna_lock held b'cos of cfg_flags access
  2132. */
  2133. static void
  2134. bnad_q_num_adjust(struct bnad *bnad, int msix_vectors, int temp)
  2135. {
  2136. bnad->num_txq_per_tx = 1;
  2137. if ((msix_vectors >= (bnad->num_tx * bnad->num_txq_per_tx) +
  2138. bnad_rxqs_per_cq + BNAD_MAILBOX_MSIX_VECTORS) &&
  2139. (bnad->cfg_flags & BNAD_CF_MSIX)) {
  2140. bnad->num_rxp_per_rx = msix_vectors -
  2141. (bnad->num_tx * bnad->num_txq_per_tx) -
  2142. BNAD_MAILBOX_MSIX_VECTORS;
  2143. } else
  2144. bnad->num_rxp_per_rx = 1;
  2145. }
  2146. /* Enable / disable ioceth */
  2147. static int
  2148. bnad_ioceth_disable(struct bnad *bnad)
  2149. {
  2150. unsigned long flags;
  2151. int err = 0;
  2152. spin_lock_irqsave(&bnad->bna_lock, flags);
  2153. init_completion(&bnad->bnad_completions.ioc_comp);
  2154. bna_ioceth_disable(&bnad->bna.ioceth, BNA_HARD_CLEANUP);
  2155. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2156. wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
  2157. msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
  2158. err = bnad->bnad_completions.ioc_comp_status;
  2159. return err;
  2160. }
  2161. static int
  2162. bnad_ioceth_enable(struct bnad *bnad)
  2163. {
  2164. int err = 0;
  2165. unsigned long flags;
  2166. spin_lock_irqsave(&bnad->bna_lock, flags);
  2167. init_completion(&bnad->bnad_completions.ioc_comp);
  2168. bnad->bnad_completions.ioc_comp_status = BNA_CB_WAITING;
  2169. bna_ioceth_enable(&bnad->bna.ioceth);
  2170. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2171. wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
  2172. msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
  2173. err = bnad->bnad_completions.ioc_comp_status;
  2174. return err;
  2175. }
  2176. /* Free BNA resources */
  2177. static void
  2178. bnad_res_free(struct bnad *bnad, struct bna_res_info *res_info,
  2179. u32 res_val_max)
  2180. {
  2181. int i;
  2182. for (i = 0; i < res_val_max; i++)
  2183. bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
  2184. }
  2185. /* Allocates memory and interrupt resources for BNA */
  2186. static int
  2187. bnad_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
  2188. u32 res_val_max)
  2189. {
  2190. int i, err;
  2191. for (i = 0; i < res_val_max; i++) {
  2192. err = bnad_mem_alloc(bnad, &res_info[i].res_u.mem_info);
  2193. if (err)
  2194. goto err_return;
  2195. }
  2196. return 0;
  2197. err_return:
  2198. bnad_res_free(bnad, res_info, res_val_max);
  2199. return err;
  2200. }
  2201. /* Interrupt enable / disable */
  2202. static void
  2203. bnad_enable_msix(struct bnad *bnad)
  2204. {
  2205. int i, ret;
  2206. unsigned long flags;
  2207. spin_lock_irqsave(&bnad->bna_lock, flags);
  2208. if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
  2209. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2210. return;
  2211. }
  2212. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2213. if (bnad->msix_table)
  2214. return;
  2215. bnad->msix_table =
  2216. kcalloc(bnad->msix_num, sizeof(struct msix_entry), GFP_KERNEL);
  2217. if (!bnad->msix_table)
  2218. goto intx_mode;
  2219. for (i = 0; i < bnad->msix_num; i++)
  2220. bnad->msix_table[i].entry = i;
  2221. ret = pci_enable_msix_range(bnad->pcidev, bnad->msix_table,
  2222. 1, bnad->msix_num);
  2223. if (ret < 0) {
  2224. goto intx_mode;
  2225. } else if (ret < bnad->msix_num) {
  2226. dev_warn(&bnad->pcidev->dev,
  2227. "%d MSI-X vectors allocated < %d requested\n",
  2228. ret, bnad->msix_num);
  2229. spin_lock_irqsave(&bnad->bna_lock, flags);
  2230. /* ret = #of vectors that we got */
  2231. bnad_q_num_adjust(bnad, (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2,
  2232. (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2);
  2233. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2234. bnad->msix_num = BNAD_NUM_TXQ + BNAD_NUM_RXP +
  2235. BNAD_MAILBOX_MSIX_VECTORS;
  2236. if (bnad->msix_num > ret) {
  2237. pci_disable_msix(bnad->pcidev);
  2238. goto intx_mode;
  2239. }
  2240. }
  2241. pci_intx(bnad->pcidev, 0);
  2242. return;
  2243. intx_mode:
  2244. dev_warn(&bnad->pcidev->dev,
  2245. "MSI-X enable failed - operating in INTx mode\n");
  2246. kfree(bnad->msix_table);
  2247. bnad->msix_table = NULL;
  2248. bnad->msix_num = 0;
  2249. spin_lock_irqsave(&bnad->bna_lock, flags);
  2250. bnad->cfg_flags &= ~BNAD_CF_MSIX;
  2251. bnad_q_num_init(bnad);
  2252. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2253. }
  2254. static void
  2255. bnad_disable_msix(struct bnad *bnad)
  2256. {
  2257. u32 cfg_flags;
  2258. unsigned long flags;
  2259. spin_lock_irqsave(&bnad->bna_lock, flags);
  2260. cfg_flags = bnad->cfg_flags;
  2261. if (bnad->cfg_flags & BNAD_CF_MSIX)
  2262. bnad->cfg_flags &= ~BNAD_CF_MSIX;
  2263. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2264. if (cfg_flags & BNAD_CF_MSIX) {
  2265. pci_disable_msix(bnad->pcidev);
  2266. kfree(bnad->msix_table);
  2267. bnad->msix_table = NULL;
  2268. }
  2269. }
  2270. /* Netdev entry points */
  2271. static int
  2272. bnad_open(struct net_device *netdev)
  2273. {
  2274. int err;
  2275. struct bnad *bnad = netdev_priv(netdev);
  2276. struct bna_pause_config pause_config;
  2277. unsigned long flags;
  2278. mutex_lock(&bnad->conf_mutex);
  2279. /* Tx */
  2280. err = bnad_setup_tx(bnad, 0);
  2281. if (err)
  2282. goto err_return;
  2283. /* Rx */
  2284. err = bnad_setup_rx(bnad, 0);
  2285. if (err)
  2286. goto cleanup_tx;
  2287. /* Port */
  2288. pause_config.tx_pause = 0;
  2289. pause_config.rx_pause = 0;
  2290. spin_lock_irqsave(&bnad->bna_lock, flags);
  2291. bna_enet_mtu_set(&bnad->bna.enet,
  2292. BNAD_FRAME_SIZE(bnad->netdev->mtu), NULL);
  2293. bna_enet_pause_config(&bnad->bna.enet, &pause_config);
  2294. bna_enet_enable(&bnad->bna.enet);
  2295. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2296. /* Enable broadcast */
  2297. bnad_enable_default_bcast(bnad);
  2298. /* Restore VLANs, if any */
  2299. bnad_restore_vlans(bnad, 0);
  2300. /* Set the UCAST address */
  2301. spin_lock_irqsave(&bnad->bna_lock, flags);
  2302. bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
  2303. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2304. /* Start the stats timer */
  2305. bnad_stats_timer_start(bnad);
  2306. mutex_unlock(&bnad->conf_mutex);
  2307. return 0;
  2308. cleanup_tx:
  2309. bnad_destroy_tx(bnad, 0);
  2310. err_return:
  2311. mutex_unlock(&bnad->conf_mutex);
  2312. return err;
  2313. }
  2314. static int
  2315. bnad_stop(struct net_device *netdev)
  2316. {
  2317. struct bnad *bnad = netdev_priv(netdev);
  2318. unsigned long flags;
  2319. mutex_lock(&bnad->conf_mutex);
  2320. /* Stop the stats timer */
  2321. bnad_stats_timer_stop(bnad);
  2322. init_completion(&bnad->bnad_completions.enet_comp);
  2323. spin_lock_irqsave(&bnad->bna_lock, flags);
  2324. bna_enet_disable(&bnad->bna.enet, BNA_HARD_CLEANUP,
  2325. bnad_cb_enet_disabled);
  2326. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2327. wait_for_completion(&bnad->bnad_completions.enet_comp);
  2328. bnad_destroy_tx(bnad, 0);
  2329. bnad_destroy_rx(bnad, 0);
  2330. /* Synchronize mailbox IRQ */
  2331. bnad_mbox_irq_sync(bnad);
  2332. mutex_unlock(&bnad->conf_mutex);
  2333. return 0;
  2334. }
  2335. /* TX */
  2336. /* Returns 0 for success */
  2337. static int
  2338. bnad_txq_wi_prepare(struct bnad *bnad, struct bna_tcb *tcb,
  2339. struct sk_buff *skb, struct bna_txq_entry *txqent)
  2340. {
  2341. u16 flags = 0;
  2342. u32 gso_size;
  2343. u16 vlan_tag = 0;
  2344. if (skb_vlan_tag_present(skb)) {
  2345. vlan_tag = (u16)skb_vlan_tag_get(skb);
  2346. flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
  2347. }
  2348. if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags)) {
  2349. vlan_tag = ((tcb->priority & 0x7) << VLAN_PRIO_SHIFT)
  2350. | (vlan_tag & 0x1fff);
  2351. flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
  2352. }
  2353. txqent->hdr.wi.vlan_tag = htons(vlan_tag);
  2354. if (skb_is_gso(skb)) {
  2355. gso_size = skb_shinfo(skb)->gso_size;
  2356. if (unlikely(gso_size > bnad->netdev->mtu)) {
  2357. BNAD_UPDATE_CTR(bnad, tx_skb_mss_too_long);
  2358. return -EINVAL;
  2359. }
  2360. if (unlikely((gso_size + skb_transport_offset(skb) +
  2361. tcp_hdrlen(skb)) >= skb->len)) {
  2362. txqent->hdr.wi.opcode = htons(BNA_TXQ_WI_SEND);
  2363. txqent->hdr.wi.lso_mss = 0;
  2364. BNAD_UPDATE_CTR(bnad, tx_skb_tso_too_short);
  2365. } else {
  2366. txqent->hdr.wi.opcode = htons(BNA_TXQ_WI_SEND_LSO);
  2367. txqent->hdr.wi.lso_mss = htons(gso_size);
  2368. }
  2369. if (bnad_tso_prepare(bnad, skb)) {
  2370. BNAD_UPDATE_CTR(bnad, tx_skb_tso_prepare);
  2371. return -EINVAL;
  2372. }
  2373. flags |= (BNA_TXQ_WI_CF_IP_CKSUM | BNA_TXQ_WI_CF_TCP_CKSUM);
  2374. txqent->hdr.wi.l4_hdr_size_n_offset =
  2375. htons(BNA_TXQ_WI_L4_HDR_N_OFFSET(
  2376. tcp_hdrlen(skb) >> 2, skb_transport_offset(skb)));
  2377. } else {
  2378. txqent->hdr.wi.opcode = htons(BNA_TXQ_WI_SEND);
  2379. txqent->hdr.wi.lso_mss = 0;
  2380. if (unlikely(skb->len > (bnad->netdev->mtu + VLAN_ETH_HLEN))) {
  2381. BNAD_UPDATE_CTR(bnad, tx_skb_non_tso_too_long);
  2382. return -EINVAL;
  2383. }
  2384. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2385. __be16 net_proto = vlan_get_protocol(skb);
  2386. u8 proto = 0;
  2387. if (net_proto == htons(ETH_P_IP))
  2388. proto = ip_hdr(skb)->protocol;
  2389. #ifdef NETIF_F_IPV6_CSUM
  2390. else if (net_proto == htons(ETH_P_IPV6)) {
  2391. /* nexthdr may not be TCP immediately. */
  2392. proto = ipv6_hdr(skb)->nexthdr;
  2393. }
  2394. #endif
  2395. if (proto == IPPROTO_TCP) {
  2396. flags |= BNA_TXQ_WI_CF_TCP_CKSUM;
  2397. txqent->hdr.wi.l4_hdr_size_n_offset =
  2398. htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
  2399. (0, skb_transport_offset(skb)));
  2400. BNAD_UPDATE_CTR(bnad, tcpcsum_offload);
  2401. if (unlikely(skb_headlen(skb) <
  2402. skb_transport_offset(skb) +
  2403. tcp_hdrlen(skb))) {
  2404. BNAD_UPDATE_CTR(bnad, tx_skb_tcp_hdr);
  2405. return -EINVAL;
  2406. }
  2407. } else if (proto == IPPROTO_UDP) {
  2408. flags |= BNA_TXQ_WI_CF_UDP_CKSUM;
  2409. txqent->hdr.wi.l4_hdr_size_n_offset =
  2410. htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
  2411. (0, skb_transport_offset(skb)));
  2412. BNAD_UPDATE_CTR(bnad, udpcsum_offload);
  2413. if (unlikely(skb_headlen(skb) <
  2414. skb_transport_offset(skb) +
  2415. sizeof(struct udphdr))) {
  2416. BNAD_UPDATE_CTR(bnad, tx_skb_udp_hdr);
  2417. return -EINVAL;
  2418. }
  2419. } else {
  2420. BNAD_UPDATE_CTR(bnad, tx_skb_csum_err);
  2421. return -EINVAL;
  2422. }
  2423. } else
  2424. txqent->hdr.wi.l4_hdr_size_n_offset = 0;
  2425. }
  2426. txqent->hdr.wi.flags = htons(flags);
  2427. txqent->hdr.wi.frame_length = htonl(skb->len);
  2428. return 0;
  2429. }
  2430. /*
  2431. * bnad_start_xmit : Netdev entry point for Transmit
  2432. * Called under lock held by net_device
  2433. */
  2434. static netdev_tx_t
  2435. bnad_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  2436. {
  2437. struct bnad *bnad = netdev_priv(netdev);
  2438. u32 txq_id = 0;
  2439. struct bna_tcb *tcb = NULL;
  2440. struct bnad_tx_unmap *unmap_q, *unmap, *head_unmap;
  2441. u32 prod, q_depth, vect_id;
  2442. u32 wis, vectors, len;
  2443. int i;
  2444. dma_addr_t dma_addr;
  2445. struct bna_txq_entry *txqent;
  2446. len = skb_headlen(skb);
  2447. /* Sanity checks for the skb */
  2448. if (unlikely(skb->len <= ETH_HLEN)) {
  2449. dev_kfree_skb_any(skb);
  2450. BNAD_UPDATE_CTR(bnad, tx_skb_too_short);
  2451. return NETDEV_TX_OK;
  2452. }
  2453. if (unlikely(len > BFI_TX_MAX_DATA_PER_VECTOR)) {
  2454. dev_kfree_skb_any(skb);
  2455. BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
  2456. return NETDEV_TX_OK;
  2457. }
  2458. if (unlikely(len == 0)) {
  2459. dev_kfree_skb_any(skb);
  2460. BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
  2461. return NETDEV_TX_OK;
  2462. }
  2463. tcb = bnad->tx_info[0].tcb[txq_id];
  2464. /*
  2465. * Takes care of the Tx that is scheduled between clearing the flag
  2466. * and the netif_tx_stop_all_queues() call.
  2467. */
  2468. if (unlikely(!tcb || !test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))) {
  2469. dev_kfree_skb_any(skb);
  2470. BNAD_UPDATE_CTR(bnad, tx_skb_stopping);
  2471. return NETDEV_TX_OK;
  2472. }
  2473. q_depth = tcb->q_depth;
  2474. prod = tcb->producer_index;
  2475. unmap_q = tcb->unmap_q;
  2476. vectors = 1 + skb_shinfo(skb)->nr_frags;
  2477. wis = BNA_TXQ_WI_NEEDED(vectors); /* 4 vectors per work item */
  2478. if (unlikely(vectors > BFI_TX_MAX_VECTORS_PER_PKT)) {
  2479. dev_kfree_skb_any(skb);
  2480. BNAD_UPDATE_CTR(bnad, tx_skb_max_vectors);
  2481. return NETDEV_TX_OK;
  2482. }
  2483. /* Check for available TxQ resources */
  2484. if (unlikely(wis > BNA_QE_FREE_CNT(tcb, q_depth))) {
  2485. if ((*tcb->hw_consumer_index != tcb->consumer_index) &&
  2486. !test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
  2487. u32 sent;
  2488. sent = bnad_txcmpl_process(bnad, tcb);
  2489. if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
  2490. bna_ib_ack(tcb->i_dbell, sent);
  2491. smp_mb__before_atomic();
  2492. clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
  2493. } else {
  2494. netif_stop_queue(netdev);
  2495. BNAD_UPDATE_CTR(bnad, netif_queue_stop);
  2496. }
  2497. smp_mb();
  2498. /*
  2499. * Check again to deal with race condition between
  2500. * netif_stop_queue here, and netif_wake_queue in
  2501. * interrupt handler which is not inside netif tx lock.
  2502. */
  2503. if (likely(wis > BNA_QE_FREE_CNT(tcb, q_depth))) {
  2504. BNAD_UPDATE_CTR(bnad, netif_queue_stop);
  2505. return NETDEV_TX_BUSY;
  2506. } else {
  2507. netif_wake_queue(netdev);
  2508. BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
  2509. }
  2510. }
  2511. txqent = &((struct bna_txq_entry *)tcb->sw_q)[prod];
  2512. head_unmap = &unmap_q[prod];
  2513. /* Program the opcode, flags, frame_len, num_vectors in WI */
  2514. if (bnad_txq_wi_prepare(bnad, tcb, skb, txqent)) {
  2515. dev_kfree_skb_any(skb);
  2516. return NETDEV_TX_OK;
  2517. }
  2518. txqent->hdr.wi.reserved = 0;
  2519. txqent->hdr.wi.num_vectors = vectors;
  2520. head_unmap->skb = skb;
  2521. head_unmap->nvecs = 0;
  2522. /* Program the vectors */
  2523. unmap = head_unmap;
  2524. dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
  2525. len, DMA_TO_DEVICE);
  2526. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  2527. dev_kfree_skb_any(skb);
  2528. BNAD_UPDATE_CTR(bnad, tx_skb_map_failed);
  2529. return NETDEV_TX_OK;
  2530. }
  2531. BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[0].host_addr);
  2532. txqent->vector[0].length = htons(len);
  2533. dma_unmap_addr_set(&unmap->vectors[0], dma_addr, dma_addr);
  2534. head_unmap->nvecs++;
  2535. for (i = 0, vect_id = 0; i < vectors - 1; i++) {
  2536. const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
  2537. u32 size = skb_frag_size(frag);
  2538. if (unlikely(size == 0)) {
  2539. /* Undo the changes starting at tcb->producer_index */
  2540. bnad_tx_buff_unmap(bnad, unmap_q, q_depth,
  2541. tcb->producer_index);
  2542. dev_kfree_skb_any(skb);
  2543. BNAD_UPDATE_CTR(bnad, tx_skb_frag_zero);
  2544. return NETDEV_TX_OK;
  2545. }
  2546. len += size;
  2547. vect_id++;
  2548. if (vect_id == BFI_TX_MAX_VECTORS_PER_WI) {
  2549. vect_id = 0;
  2550. BNA_QE_INDX_INC(prod, q_depth);
  2551. txqent = &((struct bna_txq_entry *)tcb->sw_q)[prod];
  2552. txqent->hdr.wi_ext.opcode = htons(BNA_TXQ_WI_EXTENSION);
  2553. unmap = &unmap_q[prod];
  2554. }
  2555. dma_addr = skb_frag_dma_map(&bnad->pcidev->dev, frag,
  2556. 0, size, DMA_TO_DEVICE);
  2557. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  2558. /* Undo the changes starting at tcb->producer_index */
  2559. bnad_tx_buff_unmap(bnad, unmap_q, q_depth,
  2560. tcb->producer_index);
  2561. dev_kfree_skb_any(skb);
  2562. BNAD_UPDATE_CTR(bnad, tx_skb_map_failed);
  2563. return NETDEV_TX_OK;
  2564. }
  2565. dma_unmap_len_set(&unmap->vectors[vect_id], dma_len, size);
  2566. BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[vect_id].host_addr);
  2567. txqent->vector[vect_id].length = htons(size);
  2568. dma_unmap_addr_set(&unmap->vectors[vect_id], dma_addr,
  2569. dma_addr);
  2570. head_unmap->nvecs++;
  2571. }
  2572. if (unlikely(len != skb->len)) {
  2573. /* Undo the changes starting at tcb->producer_index */
  2574. bnad_tx_buff_unmap(bnad, unmap_q, q_depth, tcb->producer_index);
  2575. dev_kfree_skb_any(skb);
  2576. BNAD_UPDATE_CTR(bnad, tx_skb_len_mismatch);
  2577. return NETDEV_TX_OK;
  2578. }
  2579. BNA_QE_INDX_INC(prod, q_depth);
  2580. tcb->producer_index = prod;
  2581. wmb();
  2582. if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
  2583. return NETDEV_TX_OK;
  2584. skb_tx_timestamp(skb);
  2585. bna_txq_prod_indx_doorbell(tcb);
  2586. return NETDEV_TX_OK;
  2587. }
  2588. /*
  2589. * Used spin_lock to synchronize reading of stats structures, which
  2590. * is written by BNA under the same lock.
  2591. */
  2592. static void
  2593. bnad_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
  2594. {
  2595. struct bnad *bnad = netdev_priv(netdev);
  2596. unsigned long flags;
  2597. spin_lock_irqsave(&bnad->bna_lock, flags);
  2598. bnad_netdev_qstats_fill(bnad, stats);
  2599. bnad_netdev_hwstats_fill(bnad, stats);
  2600. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2601. }
  2602. static void
  2603. bnad_set_rx_ucast_fltr(struct bnad *bnad)
  2604. {
  2605. struct net_device *netdev = bnad->netdev;
  2606. int uc_count = netdev_uc_count(netdev);
  2607. enum bna_cb_status ret;
  2608. u8 *mac_list;
  2609. struct netdev_hw_addr *ha;
  2610. int entry;
  2611. if (netdev_uc_empty(bnad->netdev)) {
  2612. bna_rx_ucast_listset(bnad->rx_info[0].rx, 0, NULL);
  2613. return;
  2614. }
  2615. if (uc_count > bna_attr(&bnad->bna)->num_ucmac)
  2616. goto mode_default;
  2617. mac_list = kcalloc(ETH_ALEN, uc_count, GFP_ATOMIC);
  2618. if (mac_list == NULL)
  2619. goto mode_default;
  2620. entry = 0;
  2621. netdev_for_each_uc_addr(ha, netdev) {
  2622. ether_addr_copy(&mac_list[entry * ETH_ALEN], &ha->addr[0]);
  2623. entry++;
  2624. }
  2625. ret = bna_rx_ucast_listset(bnad->rx_info[0].rx, entry, mac_list);
  2626. kfree(mac_list);
  2627. if (ret != BNA_CB_SUCCESS)
  2628. goto mode_default;
  2629. return;
  2630. /* ucast packets not in UCAM are routed to default function */
  2631. mode_default:
  2632. bnad->cfg_flags |= BNAD_CF_DEFAULT;
  2633. bna_rx_ucast_listset(bnad->rx_info[0].rx, 0, NULL);
  2634. }
  2635. static void
  2636. bnad_set_rx_mcast_fltr(struct bnad *bnad)
  2637. {
  2638. struct net_device *netdev = bnad->netdev;
  2639. int mc_count = netdev_mc_count(netdev);
  2640. enum bna_cb_status ret;
  2641. u8 *mac_list;
  2642. if (netdev->flags & IFF_ALLMULTI)
  2643. goto mode_allmulti;
  2644. if (netdev_mc_empty(netdev))
  2645. return;
  2646. if (mc_count > bna_attr(&bnad->bna)->num_mcmac)
  2647. goto mode_allmulti;
  2648. mac_list = kcalloc(mc_count + 1, ETH_ALEN, GFP_ATOMIC);
  2649. if (mac_list == NULL)
  2650. goto mode_allmulti;
  2651. ether_addr_copy(&mac_list[0], &bnad_bcast_addr[0]);
  2652. /* copy rest of the MCAST addresses */
  2653. bnad_netdev_mc_list_get(netdev, mac_list);
  2654. ret = bna_rx_mcast_listset(bnad->rx_info[0].rx, mc_count + 1, mac_list);
  2655. kfree(mac_list);
  2656. if (ret != BNA_CB_SUCCESS)
  2657. goto mode_allmulti;
  2658. return;
  2659. mode_allmulti:
  2660. bnad->cfg_flags |= BNAD_CF_ALLMULTI;
  2661. bna_rx_mcast_delall(bnad->rx_info[0].rx);
  2662. }
  2663. void
  2664. bnad_set_rx_mode(struct net_device *netdev)
  2665. {
  2666. struct bnad *bnad = netdev_priv(netdev);
  2667. enum bna_rxmode new_mode, mode_mask;
  2668. unsigned long flags;
  2669. spin_lock_irqsave(&bnad->bna_lock, flags);
  2670. if (bnad->rx_info[0].rx == NULL) {
  2671. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2672. return;
  2673. }
  2674. /* clear bnad flags to update it with new settings */
  2675. bnad->cfg_flags &= ~(BNAD_CF_PROMISC | BNAD_CF_DEFAULT |
  2676. BNAD_CF_ALLMULTI);
  2677. new_mode = 0;
  2678. if (netdev->flags & IFF_PROMISC) {
  2679. new_mode |= BNAD_RXMODE_PROMISC_DEFAULT;
  2680. bnad->cfg_flags |= BNAD_CF_PROMISC;
  2681. } else {
  2682. bnad_set_rx_mcast_fltr(bnad);
  2683. if (bnad->cfg_flags & BNAD_CF_ALLMULTI)
  2684. new_mode |= BNA_RXMODE_ALLMULTI;
  2685. bnad_set_rx_ucast_fltr(bnad);
  2686. if (bnad->cfg_flags & BNAD_CF_DEFAULT)
  2687. new_mode |= BNA_RXMODE_DEFAULT;
  2688. }
  2689. mode_mask = BNA_RXMODE_PROMISC | BNA_RXMODE_DEFAULT |
  2690. BNA_RXMODE_ALLMULTI;
  2691. bna_rx_mode_set(bnad->rx_info[0].rx, new_mode, mode_mask);
  2692. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2693. }
  2694. /*
  2695. * bna_lock is used to sync writes to netdev->addr
  2696. * conf_lock cannot be used since this call may be made
  2697. * in a non-blocking context.
  2698. */
  2699. static int
  2700. bnad_set_mac_address(struct net_device *netdev, void *addr)
  2701. {
  2702. int err;
  2703. struct bnad *bnad = netdev_priv(netdev);
  2704. struct sockaddr *sa = (struct sockaddr *)addr;
  2705. unsigned long flags;
  2706. spin_lock_irqsave(&bnad->bna_lock, flags);
  2707. err = bnad_mac_addr_set_locked(bnad, sa->sa_data);
  2708. if (!err)
  2709. ether_addr_copy(netdev->dev_addr, sa->sa_data);
  2710. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2711. return err;
  2712. }
  2713. static int
  2714. bnad_mtu_set(struct bnad *bnad, int frame_size)
  2715. {
  2716. unsigned long flags;
  2717. init_completion(&bnad->bnad_completions.mtu_comp);
  2718. spin_lock_irqsave(&bnad->bna_lock, flags);
  2719. bna_enet_mtu_set(&bnad->bna.enet, frame_size, bnad_cb_enet_mtu_set);
  2720. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2721. wait_for_completion(&bnad->bnad_completions.mtu_comp);
  2722. return bnad->bnad_completions.mtu_comp_status;
  2723. }
  2724. static int
  2725. bnad_change_mtu(struct net_device *netdev, int new_mtu)
  2726. {
  2727. int err, mtu;
  2728. struct bnad *bnad = netdev_priv(netdev);
  2729. u32 rx_count = 0, frame, new_frame;
  2730. mutex_lock(&bnad->conf_mutex);
  2731. mtu = netdev->mtu;
  2732. netdev->mtu = new_mtu;
  2733. frame = BNAD_FRAME_SIZE(mtu);
  2734. new_frame = BNAD_FRAME_SIZE(new_mtu);
  2735. /* check if multi-buffer needs to be enabled */
  2736. if (BNAD_PCI_DEV_IS_CAT2(bnad) &&
  2737. netif_running(bnad->netdev)) {
  2738. /* only when transition is over 4K */
  2739. if ((frame <= 4096 && new_frame > 4096) ||
  2740. (frame > 4096 && new_frame <= 4096))
  2741. rx_count = bnad_reinit_rx(bnad);
  2742. }
  2743. /* rx_count > 0 - new rx created
  2744. * - Linux set err = 0 and return
  2745. */
  2746. err = bnad_mtu_set(bnad, new_frame);
  2747. if (err)
  2748. err = -EBUSY;
  2749. mutex_unlock(&bnad->conf_mutex);
  2750. return err;
  2751. }
  2752. static int
  2753. bnad_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
  2754. {
  2755. struct bnad *bnad = netdev_priv(netdev);
  2756. unsigned long flags;
  2757. if (!bnad->rx_info[0].rx)
  2758. return 0;
  2759. mutex_lock(&bnad->conf_mutex);
  2760. spin_lock_irqsave(&bnad->bna_lock, flags);
  2761. bna_rx_vlan_add(bnad->rx_info[0].rx, vid);
  2762. set_bit(vid, bnad->active_vlans);
  2763. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2764. mutex_unlock(&bnad->conf_mutex);
  2765. return 0;
  2766. }
  2767. static int
  2768. bnad_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
  2769. {
  2770. struct bnad *bnad = netdev_priv(netdev);
  2771. unsigned long flags;
  2772. if (!bnad->rx_info[0].rx)
  2773. return 0;
  2774. mutex_lock(&bnad->conf_mutex);
  2775. spin_lock_irqsave(&bnad->bna_lock, flags);
  2776. clear_bit(vid, bnad->active_vlans);
  2777. bna_rx_vlan_del(bnad->rx_info[0].rx, vid);
  2778. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2779. mutex_unlock(&bnad->conf_mutex);
  2780. return 0;
  2781. }
  2782. static int bnad_set_features(struct net_device *dev, netdev_features_t features)
  2783. {
  2784. struct bnad *bnad = netdev_priv(dev);
  2785. netdev_features_t changed = features ^ dev->features;
  2786. if ((changed & NETIF_F_HW_VLAN_CTAG_RX) && netif_running(dev)) {
  2787. unsigned long flags;
  2788. spin_lock_irqsave(&bnad->bna_lock, flags);
  2789. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2790. bna_rx_vlan_strip_enable(bnad->rx_info[0].rx);
  2791. else
  2792. bna_rx_vlan_strip_disable(bnad->rx_info[0].rx);
  2793. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2794. }
  2795. return 0;
  2796. }
  2797. #ifdef CONFIG_NET_POLL_CONTROLLER
  2798. static void
  2799. bnad_netpoll(struct net_device *netdev)
  2800. {
  2801. struct bnad *bnad = netdev_priv(netdev);
  2802. struct bnad_rx_info *rx_info;
  2803. struct bnad_rx_ctrl *rx_ctrl;
  2804. u32 curr_mask;
  2805. int i, j;
  2806. if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
  2807. bna_intx_disable(&bnad->bna, curr_mask);
  2808. bnad_isr(bnad->pcidev->irq, netdev);
  2809. bna_intx_enable(&bnad->bna, curr_mask);
  2810. } else {
  2811. /*
  2812. * Tx processing may happen in sending context, so no need
  2813. * to explicitly process completions here
  2814. */
  2815. /* Rx processing */
  2816. for (i = 0; i < bnad->num_rx; i++) {
  2817. rx_info = &bnad->rx_info[i];
  2818. if (!rx_info->rx)
  2819. continue;
  2820. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  2821. rx_ctrl = &rx_info->rx_ctrl[j];
  2822. if (rx_ctrl->ccb)
  2823. bnad_netif_rx_schedule_poll(bnad,
  2824. rx_ctrl->ccb);
  2825. }
  2826. }
  2827. }
  2828. }
  2829. #endif
  2830. static const struct net_device_ops bnad_netdev_ops = {
  2831. .ndo_open = bnad_open,
  2832. .ndo_stop = bnad_stop,
  2833. .ndo_start_xmit = bnad_start_xmit,
  2834. .ndo_get_stats64 = bnad_get_stats64,
  2835. .ndo_set_rx_mode = bnad_set_rx_mode,
  2836. .ndo_validate_addr = eth_validate_addr,
  2837. .ndo_set_mac_address = bnad_set_mac_address,
  2838. .ndo_change_mtu = bnad_change_mtu,
  2839. .ndo_vlan_rx_add_vid = bnad_vlan_rx_add_vid,
  2840. .ndo_vlan_rx_kill_vid = bnad_vlan_rx_kill_vid,
  2841. .ndo_set_features = bnad_set_features,
  2842. #ifdef CONFIG_NET_POLL_CONTROLLER
  2843. .ndo_poll_controller = bnad_netpoll
  2844. #endif
  2845. };
  2846. static void
  2847. bnad_netdev_init(struct bnad *bnad, bool using_dac)
  2848. {
  2849. struct net_device *netdev = bnad->netdev;
  2850. netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
  2851. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2852. NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_TX |
  2853. NETIF_F_HW_VLAN_CTAG_RX;
  2854. netdev->vlan_features = NETIF_F_SG | NETIF_F_HIGHDMA |
  2855. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2856. NETIF_F_TSO | NETIF_F_TSO6;
  2857. netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  2858. if (using_dac)
  2859. netdev->features |= NETIF_F_HIGHDMA;
  2860. netdev->mem_start = bnad->mmio_start;
  2861. netdev->mem_end = bnad->mmio_start + bnad->mmio_len - 1;
  2862. /* MTU range: 46 - 9000 */
  2863. netdev->min_mtu = ETH_ZLEN - ETH_HLEN;
  2864. netdev->max_mtu = BNAD_JUMBO_MTU;
  2865. netdev->netdev_ops = &bnad_netdev_ops;
  2866. bnad_set_ethtool_ops(netdev);
  2867. }
  2868. /*
  2869. * 1. Initialize the bnad structure
  2870. * 2. Setup netdev pointer in pci_dev
  2871. * 3. Initialize no. of TxQ & CQs & MSIX vectors
  2872. * 4. Initialize work queue.
  2873. */
  2874. static int
  2875. bnad_init(struct bnad *bnad,
  2876. struct pci_dev *pdev, struct net_device *netdev)
  2877. {
  2878. unsigned long flags;
  2879. SET_NETDEV_DEV(netdev, &pdev->dev);
  2880. pci_set_drvdata(pdev, netdev);
  2881. bnad->netdev = netdev;
  2882. bnad->pcidev = pdev;
  2883. bnad->mmio_start = pci_resource_start(pdev, 0);
  2884. bnad->mmio_len = pci_resource_len(pdev, 0);
  2885. bnad->bar0 = ioremap_nocache(bnad->mmio_start, bnad->mmio_len);
  2886. if (!bnad->bar0) {
  2887. dev_err(&pdev->dev, "ioremap for bar0 failed\n");
  2888. return -ENOMEM;
  2889. }
  2890. dev_info(&pdev->dev, "bar0 mapped to %p, len %llu\n", bnad->bar0,
  2891. (unsigned long long) bnad->mmio_len);
  2892. spin_lock_irqsave(&bnad->bna_lock, flags);
  2893. if (!bnad_msix_disable)
  2894. bnad->cfg_flags = BNAD_CF_MSIX;
  2895. bnad->cfg_flags |= BNAD_CF_DIM_ENABLED;
  2896. bnad_q_num_init(bnad);
  2897. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2898. bnad->msix_num = (bnad->num_tx * bnad->num_txq_per_tx) +
  2899. (bnad->num_rx * bnad->num_rxp_per_rx) +
  2900. BNAD_MAILBOX_MSIX_VECTORS;
  2901. bnad->txq_depth = BNAD_TXQ_DEPTH;
  2902. bnad->rxq_depth = BNAD_RXQ_DEPTH;
  2903. bnad->tx_coalescing_timeo = BFI_TX_COALESCING_TIMEO;
  2904. bnad->rx_coalescing_timeo = BFI_RX_COALESCING_TIMEO;
  2905. sprintf(bnad->wq_name, "%s_wq_%d", BNAD_NAME, bnad->id);
  2906. bnad->work_q = create_singlethread_workqueue(bnad->wq_name);
  2907. if (!bnad->work_q) {
  2908. iounmap(bnad->bar0);
  2909. return -ENOMEM;
  2910. }
  2911. return 0;
  2912. }
  2913. /*
  2914. * Must be called after bnad_pci_uninit()
  2915. * so that iounmap() and pci_set_drvdata(NULL)
  2916. * happens only after PCI uninitialization.
  2917. */
  2918. static void
  2919. bnad_uninit(struct bnad *bnad)
  2920. {
  2921. if (bnad->work_q) {
  2922. flush_workqueue(bnad->work_q);
  2923. destroy_workqueue(bnad->work_q);
  2924. bnad->work_q = NULL;
  2925. }
  2926. if (bnad->bar0)
  2927. iounmap(bnad->bar0);
  2928. }
  2929. /*
  2930. * Initialize locks
  2931. a) Per ioceth mutes used for serializing configuration
  2932. changes from OS interface
  2933. b) spin lock used to protect bna state machine
  2934. */
  2935. static void
  2936. bnad_lock_init(struct bnad *bnad)
  2937. {
  2938. spin_lock_init(&bnad->bna_lock);
  2939. mutex_init(&bnad->conf_mutex);
  2940. }
  2941. static void
  2942. bnad_lock_uninit(struct bnad *bnad)
  2943. {
  2944. mutex_destroy(&bnad->conf_mutex);
  2945. }
  2946. /* PCI Initialization */
  2947. static int
  2948. bnad_pci_init(struct bnad *bnad,
  2949. struct pci_dev *pdev, bool *using_dac)
  2950. {
  2951. int err;
  2952. err = pci_enable_device(pdev);
  2953. if (err)
  2954. return err;
  2955. err = pci_request_regions(pdev, BNAD_NAME);
  2956. if (err)
  2957. goto disable_device;
  2958. if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
  2959. *using_dac = true;
  2960. } else {
  2961. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2962. if (err)
  2963. goto release_regions;
  2964. *using_dac = false;
  2965. }
  2966. pci_set_master(pdev);
  2967. return 0;
  2968. release_regions:
  2969. pci_release_regions(pdev);
  2970. disable_device:
  2971. pci_disable_device(pdev);
  2972. return err;
  2973. }
  2974. static void
  2975. bnad_pci_uninit(struct pci_dev *pdev)
  2976. {
  2977. pci_release_regions(pdev);
  2978. pci_disable_device(pdev);
  2979. }
  2980. static int
  2981. bnad_pci_probe(struct pci_dev *pdev,
  2982. const struct pci_device_id *pcidev_id)
  2983. {
  2984. bool using_dac;
  2985. int err;
  2986. struct bnad *bnad;
  2987. struct bna *bna;
  2988. struct net_device *netdev;
  2989. struct bfa_pcidev pcidev_info;
  2990. unsigned long flags;
  2991. mutex_lock(&bnad_fwimg_mutex);
  2992. if (!cna_get_firmware_buf(pdev)) {
  2993. mutex_unlock(&bnad_fwimg_mutex);
  2994. dev_err(&pdev->dev, "failed to load firmware image!\n");
  2995. return -ENODEV;
  2996. }
  2997. mutex_unlock(&bnad_fwimg_mutex);
  2998. /*
  2999. * Allocates sizeof(struct net_device + struct bnad)
  3000. * bnad = netdev->priv
  3001. */
  3002. netdev = alloc_etherdev(sizeof(struct bnad));
  3003. if (!netdev) {
  3004. err = -ENOMEM;
  3005. return err;
  3006. }
  3007. bnad = netdev_priv(netdev);
  3008. bnad_lock_init(bnad);
  3009. bnad->id = atomic_inc_return(&bna_id) - 1;
  3010. mutex_lock(&bnad->conf_mutex);
  3011. /*
  3012. * PCI initialization
  3013. * Output : using_dac = 1 for 64 bit DMA
  3014. * = 0 for 32 bit DMA
  3015. */
  3016. using_dac = false;
  3017. err = bnad_pci_init(bnad, pdev, &using_dac);
  3018. if (err)
  3019. goto unlock_mutex;
  3020. /*
  3021. * Initialize bnad structure
  3022. * Setup relation between pci_dev & netdev
  3023. */
  3024. err = bnad_init(bnad, pdev, netdev);
  3025. if (err)
  3026. goto pci_uninit;
  3027. /* Initialize netdev structure, set up ethtool ops */
  3028. bnad_netdev_init(bnad, using_dac);
  3029. /* Set link to down state */
  3030. netif_carrier_off(netdev);
  3031. /* Setup the debugfs node for this bfad */
  3032. if (bna_debugfs_enable)
  3033. bnad_debugfs_init(bnad);
  3034. /* Get resource requirement form bna */
  3035. spin_lock_irqsave(&bnad->bna_lock, flags);
  3036. bna_res_req(&bnad->res_info[0]);
  3037. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3038. /* Allocate resources from bna */
  3039. err = bnad_res_alloc(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
  3040. if (err)
  3041. goto drv_uninit;
  3042. bna = &bnad->bna;
  3043. /* Setup pcidev_info for bna_init() */
  3044. pcidev_info.pci_slot = PCI_SLOT(bnad->pcidev->devfn);
  3045. pcidev_info.pci_func = PCI_FUNC(bnad->pcidev->devfn);
  3046. pcidev_info.device_id = bnad->pcidev->device;
  3047. pcidev_info.pci_bar_kva = bnad->bar0;
  3048. spin_lock_irqsave(&bnad->bna_lock, flags);
  3049. bna_init(bna, bnad, &pcidev_info, &bnad->res_info[0]);
  3050. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3051. bnad->stats.bna_stats = &bna->stats;
  3052. bnad_enable_msix(bnad);
  3053. err = bnad_mbox_irq_alloc(bnad);
  3054. if (err)
  3055. goto res_free;
  3056. /* Set up timers */
  3057. timer_setup(&bnad->bna.ioceth.ioc.ioc_timer, bnad_ioc_timeout, 0);
  3058. timer_setup(&bnad->bna.ioceth.ioc.hb_timer, bnad_ioc_hb_check, 0);
  3059. timer_setup(&bnad->bna.ioceth.ioc.iocpf_timer, bnad_iocpf_timeout, 0);
  3060. timer_setup(&bnad->bna.ioceth.ioc.sem_timer, bnad_iocpf_sem_timeout,
  3061. 0);
  3062. /*
  3063. * Start the chip
  3064. * If the call back comes with error, we bail out.
  3065. * This is a catastrophic error.
  3066. */
  3067. err = bnad_ioceth_enable(bnad);
  3068. if (err) {
  3069. dev_err(&pdev->dev, "initialization failed err=%d\n", err);
  3070. goto probe_success;
  3071. }
  3072. spin_lock_irqsave(&bnad->bna_lock, flags);
  3073. if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
  3074. bna_num_rxp_set(bna, BNAD_NUM_RXP + 1)) {
  3075. bnad_q_num_adjust(bnad, bna_attr(bna)->num_txq - 1,
  3076. bna_attr(bna)->num_rxp - 1);
  3077. if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
  3078. bna_num_rxp_set(bna, BNAD_NUM_RXP + 1))
  3079. err = -EIO;
  3080. }
  3081. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3082. if (err)
  3083. goto disable_ioceth;
  3084. spin_lock_irqsave(&bnad->bna_lock, flags);
  3085. bna_mod_res_req(&bnad->bna, &bnad->mod_res_info[0]);
  3086. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3087. err = bnad_res_alloc(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
  3088. if (err) {
  3089. err = -EIO;
  3090. goto disable_ioceth;
  3091. }
  3092. spin_lock_irqsave(&bnad->bna_lock, flags);
  3093. bna_mod_init(&bnad->bna, &bnad->mod_res_info[0]);
  3094. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3095. /* Get the burnt-in mac */
  3096. spin_lock_irqsave(&bnad->bna_lock, flags);
  3097. bna_enet_perm_mac_get(&bna->enet, bnad->perm_addr);
  3098. bnad_set_netdev_perm_addr(bnad);
  3099. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3100. mutex_unlock(&bnad->conf_mutex);
  3101. /* Finally, reguister with net_device layer */
  3102. err = register_netdev(netdev);
  3103. if (err) {
  3104. dev_err(&pdev->dev, "registering net device failed\n");
  3105. goto probe_uninit;
  3106. }
  3107. set_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags);
  3108. return 0;
  3109. probe_success:
  3110. mutex_unlock(&bnad->conf_mutex);
  3111. return 0;
  3112. probe_uninit:
  3113. mutex_lock(&bnad->conf_mutex);
  3114. bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
  3115. disable_ioceth:
  3116. bnad_ioceth_disable(bnad);
  3117. del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
  3118. del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
  3119. del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
  3120. spin_lock_irqsave(&bnad->bna_lock, flags);
  3121. bna_uninit(bna);
  3122. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3123. bnad_mbox_irq_free(bnad);
  3124. bnad_disable_msix(bnad);
  3125. res_free:
  3126. bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
  3127. drv_uninit:
  3128. /* Remove the debugfs node for this bnad */
  3129. kfree(bnad->regdata);
  3130. bnad_debugfs_uninit(bnad);
  3131. bnad_uninit(bnad);
  3132. pci_uninit:
  3133. bnad_pci_uninit(pdev);
  3134. unlock_mutex:
  3135. mutex_unlock(&bnad->conf_mutex);
  3136. bnad_lock_uninit(bnad);
  3137. free_netdev(netdev);
  3138. return err;
  3139. }
  3140. static void
  3141. bnad_pci_remove(struct pci_dev *pdev)
  3142. {
  3143. struct net_device *netdev = pci_get_drvdata(pdev);
  3144. struct bnad *bnad;
  3145. struct bna *bna;
  3146. unsigned long flags;
  3147. if (!netdev)
  3148. return;
  3149. bnad = netdev_priv(netdev);
  3150. bna = &bnad->bna;
  3151. if (test_and_clear_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags))
  3152. unregister_netdev(netdev);
  3153. mutex_lock(&bnad->conf_mutex);
  3154. bnad_ioceth_disable(bnad);
  3155. del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
  3156. del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
  3157. del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
  3158. spin_lock_irqsave(&bnad->bna_lock, flags);
  3159. bna_uninit(bna);
  3160. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3161. bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
  3162. bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
  3163. bnad_mbox_irq_free(bnad);
  3164. bnad_disable_msix(bnad);
  3165. bnad_pci_uninit(pdev);
  3166. mutex_unlock(&bnad->conf_mutex);
  3167. bnad_lock_uninit(bnad);
  3168. /* Remove the debugfs node for this bnad */
  3169. kfree(bnad->regdata);
  3170. bnad_debugfs_uninit(bnad);
  3171. bnad_uninit(bnad);
  3172. free_netdev(netdev);
  3173. }
  3174. static const struct pci_device_id bnad_pci_id_table[] = {
  3175. {
  3176. PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
  3177. PCI_DEVICE_ID_BROCADE_CT),
  3178. .class = PCI_CLASS_NETWORK_ETHERNET << 8,
  3179. .class_mask = 0xffff00
  3180. },
  3181. {
  3182. PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
  3183. BFA_PCI_DEVICE_ID_CT2),
  3184. .class = PCI_CLASS_NETWORK_ETHERNET << 8,
  3185. .class_mask = 0xffff00
  3186. },
  3187. {0, },
  3188. };
  3189. MODULE_DEVICE_TABLE(pci, bnad_pci_id_table);
  3190. static struct pci_driver bnad_pci_driver = {
  3191. .name = BNAD_NAME,
  3192. .id_table = bnad_pci_id_table,
  3193. .probe = bnad_pci_probe,
  3194. .remove = bnad_pci_remove,
  3195. };
  3196. static int __init
  3197. bnad_module_init(void)
  3198. {
  3199. int err;
  3200. pr_info("bna: QLogic BR-series 10G Ethernet driver - version: %s\n",
  3201. BNAD_VERSION);
  3202. bfa_nw_ioc_auto_recover(bnad_ioc_auto_recover);
  3203. err = pci_register_driver(&bnad_pci_driver);
  3204. if (err < 0) {
  3205. pr_err("bna: PCI driver registration failed err=%d\n", err);
  3206. return err;
  3207. }
  3208. return 0;
  3209. }
  3210. static void __exit
  3211. bnad_module_exit(void)
  3212. {
  3213. pci_unregister_driver(&bnad_pci_driver);
  3214. release_firmware(bfi_fw);
  3215. }
  3216. module_init(bnad_module_init);
  3217. module_exit(bnad_module_exit);
  3218. MODULE_AUTHOR("Brocade");
  3219. MODULE_LICENSE("GPL");
  3220. MODULE_DESCRIPTION("QLogic BR-series 10G PCIe Ethernet driver");
  3221. MODULE_VERSION(BNAD_VERSION);
  3222. MODULE_FIRMWARE(CNA_FW_FILE_CT);
  3223. MODULE_FIRMWARE(CNA_FW_FILE_CT2);