bnad.c 78 KB

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