bnad.c 85 KB

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