bnad.c 78 KB

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