bnad.c 78 KB

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