bnad.c 93 KB

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