bnad.c 79 KB

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