bnad.c 84 KB

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