bnad.c 83 KB

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