bnad.c 92 KB

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