bnad.c 93 KB

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